| 14 |
|
|
| 15 |
|
#include "platform.h" |
| 16 |
|
#include "rtprocess.h" |
| 17 |
+ |
#include "rtio.h" |
| 18 |
|
|
| 19 |
|
#define MAXFILE 512 /* maximum number of files */ |
| 20 |
|
|
| 21 |
< |
#define MAXLINE 4096 /* maximum input line */ |
| 21 |
> |
#define MAXLINE 65536 /* maximum input line */ |
| 22 |
|
|
| 23 |
|
FILE *input[MAXFILE]; |
| 24 |
|
int bytsiz[MAXFILE]; |
| 32 |
|
int argc; |
| 33 |
|
char *argv[]; |
| 34 |
|
{ |
| 35 |
< |
register int i; |
| 36 |
< |
char *curtab; |
| 37 |
< |
int curbytes; |
| 38 |
< |
int running, puteol; |
| 35 |
> |
long incnt = 0; |
| 36 |
> |
int unbuff = 0; |
| 37 |
> |
int binout = 0; |
| 38 |
> |
char *curtab = "\t"; |
| 39 |
> |
int curbytes = 0; |
| 40 |
> |
int puteol; |
| 41 |
> |
int i; |
| 42 |
|
|
| 39 |
– |
curtab = "\t"; |
| 40 |
– |
curbytes = 0; |
| 43 |
|
nfiles = 0; |
| 44 |
|
for (i = 1; i < argc; i++) { |
| 45 |
|
if (argv[i][0] == '-') { |
| 47 |
|
case 't': |
| 48 |
|
curtab = argv[i]+2; |
| 49 |
|
break; |
| 50 |
+ |
case 'u': |
| 51 |
+ |
unbuff = !unbuff; |
| 52 |
+ |
break; |
| 53 |
|
case 'i': |
| 54 |
|
switch (argv[i][2]) { |
| 55 |
+ |
case 'n': |
| 56 |
+ |
incnt = atol(argv[++i]); |
| 57 |
+ |
break; |
| 58 |
|
case 'f': |
| 59 |
|
curbytes = sizeof(float); |
| 60 |
|
break; |
| 61 |
|
case 'd': |
| 62 |
|
curbytes = sizeof(double); |
| 63 |
|
break; |
| 64 |
< |
case 'w': |
| 64 |
> |
case 'i': |
| 65 |
|
curbytes = sizeof(int); |
| 66 |
|
break; |
| 67 |
+ |
case 'w': |
| 68 |
+ |
curbytes = 2; |
| 69 |
+ |
break; |
| 70 |
+ |
case 'b': |
| 71 |
+ |
curbytes = 1; |
| 72 |
+ |
break; |
| 73 |
|
case 'a': |
| 74 |
< |
curbytes = argv[i][3] ? 1 : 0; |
| 74 |
> |
curbytes = argv[i][3] ? -1 : 0; |
| 75 |
|
break; |
| 76 |
|
default: |
| 77 |
|
goto badopt; |
| 78 |
|
} |
| 79 |
|
if (isdigit(argv[i][3])) |
| 80 |
|
curbytes *= atoi(argv[i]+3); |
| 81 |
< |
if (curbytes < 0 || curbytes > MAXLINE) { |
| 81 |
> |
if (abs(curbytes) > MAXLINE) { |
| 82 |
|
fputs(argv[0], stderr); |
| 83 |
< |
fputs(": illegal input size\n", stderr); |
| 83 |
> |
fputs(": input size too big\n", stderr); |
| 84 |
|
exit(1); |
| 85 |
|
} |
| 86 |
+ |
if (curbytes) { |
| 87 |
+ |
curtab = ""; |
| 88 |
+ |
binout += (curbytes > 0); |
| 89 |
+ |
} |
| 90 |
|
break; |
| 91 |
|
case '\0': |
| 92 |
|
tabc[nfiles] = curtab; |
| 93 |
< |
bytsiz[nfiles] = curbytes; |
| 94 |
< |
input[nfiles++] = stdin; |
| 93 |
> |
input[nfiles] = stdin; |
| 94 |
> |
if (curbytes > 0) |
| 95 |
> |
SET_FILE_BINARY(input[nfiles]); |
| 96 |
> |
else |
| 97 |
> |
curbytes = -curbytes; |
| 98 |
> |
bytsiz[nfiles++] = curbytes; |
| 99 |
|
break; |
| 100 |
|
badopt:; |
| 101 |
|
default: |
| 105 |
|
} |
| 106 |
|
} else if (argv[i][0] == '!') { |
| 107 |
|
tabc[nfiles] = curtab; |
| 108 |
< |
bytsiz[nfiles] = curbytes; |
| 87 |
< |
if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) { |
| 108 |
> |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
| 109 |
|
fputs(argv[i], stderr); |
| 110 |
|
fputs(": cannot start command\n", stderr); |
| 111 |
|
exit(1); |
| 112 |
|
} |
| 113 |
+ |
if (curbytes > 0) |
| 114 |
+ |
SET_FILE_BINARY(input[nfiles]); |
| 115 |
+ |
else |
| 116 |
+ |
curbytes = -curbytes; |
| 117 |
+ |
bytsiz[nfiles++] = curbytes; |
| 118 |
|
} else { |
| 119 |
|
tabc[nfiles] = curtab; |
| 120 |
< |
bytsiz[nfiles] = curbytes; |
| 95 |
< |
if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) { |
| 120 |
> |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
| 121 |
|
fputs(argv[i], stderr); |
| 122 |
|
fputs(": cannot open file\n", stderr); |
| 123 |
|
exit(1); |
| 124 |
|
} |
| 125 |
+ |
if (curbytes > 0) |
| 126 |
+ |
SET_FILE_BINARY(input[nfiles]); |
| 127 |
+ |
else |
| 128 |
+ |
curbytes = -curbytes; |
| 129 |
+ |
bytsiz[nfiles++] = curbytes; |
| 130 |
|
} |
| 131 |
|
if (nfiles >= MAXFILE) { |
| 132 |
|
fputs(argv[0], stderr); |
| 134 |
|
exit(1); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
< |
puteol = 0; /* check for tab character */ |
| 137 |
> |
if (binout) /* binary output? */ |
| 138 |
> |
SET_FILE_BINARY(stdout); |
| 139 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 140 |
|
for (i = nfiles; i--; ) |
| 141 |
< |
if (isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
| 141 |
> |
flockfile(input[i]); |
| 142 |
> |
flockfile(stdout); |
| 143 |
> |
#endif |
| 144 |
> |
puteol = 0; /* any ASCII output at all? */ |
| 145 |
> |
for (i = nfiles; i--; ) |
| 146 |
> |
if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
| 147 |
|
puteol++; |
| 148 |
|
break; |
| 149 |
|
} |
| 150 |
< |
do { |
| 114 |
< |
running = 0; |
| 150 |
> |
do { /* main loop */ |
| 151 |
|
for (i = 0; i < nfiles; i++) { |
| 152 |
< |
if (bytsiz[i]) { /* binary file */ |
| 153 |
< |
if (fread(buf, bytsiz[i], 1, input[i]) == 1) { |
| 154 |
< |
if (i) |
| 119 |
< |
fputs(tabc[i], stdout); |
| 120 |
< |
fwrite(buf, bytsiz[i], 1, stdout); |
| 121 |
< |
running++; |
| 122 |
< |
} |
| 123 |
< |
} else if (fgets(buf, MAXLINE, input[i]) != NULL) { |
| 152 |
> |
if (bytsiz[i]) { /* binary/fixed width */ |
| 153 |
> |
if (getbinary(buf, bytsiz[i], 1, input[i]) < 1) |
| 154 |
> |
break; |
| 155 |
|
if (i) |
| 156 |
|
fputs(tabc[i], stdout); |
| 157 |
+ |
putbinary(buf, bytsiz[i], 1, stdout); |
| 158 |
+ |
} else { |
| 159 |
+ |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
| 160 |
+ |
break; |
| 161 |
+ |
if (i) |
| 162 |
+ |
fputs(tabc[i], stdout); |
| 163 |
|
buf[strlen(buf)-1] = '\0'; |
| 164 |
|
fputs(buf, stdout); |
| 128 |
– |
puteol++; |
| 129 |
– |
running++; |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
< |
if (running && puteol) |
| 167 |
> |
if (i < nfiles) |
| 168 |
> |
break; |
| 169 |
> |
if (puteol) |
| 170 |
|
putchar('\n'); |
| 171 |
< |
} while (running); |
| 172 |
< |
|
| 173 |
< |
exit(0); |
| 171 |
> |
if (unbuff) |
| 172 |
> |
fflush(stdout); |
| 173 |
> |
} while (--incnt); |
| 174 |
> |
return(0); |
| 175 |
|
} |