--- ray/src/cal/lam.c 2006/05/07 15:44:28 1.9 +++ ray/src/cal/lam.c 2013/07/26 16:45:04 1.12 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: lam.c,v 1.9 2006/05/07 15:44:28 greg Exp $"; +static const char RCSid[] = "$Id: lam.c,v 1.12 2013/07/26 16:45:04 greg Exp $"; #endif /* * lam.c - simple program to laminate files. @@ -31,10 +31,11 @@ main(argc, argv) int argc; char *argv[]; { - register int i; + int unbuff = 0; + int i; char *curtab; int curbytes; - int running, puteol; + int puteol; curtab = "\t"; curbytes = 0; @@ -45,6 +46,9 @@ char *argv[]; case 't': curtab = argv[i]+2; break; + case 'u': + unbuff = !unbuff; + break; case 'i': switch (argv[i][2]) { case 'f': @@ -53,20 +57,26 @@ char *argv[]; case 'd': curbytes = sizeof(double); break; - case 'w': + case 'i': curbytes = sizeof(int); break; + case 'w': + curbytes = 2; + break; + case 'b': + 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) @@ -74,8 +84,12 @@ char *argv[]; 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: @@ -85,26 +99,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); @@ -112,34 +128,35 @@ char *argv[]; exit(1); } } - puteol = 0; /* check for tab character */ + puteol = 0; /* check for ASCII output */ for (i = nfiles; i--; ) - if (isprint(tabc[i][0]) || tabc[i][0] == '\t') { + if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { puteol++; break; } - do { - running = 0; + for ( ; ; ) { /* main loop */ for (i = 0; i < nfiles; i++) { if (bytsiz[i]) { /* binary file */ - if (fread(buf, bytsiz[i], 1, input[i]) == 1) { - if (i) - fputs(tabc[i], stdout); - fwrite(buf, bytsiz[i], 1, stdout); - running++; - } - } else if (fgets(buf, MAXLINE, input[i]) != NULL) { + if (fread(buf, bytsiz[i], 1, input[i]) < 1) + break; if (i) fputs(tabc[i], stdout); + fwrite(buf, bytsiz[i], 1, stdout); + } else { + if (fgets(buf, MAXLINE, input[i]) == NULL) + break; + if (i) + fputs(tabc[i], stdout); buf[strlen(buf)-1] = '\0'; fputs(buf, stdout); - puteol++; - running++; } } - if (running && puteol) + if (i < nfiles) + break; + if (puteol) putchar('\n'); - } while (running); - - exit(0); + if (unbuff) + fflush(stdout); + } + return(0); }