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