=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/lam/lam.c,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- src/usr.bin/lam/lam.c 2004/12/27 23:37:37 1.12 +++ src/usr.bin/lam/lam.c 2007/06/26 05:14:52 1.13 @@ -1,4 +1,4 @@ -/* $OpenBSD: lam.c,v 1.12 2004/12/27 23:37:37 deraadt Exp $ */ +/* $OpenBSD: lam.c,v 1.13 2007/06/26 05:14:52 ray Exp $ */ /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $ */ /*- @@ -40,7 +40,7 @@ #if 0 static const char sccsid[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93"; #endif -static const char rcsid[] = "$OpenBSD: lam.c,v 1.12 2004/12/27 23:37:37 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: lam.c,v 1.13 2007/06/26 05:14:52 ray Exp $"; #endif /* not lint */ /* @@ -48,9 +48,10 @@ * Author: John Kunze, UCB */ +#include + #include #include -#include #include #include #include @@ -64,9 +65,10 @@ char eol; /* end of line character */ char *sepstring; /* string to print before each line */ char *format; /* printf(3) style string spec. */ -} input[OPEN_MAX]; +} input[NOFILE_MAX + 1]; /* last one is for the last -s arg. */ +#define INPUTSIZE sizeof(input) / sizeof(*input) -int morefiles; /* set by getargs(), changed by gatherline() */ +int numfiles; /* number of open files */ int nofinalnl; /* normally append \n to each output line */ char line[BIGBUFSIZ]; char *linep; @@ -79,19 +81,27 @@ int main(int argc, char *argv[]) { - struct openfile *ip; + int i; + /* Process arguments, set numfiles to file argument count. */ getargs(argc, argv); - if (!morefiles) + if (numfiles == 0) usage(); + /* Concatenate lines from each file, then print. */ for (;;) { linep = line; - for (ip = input; ip->fp != NULL; ip++) - linep = gatherline(ip); - if (!morefiles) + /* + * For each file that has a line to print, numfile is + * incremented. Thus if numfiles is 0, we are done. + */ + numfiles = 0; + for (i = 0; i < INPUTSIZE - 1 && input[i].fp != NULL; i++) + linep = gatherline(&input[i]); + if (numfiles == 0) exit(0); fputs(line, stdout); - fputs(ip->sepstring, stdout); + /* Print terminating -s argument. */ + fputs(input[i].sepstring, stdout); if (!nofinalnl) putchar('\n'); } @@ -140,7 +150,10 @@ case -1: if (optind >= argc) break; /* to support "--" */ - morefiles++; + /* This is a file, not a flag. */ + ++numfiles; + if (numfiles >= INPUTSIZE) + errx(1, "too many files"); if (strcmp(argv[optind], "-") == 0) ip->fp = stdin; else if ((ip->fp = fopen(argv[optind], "r")) == NULL) @@ -181,6 +194,10 @@ return (lp); } +/* + * Grab line from file, appending to linep. Increments numfiles if file + * is still open. + */ char * gatherline(struct openfile *ip) { @@ -201,9 +218,10 @@ ip->eof = 1; if (ip->fp == stdin) fclose(stdin); - morefiles--; return (pad(ip)); } + /* Something will be printed. */ + numfiles++; n = strlcpy(lp, ip->sepstring, line + sizeof(line) - lp); lp += (n < line + sizeof(line) - lp) ? n : strlen(lp); n = snprintf(lp, line + sizeof(line) - lp, ip->format, s);