=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/lam/lam.c,v retrieving revision 1.12 retrieving revision 1.13 diff -c -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 **** ! /* $OpenBSD: lam.c,v 1.12 2004/12/27 23:37:37 deraadt Exp $ */ /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $ */ /*- --- 1,4 ---- ! /* $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,46 **** #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 $"; #endif /* not lint */ /* --- 40,46 ---- #if 0 static const char sccsid[] = "@(#)lam.c 8.1 (Berkeley) 6/6/93"; #endif ! static const char rcsid[] = "$OpenBSD: lam.c,v 1.13 2007/06/26 05:14:52 ray Exp $"; #endif /* not lint */ /* *************** *** 48,56 **** * Author: John Kunze, UCB */ #include #include - #include #include #include #include --- 48,57 ---- * Author: John Kunze, UCB */ + #include + #include #include #include #include #include *************** *** 64,72 **** char eol; /* end of line character */ char *sepstring; /* string to print before each line */ char *format; /* printf(3) style string spec. */ ! } input[OPEN_MAX]; ! int morefiles; /* set by getargs(), changed by gatherline() */ int nofinalnl; /* normally append \n to each output line */ char line[BIGBUFSIZ]; char *linep; --- 65,74 ---- char eol; /* end of line character */ char *sepstring; /* string to print before each line */ char *format; /* printf(3) style string spec. */ ! } input[NOFILE_MAX + 1]; /* last one is for the last -s arg. */ ! #define INPUTSIZE sizeof(input) / sizeof(*input) ! int numfiles; /* number of open files */ int nofinalnl; /* normally append \n to each output line */ char line[BIGBUFSIZ]; char *linep; *************** *** 79,97 **** int main(int argc, char *argv[]) { ! struct openfile *ip; getargs(argc, argv); ! if (!morefiles) usage(); for (;;) { linep = line; ! for (ip = input; ip->fp != NULL; ip++) ! linep = gatherline(ip); ! if (!morefiles) exit(0); fputs(line, stdout); ! fputs(ip->sepstring, stdout); if (!nofinalnl) putchar('\n'); } --- 81,107 ---- int main(int argc, char *argv[]) { ! int i; + /* Process arguments, set numfiles to file argument count. */ getargs(argc, argv); ! if (numfiles == 0) usage(); + /* Concatenate lines from each file, then print. */ for (;;) { linep = line; ! /* ! * 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); ! /* Print terminating -s argument. */ ! fputs(input[i].sepstring, stdout); if (!nofinalnl) putchar('\n'); } *************** *** 140,146 **** case -1: if (optind >= argc) break; /* to support "--" */ ! morefiles++; if (strcmp(argv[optind], "-") == 0) ip->fp = stdin; else if ((ip->fp = fopen(argv[optind], "r")) == NULL) --- 150,159 ---- case -1: if (optind >= argc) break; /* to support "--" */ ! /* 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,186 **** --- 194,203 ---- return (lp); } + /* + * Grab line from file, appending to linep. Increments numfiles if file + * is still open. + */ char * gatherline(struct openfile *ip) { *************** *** 201,209 **** ip->eof = 1; if (ip->fp == stdin) fclose(stdin); - morefiles--; return (pad(ip)); } 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); --- 218,227 ---- ip->eof = 1; if (ip->fp == stdin) fclose(stdin); 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);