--- ray/src/cal/lam.c 2010/06/18 21:22:49 1.11 +++ ray/src/cal/lam.c 2016/08/18 00:52:47 1.17 @@ -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.17 2016/08/18 00:52:47 greg Exp $"; #endif /* * lam.c - simple program to laminate files. @@ -14,11 +14,14 @@ static const char RCSid[] = "$Id: lam.c,v 1.11 2010/06 #include "platform.h" #include "rtprocess.h" +#include "rtio.h" #define MAXFILE 512 /* maximum number of files */ -#define MAXLINE 4096 /* maximum input line */ +#define MAXLINE 65536 /* maximum input line */ +long incnt = 0; /* limit number of records? */ + FILE *input[MAXFILE]; int bytsiz[MAXFILE]; char *tabc[MAXFILE]; @@ -32,6 +35,7 @@ int argc; char *argv[]; { int unbuff = 0; + int binout = 0; int i; char *curtab; int curbytes; @@ -51,6 +55,9 @@ char *argv[]; break; case 'i': switch (argv[i][2]) { + case 'n': + incnt = atol(argv[++i]); + break; case 'f': curbytes = sizeof(float); break; @@ -67,25 +74,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 +108,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,20 +137,27 @@ char *argv[]; exit(1); } } - puteol = 0; /* check for ASCII output */ + 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; /* any ASCII output at all? */ + for (i = nfiles; i--; ) if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { puteol++; break; } - for ( ; ; ) { /* main loop */ + do { /* main loop */ for (i = 0; i < nfiles; i++) { if (bytsiz[i]) { /* binary file */ - if (fread(buf, bytsiz[i], 1, input[i]) < 1) + if (getbinary(buf, bytsiz[i], 1, input[i]) < 1) break; if (i) fputs(tabc[i], stdout); - fwrite(buf, bytsiz[i], 1, stdout); + putbinary(buf, bytsiz[i], 1, stdout); } else { if (fgets(buf, MAXLINE, input[i]) == NULL) break; @@ -151,6 +173,6 @@ char *argv[]; putchar('\n'); if (unbuff) fflush(stdout); - } + } while (--incnt); return(0); }