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 |
+ |
long incnt = 0; |
36 |
|
int unbuff = 0; |
37 |
< |
int i; |
38 |
< |
char *curtab; |
39 |
< |
int curbytes; |
37 |
> |
int binout = 0; |
38 |
> |
char *curtab = "\t"; |
39 |
> |
int curbytes = 0; |
40 |
|
int puteol; |
41 |
+ |
int i; |
42 |
|
|
40 |
– |
curtab = "\t"; |
41 |
– |
curbytes = 0; |
43 |
|
nfiles = 0; |
44 |
|
for (i = 1; i < argc; i++) { |
45 |
|
if (argv[i][0] == '-') { |
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; |
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) |
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; |
98 |
– |
bytsiz[nfiles] = curbytes; |
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 (bytsiz[nfiles]) |
113 |
> |
if (curbytes > 0) |
114 |
|
SET_FILE_BINARY(input[nfiles]); |
115 |
< |
++nfiles; |
115 |
> |
else |
116 |
> |
curbytes = -curbytes; |
117 |
> |
bytsiz[nfiles++] = curbytes; |
118 |
|
} else { |
119 |
|
tabc[nfiles] = curtab; |
109 |
– |
bytsiz[nfiles] = curbytes; |
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 (bytsiz[nfiles]) |
125 |
> |
if (curbytes > 0) |
126 |
|
SET_FILE_BINARY(input[nfiles]); |
127 |
< |
++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 ASCII output */ |
137 |
> |
if (binout) /* binary output? */ |
138 |
> |
SET_FILE_BINARY(stdout); |
139 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
140 |
|
for (i = nfiles; i--; ) |
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 |
< |
for ( ; ; ) { /* main loop */ |
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) |
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 |
< |
fwrite(buf, bytsiz[i], 1, stdout); |
157 |
> |
putbinary(buf, bytsiz[i], 1, stdout); |
158 |
|
} else { |
159 |
|
if (fgets(buf, MAXLINE, input[i]) == NULL) |
160 |
|
break; |
170 |
|
putchar('\n'); |
171 |
|
if (unbuff) |
172 |
|
fflush(stdout); |
173 |
< |
} |
173 |
> |
} while (--incnt); |
174 |
|
return(0); |
175 |
|
} |