13 |
|
#include <math.h> |
14 |
|
#include "platform.h" |
15 |
|
|
16 |
< |
#define MAXCOL 2048 /* maximum number of columns */ |
16 |
> |
#define MAXCOL 8192 /* maximum number of columns */ |
17 |
|
|
18 |
|
#define ADD 0 /* add numbers */ |
19 |
|
#define MULT 1 /* multiply numbers */ |
22 |
|
|
23 |
|
double init_val[] = {0., 1., -1e12, 1e12}; /* initial values */ |
24 |
|
|
25 |
+ |
long incnt = 0; /* limit number of input records? */ |
26 |
+ |
long outcnt = 0; /* limit number of output records? */ |
27 |
+ |
|
28 |
|
int func = ADD; /* default function */ |
29 |
|
double power = 0.0; /* power for sum */ |
30 |
|
int mean = 0; /* compute mean */ |
33 |
|
int bocols = 0; /* produce binary output columns */ |
34 |
|
int tabc = '\t'; /* default separator */ |
35 |
|
int subtotal = 0; /* produce subtotals? */ |
36 |
+ |
int nrecsout = 0; /* number of records produced */ |
37 |
|
|
38 |
|
static int execute(char *fname); |
39 |
|
|
76 |
|
break; |
77 |
|
case 'i': |
78 |
|
switch (argv[a][2]) { |
79 |
+ |
case 'n': |
80 |
+ |
incnt = atol(argv[++a]); |
81 |
+ |
break; |
82 |
|
case 'a': |
83 |
|
nbicols = 0; |
84 |
|
break; |
112 |
|
break; |
113 |
|
case 'o': |
114 |
|
switch (argv[a][2]) { |
115 |
+ |
case 'n': |
116 |
+ |
outcnt = atol(argv[++a]); |
117 |
+ |
break; |
118 |
|
case 'a': |
119 |
|
bocols = 0; |
120 |
|
break; |
153 |
|
if (a >= argc) |
154 |
|
status = execute(NULL) == -1 ? 1 : status; |
155 |
|
else |
156 |
< |
for ( ; a < argc; a++) |
156 |
> |
for ( ; a < argc && (outcnt <= 0 || nrecsout < outcnt); a++) |
157 |
|
status = execute(argv[a]) == -1 ? 2 : status; |
158 |
|
exit(status); |
159 |
|
} |
223 |
|
return; |
224 |
|
} |
225 |
|
/* ASCII output */ |
226 |
< |
while (n-- > 0) |
227 |
< |
fprintf(fp, "%.9g%c", *field++, tabc); |
226 |
> |
while (n-- > 0) { |
227 |
> |
fprintf(fp, "%.9g", *field++); |
228 |
> |
if (n) fputc(tabc, fp); |
229 |
> |
} |
230 |
|
fputc('\n', fp); |
231 |
|
} |
232 |
|
|
253 |
|
} |
254 |
|
if (nbicols) |
255 |
|
SET_FILE_BINARY(fp); |
256 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
257 |
+ |
flockfile(fp); |
258 |
+ |
#endif |
259 |
+ |
|
260 |
|
ltotal = 0; |
261 |
|
while (!feof(fp)) { |
262 |
|
if (ltotal == 0) { /* initialize */ |
271 |
|
} |
272 |
|
ncol = 0; |
273 |
|
for (nlin = 0; (count <= 0 || nlin < count) && |
274 |
+ |
(incnt <= 0 || nlin < incnt) && |
275 |
|
(nread = getrecord(inpval, fp)) > 0; |
276 |
|
nlin++) { |
277 |
|
/* compute */ |
322 |
|
result[n] = rsign[n] * exp(result[n]); |
323 |
|
} |
324 |
|
putrecord(result, ncol, stdout); |
325 |
+ |
++nrecsout; |
326 |
+ |
if (outcnt > 0 && nrecsout >= outcnt) |
327 |
+ |
break; |
328 |
|
if (!subtotal) |
329 |
|
ltotal = 0; |
330 |
|
} |