13 |
|
#include "platform.h" |
14 |
|
#include "paths.h" |
15 |
|
|
16 |
< |
#define MAXFILE 512 /* maximum number of files */ |
16 |
> |
#define MAXLINE 262144 /* maximum input line */ |
17 |
|
|
18 |
< |
#define MAXLINE 65536 /* maximum input line */ |
18 |
> |
struct instream { /* structure to hold input stream info */ |
19 |
> |
FILE *input; /* input stream */ |
20 |
> |
int bytsiz; /* bytes/component if binary */ |
21 |
> |
char *tabc; /* data separation string */ |
22 |
> |
} *rifile = NULL; |
23 |
|
|
20 |
– |
FILE *input[MAXFILE]; |
21 |
– |
int bytsiz[MAXFILE]; |
22 |
– |
char *tabc[MAXFILE]; |
24 |
|
int nfiles = 0; |
25 |
|
|
26 |
|
char buf[MAXLINE]; |
36 |
|
int puteol; |
37 |
|
int i; |
38 |
|
|
39 |
+ |
rifile = (struct instream *)calloc(argc-1, sizeof(struct instream)); |
40 |
+ |
if (!rifile) { |
41 |
+ |
fputs(argv[0], stderr); |
42 |
+ |
fputs(": not enough memory\n", stderr); |
43 |
+ |
return(1); |
44 |
+ |
} |
45 |
|
for (i = 1; i < argc; i++) { |
46 |
|
if (argv[i][0] == '-') { |
47 |
|
switch (argv[i][1]) { |
96 |
|
} |
97 |
|
break; |
98 |
|
case '\0': |
99 |
< |
tabc[nfiles] = curtab; |
100 |
< |
input[nfiles] = stdin; |
99 |
> |
rifile[nfiles].tabc = curtab; |
100 |
> |
rifile[nfiles].input = stdin; |
101 |
|
if (curbytes > 0) |
102 |
< |
SET_FILE_BINARY(input[nfiles]); |
103 |
< |
bytsiz[nfiles++] = curbytes; |
102 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
103 |
> |
rifile[nfiles++].bytsiz = curbytes; |
104 |
|
break; |
105 |
|
badopt:; |
106 |
|
default: |
109 |
|
return(1); |
110 |
|
} |
111 |
|
} else if (argv[i][0] == '!') { |
112 |
< |
tabc[nfiles] = curtab; |
113 |
< |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
112 |
> |
rifile[nfiles].tabc = curtab; |
113 |
> |
if ((rifile[nfiles].input = popen(argv[i]+1, "r")) == NULL) { |
114 |
|
fputs(argv[i], stderr); |
115 |
|
fputs(": cannot start command\n", stderr); |
116 |
|
return(1); |
117 |
|
} |
118 |
|
if (curbytes > 0) |
119 |
< |
SET_FILE_BINARY(input[nfiles]); |
120 |
< |
bytsiz[nfiles++] = curbytes; |
119 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
120 |
> |
rifile[nfiles++].bytsiz = curbytes; |
121 |
|
} else { |
122 |
< |
tabc[nfiles] = curtab; |
123 |
< |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
122 |
> |
rifile[nfiles].tabc = curtab; |
123 |
> |
if ((rifile[nfiles].input = fopen(argv[i], "r")) == NULL) { |
124 |
|
fputs(argv[i], stderr); |
125 |
|
fputs(": cannot open file\n", stderr); |
126 |
|
return(1); |
127 |
|
} |
128 |
|
if (curbytes > 0) |
129 |
< |
SET_FILE_BINARY(input[nfiles]); |
130 |
< |
bytsiz[nfiles++] = curbytes; |
129 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
130 |
> |
rifile[nfiles++].bytsiz = curbytes; |
131 |
|
} |
125 |
– |
if (nfiles >= MAXFILE) { |
126 |
– |
fputs(argv[0], stderr); |
127 |
– |
fputs(": too many input streams\n", stderr); |
128 |
– |
return(1); |
129 |
– |
} |
132 |
|
} |
133 |
|
if (!nfiles) { |
134 |
|
fputs(argv[0], stderr); |
135 |
|
fputs(": no input streams\n", stderr); |
136 |
|
return(1); |
137 |
+ |
} /* reduce array to size we need */ |
138 |
+ |
rifile = (struct instream *)realloc(rifile, nfiles*sizeof(struct instream)); |
139 |
+ |
if (!rifile) { |
140 |
+ |
fputs(argv[0], stderr); |
141 |
+ |
fputs(": realloc() failed!\n", stderr); |
142 |
+ |
return(1); |
143 |
|
} |
144 |
|
if (binout) /* binary output? */ |
145 |
|
SET_FILE_BINARY(stdout); |
146 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
147 |
|
for (i = nfiles; i--; ) |
148 |
< |
flockfile(input[i]); |
148 |
> |
flockfile(rifile[i].input); |
149 |
|
flockfile(stdout); |
150 |
|
#endif |
151 |
|
puteol = 0; /* any ASCII output at all? */ |
152 |
|
for (i = nfiles; i--; ) |
153 |
< |
puteol += (bytsiz[i] <= 0); |
153 |
> |
puteol += (rifile[i].bytsiz <= 0); |
154 |
|
do { /* main loop */ |
155 |
|
for (i = 0; i < nfiles; i++) { |
156 |
< |
if (bytsiz[i] > 0) { /* binary input */ |
157 |
< |
if (getbinary(buf, bytsiz[i], 1, input[i]) < 1) |
156 |
> |
if (rifile[i].bytsiz > 0) { /* binary input */ |
157 |
> |
if (getbinary(buf, rifile[i].bytsiz, 1, rifile[i].input) < 1) |
158 |
|
break; |
159 |
< |
if (putbinary(buf, bytsiz[i], 1, stdout) != 1) |
159 |
> |
if (putbinary(buf, rifile[i].bytsiz, 1, stdout) != 1) |
160 |
|
break; |
161 |
< |
} else if (bytsiz[i] < 0) { /* multi-line input */ |
162 |
< |
int n = -bytsiz[i]; |
161 |
> |
} else if (rifile[i].bytsiz < 0) { /* multi-line input */ |
162 |
> |
int n = -rifile[i].bytsiz; |
163 |
|
while (n--) { |
164 |
< |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
164 |
> |
if (fgets(buf, MAXLINE, rifile[i].input) == NULL) |
165 |
|
break; |
166 |
< |
if ((i > 0) | (n < -bytsiz[i]-1)) |
167 |
< |
fputs(tabc[i], stdout); |
166 |
> |
if ((i > 0) | (n < -rifile[i].bytsiz-1)) |
167 |
> |
fputs(rifile[i].tabc, stdout); |
168 |
|
buf[strlen(buf)-1] = '\0'; |
169 |
|
if (fputs(buf, stdout) == EOF) |
170 |
|
break; |
172 |
|
if (n >= 0) /* fell short? */ |
173 |
|
break; |
174 |
|
} else { /* single-line input */ |
175 |
< |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
175 |
> |
if (fgets(buf, MAXLINE, rifile[i].input) == NULL) |
176 |
|
break; |
177 |
|
if (i) |
178 |
< |
fputs(tabc[i], stdout); |
178 |
> |
fputs(rifile[i].tabc, stdout); |
179 |
|
buf[strlen(buf)-1] = '\0'; |
180 |
|
if (fputs(buf, stdout) == EOF) |
181 |
|
break; |