=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/lam/lam.c,v retrieving revision 1.23 retrieving revision 1.24 diff -c -r1.23 -r1.24 *** src/usr.bin/lam/lam.c 2021/12/02 15:15:29 1.23 --- src/usr.bin/lam/lam.c 2021/12/03 15:15:22 1.24 *************** *** 1,4 **** ! /* $OpenBSD: lam.c,v 1.23 2021/12/02 15:15:29 jmc Exp $ */ /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $ */ /*- --- 1,4 ---- ! /* $OpenBSD: lam.c,v 1.24 2021/12/03 15:15:22 deraadt Exp $ */ /* $NetBSD: lam.c,v 1.2 1994/11/14 20:27:42 jtc Exp $ */ /*- *************** *** 35,47 **** * Author: John Kunze, UCB */ ! #include /* NOFILE_MAX */ #include #include #include #include #include #include #include --- 35,48 ---- * Author: John Kunze, UCB */ ! #include #include #include #include #include #include + #include #include #include *************** *** 56,65 **** char eol; /* end of line character */ char align; /* '0' for zero fill, '-' for left align */ char *sepstring; /* string to print before each line */ ! } 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; --- 57,66 ---- char eol; /* end of line character */ char align; /* '0' for zero fill, '-' for left align */ char *sepstring; /* string to print before each line */ ! } *input; ! int inputsize; /* number of openfile entries */ ! int output; /* line output produced */ int nofinalnl; /* normally append \n to each output line */ char line[BIGBUFSIZ]; char *linep; *************** *** 81,89 **** if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); - /* Process arguments, set numfiles to file argument count. */ getargs(argc, argv); ! if (numfiles == 0) usage(); if (pledge("stdio", NULL) == -1) --- 82,89 ---- if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); getargs(argc, argv); ! if (inputsize == 0) usage(); if (pledge("stdio", NULL) == -1) *************** *** 93,105 **** 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. */ --- 93,105 ---- for (;;) { linep = line; /* ! * For each file that has a line to print, output is * incremented. Thus if numfiles is 0, we are done. */ ! output = 0; ! for (i = 0; i < inputsize && input[i].fp != NULL; i++) linep = gatherline(&input[i]); ! if (output == 0) exit(0); fputs(line, stdout); /* Print terminating -s argument. */ *************** *** 112,122 **** void getargs(int argc, char *argv[]) { ! struct openfile *ip = input; const char *errstr; char *p, *q; int ch, P, S, F, T; P = S = F = T = 0; /* capitalized options */ while (optind < argc) { switch (ch = getopt(argc, argv, "F:f:P:p:S:s:T:t:")) { --- 112,128 ---- void getargs(int argc, char *argv[]) { ! struct openfile *ip; const char *errstr; char *p, *q; + void *tmp; int ch, P, S, F, T; + input = calloc(inputsize+1, sizeof *input); + if (input == NULL) + errx(1, "too many files"); + ip = &input[inputsize]; + P = S = F = T = 0; /* capitalized options */ while (optind < argc) { switch (ch = getopt(argc, argv, "F:f:P:p:S:s:T:t:")) { *************** *** 165,173 **** 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) --- 171,176 ---- *************** *** 185,191 **** } else ip->maxwidth = INT_MAX; } ! ip++; optind++; break; default: --- 188,203 ---- } else ip->maxwidth = INT_MAX; } ! ! ++inputsize; ! ! /* Prepare for next file argument */ ! tmp = recallocarray(input, inputsize, ! inputsize+1, sizeof *input); ! if (tmp == NULL) ! errx(1, "too many files"); ! input = tmp; ! ip = &input[inputsize]; optind++; break; default: *************** *** 215,221 **** } /* ! * Grab line from file, appending to linep. Increments numfiles if file * is still open. */ char * --- 227,233 ---- } /* ! * Grab line from file, appending to linep. Increments printed if file * is still open. */ char * *************** *** 241,247 **** 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); width = mbswidth_truncate(s, ip->maxwidth); --- 253,259 ---- return (pad(ip)); } /* Something will be printed. */ ! output++; n = strlcpy(lp, ip->sepstring, line + sizeof(line) - lp); lp += (n < line + sizeof(line) - lp) ? n : strlen(lp); width = mbswidth_truncate(s, ip->maxwidth);