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