Use err(3). Add prototypes. Silent -Wall.

This commit is contained in:
Philippe Charnier 1997-07-31 06:59:26 +00:00
parent 7e19b1ec24
commit 1e133604e1
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=27787
2 changed files with 39 additions and 41 deletions

View file

@ -42,13 +42,13 @@
.Nm paste .Nm paste
.Nd merge corresponding or subsequent lines of files .Nd merge corresponding or subsequent lines of files
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm paste .Nm
.Op Fl s .Op Fl s
.Op Fl d Ar list .Op Fl d Ar list
.Ar file ... .Ar file ...
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm paste .Nm
utility concatenates the corresponding lines of the given input files, utility concatenates the corresponding lines of the given input files,
replacing all but the last file's newline characters with a single tab replacing all but the last file's newline characters with a single tab
character, and writes the resulting lines to standard output. character, and writes the resulting lines to standard output.
@ -71,7 +71,7 @@ is reused.
This continues until a line from the last input file (in default operation) This continues until a line from the last input file (in default operation)
or the last line in each file (using the -s option) is displayed, at which or the last line in each file (using the -s option) is displayed, at which
time time
.Nm paste .Nm
begins selecting characters from the beginning of begins selecting characters from the beginning of
.Ar list .Ar list
again. again.
@ -107,13 +107,13 @@ for each instance of
.Ql Fl . .Ql Fl .
.Pp .Pp
The The
.Nm paste .Nm
utility exits 0 on success, and >0 if an error occurs. utility exits 0 on success, and >0 if an error occurs.
.Sh SEE ALSO .Sh SEE ALSO
.Xr cut 1 .Xr cut 1
.Sh STANDARDS .Sh STANDARDS
The The
.Nm paste .Nm
utility is expected to be utility is expected to be
.St -p1003.2 .St -p1003.2
compatible. compatible.

View file

@ -35,30 +35,40 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\ "@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
#if 0
static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)paste.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
#include <err.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
char *delim; char *delim;
int delimcnt; int delimcnt;
void parallel __P((char **));
void sequential __P((char **));
int tr __P((char *));
static void usage __P((void));
int
main(argc, argv) main(argc, argv)
int argc; int argc;
char **argv; char **argv;
{ {
extern char *optarg;
extern int optind;
int ch, seq; int ch, seq;
seq = 0; seq = 0;
@ -96,6 +106,7 @@ typedef struct _list {
char *name; char *name;
} LIST; } LIST;
void
parallel(argv) parallel(argv)
char **argv; char **argv;
{ {
@ -106,18 +117,13 @@ parallel(argv)
int opencnt, output; int opencnt, output;
char buf[_POSIX2_LINE_MAX + 1], *malloc(); char buf[_POSIX2_LINE_MAX + 1], *malloc();
for (cnt = 0, head = NULL; p = *argv; ++argv, ++cnt) { for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) {
if (!(lp = (LIST *)malloc((u_int)sizeof(LIST)))) { if (!(lp = (LIST *)malloc((u_int)sizeof(LIST))))
(void)fprintf(stderr, "paste: %s.\n", strerror(ENOMEM)); errx(1, "%s", strerror(ENOMEM));
exit(1);
}
if (p[0] == '-' && !p[1]) if (p[0] == '-' && !p[1])
lp->fp = stdin; lp->fp = stdin;
else if (!(lp->fp = fopen(p, "r"))) { else if (!(lp->fp = fopen(p, "r")))
(void)fprintf(stderr, "paste: %s: %s.\n", p, err(1, "%s", p);
strerror(errno));
exit(1);
}
lp->next = NULL; lp->next = NULL;
lp->cnt = cnt; lp->cnt = cnt;
lp->name = p; lp->name = p;
@ -146,12 +152,8 @@ parallel(argv)
putchar(ch); putchar(ch);
continue; continue;
} }
if (!(p = index(buf, '\n'))) { if (!(p = index(buf, '\n')))
(void)fprintf(stderr, errx(1, "%s: input line too long", lp->name);
"paste: %s: input line too long.\n",
lp->name);
exit(1);
}
*p = '\0'; *p = '\0';
/* /*
* make sure that we don't print any delimiters * make sure that we don't print any delimiters
@ -160,9 +162,9 @@ parallel(argv)
if (!output) { if (!output) {
output = 1; output = 1;
for (cnt = 0; cnt < lp->cnt; ++cnt) for (cnt = 0; cnt < lp->cnt; ++cnt)
if (ch = delim[cnt % delimcnt]) if ((ch = delim[cnt % delimcnt]))
putchar(ch); putchar(ch);
} else if (ch = delim[(lp->cnt - 1) % delimcnt]) } else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
putchar(ch); putchar(ch);
(void)printf("%s", buf); (void)printf("%s", buf);
} }
@ -171,6 +173,7 @@ parallel(argv)
} }
} }
void
sequential(argv) sequential(argv)
char **argv; char **argv;
{ {
@ -179,27 +182,22 @@ sequential(argv)
register char ch, *p, *dp; register char ch, *p, *dp;
char buf[_POSIX2_LINE_MAX + 1]; char buf[_POSIX2_LINE_MAX + 1];
for (; p = *argv; ++argv) { for (; (p = *argv); ++argv) {
if (p[0] == '-' && !p[1]) if (p[0] == '-' && !p[1])
fp = stdin; fp = stdin;
else if (!(fp = fopen(p, "r"))) { else if (!(fp = fopen(p, "r"))) {
(void)fprintf(stderr, "paste: %s: %s.\n", p, warn("%s", p);
strerror(errno));
continue; continue;
} }
if (fgets(buf, sizeof(buf), fp)) { if (fgets(buf, sizeof(buf), fp)) {
for (cnt = 0, dp = delim;;) { for (cnt = 0, dp = delim;;) {
if (!(p = index(buf, '\n'))) { if (!(p = index(buf, '\n')))
(void)fprintf(stderr, errx(1, "%s: input line too long", *argv);
"paste: %s: input line too long.\n",
*argv);
exit(1);
}
*p = '\0'; *p = '\0';
(void)printf("%s", buf); (void)printf("%s", buf);
if (!fgets(buf, sizeof(buf), fp)) if (!fgets(buf, sizeof(buf), fp))
break; break;
if (ch = *dp++) if ((ch = *dp++))
putchar(ch); putchar(ch);
if (++cnt == delimcnt) { if (++cnt == delimcnt) {
dp = delim; dp = delim;
@ -213,6 +211,7 @@ sequential(argv)
} }
} }
int
tr(arg) tr(arg)
char *arg; char *arg;
{ {
@ -237,15 +236,14 @@ tr(arg)
} else } else
*arg = ch; *arg = ch;
if (!cnt) { if (!cnt)
(void)fprintf(stderr, "paste: no delimiters specified.\n"); errx(1, "no delimiters specified");
exit(1);
}
return(cnt); return(cnt);
} }
static void
usage() usage()
{ {
(void)fprintf(stderr, "paste: [-s] [-d delimiters] file ...\n"); (void)fprintf(stderr, "usage: paste [-s] [-d delimiters] file ...\n");
exit(1); exit(1);
} }