--- ray/src/cal/lam.c 2010/06/18 21:22:49 1.11 +++ ray/src/cal/lam.c 2014/03/09 20:07:27 1.14 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: lam.c,v 1.11 2010/06/18 21:22:49 greg Exp $"; +static const char RCSid[] = "$Id: lam.c,v 1.14 2014/03/09 20:07:27 greg Exp $"; #endif /* * lam.c - simple program to laminate files. @@ -17,7 +17,7 @@ static const char RCSid[] = "$Id: lam.c,v 1.11 2010/06 #define MAXFILE 512 /* maximum number of files */ -#define MAXLINE 4096 /* maximum input line */ +#define MAXLINE 65536 /* maximum input line */ FILE *input[MAXFILE]; int bytsiz[MAXFILE]; @@ -32,6 +32,7 @@ int argc; char *argv[]; { int unbuff = 0; + int binout = 0; int i; char *curtab; int curbytes; @@ -67,25 +68,31 @@ char *argv[]; curbytes = 1; break; case 'a': - curbytes = argv[i][3] ? 1 : 0; + curbytes = argv[i][3] ? -1 : 0; break; default: goto badopt; } if (isdigit(argv[i][3])) curbytes *= atoi(argv[i]+3); - if (curbytes < 0 || curbytes > MAXLINE) { + if (abs(curbytes) > MAXLINE) { fputs(argv[0], stderr); - fputs(": illegal input size\n", stderr); + fputs(": input size too big\n", stderr); exit(1); } - if (curbytes) + if (curbytes) { curtab = ""; + ++binout; + } break; case '\0': tabc[nfiles] = curtab; - bytsiz[nfiles] = curbytes; - input[nfiles++] = stdin; + input[nfiles] = stdin; + if (curbytes > 0) + SET_FILE_BINARY(input[nfiles]); + else + curbytes = -curbytes; + bytsiz[nfiles++] = curbytes; break; badopt:; default: @@ -95,26 +102,28 @@ char *argv[]; } } else if (argv[i][0] == '!') { tabc[nfiles] = curtab; - bytsiz[nfiles] = curbytes; if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { fputs(argv[i], stderr); fputs(": cannot start command\n", stderr); exit(1); } - if (bytsiz[nfiles]) + if (curbytes > 0) SET_FILE_BINARY(input[nfiles]); - ++nfiles; + else + curbytes = -curbytes; + bytsiz[nfiles++] = curbytes; } else { tabc[nfiles] = curtab; - bytsiz[nfiles] = curbytes; if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { fputs(argv[i], stderr); fputs(": cannot open file\n", stderr); exit(1); } - if (bytsiz[nfiles]) + if (curbytes > 0) SET_FILE_BINARY(input[nfiles]); - ++nfiles; + else + curbytes = -curbytes; + bytsiz[nfiles++] = curbytes; } if (nfiles >= MAXFILE) { fputs(argv[0], stderr); @@ -122,6 +131,13 @@ char *argv[]; exit(1); } } + if (binout) /* binary output? */ + SET_FILE_BINARY(stdout); +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + for (i = nfiles; i--; ) + flockfile(input[i]); + flockfile(stdout); +#endif puteol = 0; /* check for ASCII output */ for (i = nfiles; i--; ) if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') {