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]; |
33 |
|
int argc; |
34 |
|
char *argv[]; |
35 |
|
{ |
36 |
< |
register int i; |
36 |
> |
int unbuff = 0; |
37 |
> |
int binout = 0; |
38 |
> |
int i; |
39 |
|
char *curtab; |
40 |
|
int curbytes; |
41 |
< |
int running, puteol; |
41 |
> |
int puteol; |
42 |
|
|
43 |
|
curtab = "\t"; |
44 |
|
curbytes = 0; |
49 |
|
case 't': |
50 |
|
curtab = argv[i]+2; |
51 |
|
break; |
52 |
+ |
case 'u': |
53 |
+ |
unbuff = !unbuff; |
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; |
63 |
|
case 'd': |
64 |
|
curbytes = sizeof(double); |
65 |
|
break; |
66 |
< |
case 'w': |
66 |
> |
case 'i': |
67 |
|
curbytes = sizeof(int); |
68 |
|
break; |
69 |
+ |
case 'w': |
70 |
+ |
curbytes = 2; |
71 |
+ |
break; |
72 |
+ |
case 'b': |
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) { |
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; |
110 |
< |
bytsiz[nfiles] = curbytes; |
87 |
< |
if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) { |
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 (curbytes > 0) |
116 |
+ |
SET_FILE_BINARY(input[nfiles]); |
117 |
+ |
else |
118 |
+ |
curbytes = -curbytes; |
119 |
+ |
bytsiz[nfiles++] = curbytes; |
120 |
|
} else { |
121 |
|
tabc[nfiles] = curtab; |
122 |
< |
bytsiz[nfiles] = curbytes; |
95 |
< |
if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) { |
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 (curbytes > 0) |
128 |
+ |
SET_FILE_BINARY(input[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 tab character */ |
139 |
> |
if (binout) /* binary output? */ |
140 |
> |
SET_FILE_BINARY(stdout); |
141 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
142 |
|
for (i = nfiles; i--; ) |
143 |
< |
if (isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
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 |
< |
do { |
114 |
< |
running = 0; |
152 |
> |
while (--incnt) { /* 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) { |
156 |
< |
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) { |
155 |
> |
if (fread(buf, bytsiz[i], 1, input[i]) < 1) |
156 |
> |
break; |
157 |
|
if (i) |
158 |
|
fputs(tabc[i], stdout); |
159 |
+ |
fwrite(buf, bytsiz[i], 1, stdout); |
160 |
+ |
} else { |
161 |
+ |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
162 |
+ |
break; |
163 |
+ |
if (i) |
164 |
+ |
fputs(tabc[i], stdout); |
165 |
|
buf[strlen(buf)-1] = '\0'; |
166 |
|
fputs(buf, stdout); |
128 |
– |
puteol++; |
129 |
– |
running++; |
167 |
|
} |
168 |
|
} |
169 |
< |
if (running && puteol) |
169 |
> |
if (i < nfiles) |
170 |
> |
break; |
171 |
> |
if (puteol) |
172 |
|
putchar('\n'); |
173 |
< |
} while (running); |
174 |
< |
|
175 |
< |
exit(0); |
173 |
> |
if (unbuff) |
174 |
> |
fflush(stdout); |
175 |
> |
} |
176 |
> |
return(0); |
177 |
|
} |