| 17 |
|
|
| 18 |
|
#define MAXFILE 512 /* maximum number of files */ |
| 19 |
|
|
| 20 |
< |
#define MAXLINE 4096 /* maximum input line */ |
| 20 |
> |
#define MAXLINE 65536 /* maximum input line */ |
| 21 |
|
|
| 22 |
+ |
long incnt = 0; /* limit number of records? */ |
| 23 |
+ |
|
| 24 |
|
FILE *input[MAXFILE]; |
| 25 |
|
int bytsiz[MAXFILE]; |
| 26 |
|
char *tabc[MAXFILE]; |
| 34 |
|
char *argv[]; |
| 35 |
|
{ |
| 36 |
|
int unbuff = 0; |
| 37 |
+ |
int binout = 0; |
| 38 |
|
int i; |
| 39 |
|
char *curtab; |
| 40 |
|
int curbytes; |
| 54 |
|
break; |
| 55 |
|
case 'i': |
| 56 |
|
switch (argv[i][2]) { |
| 57 |
+ |
case 'n': |
| 58 |
+ |
incnt = atol(argv[++i]); |
| 59 |
+ |
break; |
| 60 |
|
case 'f': |
| 61 |
|
curbytes = sizeof(float); |
| 62 |
|
break; |
| 73 |
|
curbytes = 1; |
| 74 |
|
break; |
| 75 |
|
case 'a': |
| 76 |
< |
curbytes = argv[i][3] ? 1 : 0; |
| 76 |
> |
curbytes = argv[i][3] ? -1 : 0; |
| 77 |
|
break; |
| 78 |
|
default: |
| 79 |
|
goto badopt; |
| 80 |
|
} |
| 81 |
|
if (isdigit(argv[i][3])) |
| 82 |
|
curbytes *= atoi(argv[i]+3); |
| 83 |
< |
if (curbytes < 0 || curbytes > MAXLINE) { |
| 83 |
> |
if (abs(curbytes) > MAXLINE) { |
| 84 |
|
fputs(argv[0], stderr); |
| 85 |
< |
fputs(": illegal input size\n", stderr); |
| 85 |
> |
fputs(": input size too big\n", stderr); |
| 86 |
|
exit(1); |
| 87 |
|
} |
| 88 |
< |
if (curbytes) |
| 88 |
> |
if (curbytes) { |
| 89 |
|
curtab = ""; |
| 90 |
+ |
++binout; |
| 91 |
+ |
} |
| 92 |
|
break; |
| 93 |
|
case '\0': |
| 94 |
|
tabc[nfiles] = curtab; |
| 95 |
< |
bytsiz[nfiles] = curbytes; |
| 96 |
< |
input[nfiles++] = stdin; |
| 95 |
> |
input[nfiles] = stdin; |
| 96 |
> |
if (curbytes > 0) |
| 97 |
> |
SET_FILE_BINARY(input[nfiles]); |
| 98 |
> |
else |
| 99 |
> |
curbytes = -curbytes; |
| 100 |
> |
bytsiz[nfiles++] = curbytes; |
| 101 |
|
break; |
| 102 |
|
badopt:; |
| 103 |
|
default: |
| 107 |
|
} |
| 108 |
|
} else if (argv[i][0] == '!') { |
| 109 |
|
tabc[nfiles] = curtab; |
| 98 |
– |
bytsiz[nfiles] = curbytes; |
| 110 |
|
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
| 111 |
|
fputs(argv[i], stderr); |
| 112 |
|
fputs(": cannot start command\n", stderr); |
| 113 |
|
exit(1); |
| 114 |
|
} |
| 115 |
< |
if (bytsiz[nfiles]) |
| 115 |
> |
if (curbytes > 0) |
| 116 |
|
SET_FILE_BINARY(input[nfiles]); |
| 117 |
< |
++nfiles; |
| 117 |
> |
else |
| 118 |
> |
curbytes = -curbytes; |
| 119 |
> |
bytsiz[nfiles++] = curbytes; |
| 120 |
|
} else { |
| 121 |
|
tabc[nfiles] = curtab; |
| 109 |
– |
bytsiz[nfiles] = curbytes; |
| 122 |
|
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
| 123 |
|
fputs(argv[i], stderr); |
| 124 |
|
fputs(": cannot open file\n", stderr); |
| 125 |
|
exit(1); |
| 126 |
|
} |
| 127 |
< |
if (bytsiz[nfiles]) |
| 127 |
> |
if (curbytes > 0) |
| 128 |
|
SET_FILE_BINARY(input[nfiles]); |
| 129 |
< |
++nfiles; |
| 129 |
> |
else |
| 130 |
> |
curbytes = -curbytes; |
| 131 |
> |
bytsiz[nfiles++] = curbytes; |
| 132 |
|
} |
| 133 |
|
if (nfiles >= MAXFILE) { |
| 134 |
|
fputs(argv[0], stderr); |
| 136 |
|
exit(1); |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
< |
puteol = 0; /* check for ASCII output */ |
| 139 |
> |
if (binout) /* binary output? */ |
| 140 |
> |
SET_FILE_BINARY(stdout); |
| 141 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 142 |
|
for (i = nfiles; i--; ) |
| 143 |
+ |
flockfile(input[i]); |
| 144 |
+ |
flockfile(stdout); |
| 145 |
+ |
#endif |
| 146 |
+ |
puteol = 0; /* any ASCII output at all? */ |
| 147 |
+ |
for (i = nfiles; i--; ) |
| 148 |
|
if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
| 149 |
|
puteol++; |
| 150 |
|
break; |
| 151 |
|
} |
| 152 |
< |
for ( ; ; ) { /* main loop */ |
| 152 |
> |
do { /* main loop */ |
| 153 |
|
for (i = 0; i < nfiles; i++) { |
| 154 |
|
if (bytsiz[i]) { /* binary file */ |
| 155 |
|
if (fread(buf, bytsiz[i], 1, input[i]) < 1) |
| 172 |
|
putchar('\n'); |
| 173 |
|
if (unbuff) |
| 174 |
|
fflush(stdout); |
| 175 |
< |
} |
| 175 |
> |
} while (--incnt); |
| 176 |
|
return(0); |
| 177 |
|
} |