96 |
|
int append = 0; |
97 |
|
long outcnt = 0; |
98 |
|
int bininp = 0; |
99 |
+ |
int nstdoutcomp = 0; |
100 |
|
int curterm = '\n'; |
101 |
|
int curncomp = 1; |
102 |
|
int curbytes = 0; |
103 |
|
int curflags = 0; |
104 |
|
const char *curfmt = "ascii"; |
105 |
< |
int i; |
105 |
> |
short outndx[MAXFILE]; |
106 |
> |
int i; |
107 |
|
|
108 |
+ |
memset(outndx, 0, sizeof(outndx)); |
109 |
|
for (i = 1; i < argc; i++) { |
110 |
|
if (argv[i][0] == '-') { |
111 |
|
switch (argv[i][1]) { |
184 |
|
} |
185 |
|
bininp += (curbytes > 0); |
186 |
|
break; |
187 |
< |
case '\0': |
188 |
< |
needres |= (curflags & DORESOLU); |
189 |
< |
termc[nfiles] = curterm; |
190 |
< |
hdrflags[nfiles] = curflags; |
187 |
> |
case '\0': /* stdout */ |
188 |
> |
if (!nstdoutcomp) { /* first use? */ |
189 |
> |
needres |= (curflags & DORESOLU); |
190 |
> |
hdrflags[nfiles] = curflags; |
191 |
> |
} |
192 |
|
output[nfiles] = stdout; |
193 |
|
if (curbytes > 0) |
194 |
|
SET_FILE_BINARY(output[nfiles]); |
195 |
+ |
termc[nfiles] = curterm; |
196 |
|
format[nfiles] = curfmt; |
197 |
< |
ncomp[nfiles] = curncomp; |
198 |
< |
bytsiz[nfiles++] = curbytes; |
197 |
> |
nstdoutcomp += |
198 |
> |
ncomp[nfiles] = curncomp; |
199 |
> |
bytsiz[nfiles] = curbytes; |
200 |
> |
outndx[nfiles++] = i; |
201 |
|
break; |
202 |
|
badopt:; |
203 |
|
default: |
205 |
|
fputs(": bad option\n", stderr); |
206 |
|
return(1); |
207 |
|
} |
208 |
+ |
} else if (argv[i][0] == '.' && !argv[i][1]) { |
209 |
+ |
output[nfiles] = NULL; /* discard data */ |
210 |
+ |
termc[nfiles] = curterm; |
211 |
+ |
format[nfiles] = curfmt; |
212 |
+ |
ncomp[nfiles] = curncomp; |
213 |
+ |
bytsiz[nfiles] = curbytes; |
214 |
+ |
outndx[nfiles++] = i; |
215 |
|
} else if (argv[i][0] == '!') { |
216 |
|
needres |= (curflags & DORESOLU); |
203 |
– |
termc[nfiles] = curterm; |
217 |
|
hdrflags[nfiles] = curflags; |
218 |
+ |
termc[nfiles] = curterm; |
219 |
|
if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) { |
220 |
|
fputs(argv[i], stderr); |
221 |
|
fputs(": cannot start command\n", stderr); |
225 |
|
SET_FILE_BINARY(output[nfiles]); |
226 |
|
format[nfiles] = curfmt; |
227 |
|
ncomp[nfiles] = curncomp; |
228 |
< |
bytsiz[nfiles++] = curbytes; |
228 |
> |
bytsiz[nfiles] = curbytes; |
229 |
> |
outndx[nfiles++] = i; |
230 |
|
} else { |
231 |
+ |
int j = nfiles; |
232 |
+ |
while (j--) /* check duplicates */ |
233 |
+ |
if (!strcmp(argv[i], argv[outndx[j]])) { |
234 |
+ |
fputs(argv[0], stderr); |
235 |
+ |
fputs(": duplicate output: ", stderr); |
236 |
+ |
fputs(argv[i], stderr); |
237 |
+ |
fputc('\n', stderr); |
238 |
+ |
return(1); |
239 |
+ |
} |
240 |
|
if (append & (curflags != 0)) { |
241 |
|
fputs(argv[0], stderr); |
242 |
|
fputs(": -a option incompatible with -oh and -oH\n", |
244 |
|
return(1); |
245 |
|
} |
246 |
|
needres |= (curflags & DORESOLU); |
223 |
– |
termc[nfiles] = curterm; |
247 |
|
hdrflags[nfiles] = curflags; |
248 |
+ |
termc[nfiles] = curterm; |
249 |
|
if (!append & !force && access(argv[i], F_OK) == 0) { |
250 |
|
fputs(argv[i], stderr); |
251 |
|
fputs(": file exists -- use -f to overwrite\n", |
262 |
|
SET_FILE_BINARY(output[nfiles]); |
263 |
|
format[nfiles] = curfmt; |
264 |
|
ncomp[nfiles] = curncomp; |
265 |
< |
bytsiz[nfiles++] = curbytes; |
265 |
> |
bytsiz[nfiles] = curbytes; |
266 |
> |
outndx[nfiles++] = i; |
267 |
|
} |
268 |
|
if (nfiles >= MAXFILE) { |
269 |
|
fputs(argv[0], stderr); |
281 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
282 |
|
flockfile(stdin); |
283 |
|
for (i = nfiles; i--; ) |
284 |
< |
flockfile(output[i]); |
284 |
> |
if (output[i] != NULL) |
285 |
> |
ftrylockfile(output[i]); |
286 |
|
#endif |
287 |
|
/* load/copy header */ |
288 |
|
if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) { |
289 |
|
fputs(argv[0], stderr); |
290 |
< |
fputs(": cannot get header from standard input\n", |
290 |
> |
fputs(": cannot read header from standard input\n", |
291 |
|
stderr); |
292 |
|
return(1); |
293 |
|
} |
294 |
|
/* handle resolution string */ |
295 |
|
if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) { |
296 |
|
fputs(argv[0], stderr); |
297 |
< |
fputs(": cannot get resolution string from standard input\n", |
272 |
< |
stderr); |
297 |
> |
fputs(": bad resolution string on standard input\n", stderr); |
298 |
|
return(1); |
299 |
|
} |
300 |
|
if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
316 |
|
if (!(inpflags & DOHEADER)) |
317 |
|
newheader("RADIANCE", output[i]); |
318 |
|
printargs(argc, argv, output[i]); |
319 |
< |
fprintf(output[i], "NCOMP=%d\n", ncomp[i]); |
319 |
> |
fprintf(output[i], "NCOMP=%d\n", output[i]==stdout ? |
320 |
> |
nstdoutcomp : ncomp[i]); |
321 |
|
if (format[i] != NULL) { |
322 |
|
extern const char BIGEND[]; |
323 |
< |
if ((format[i][0] == 'f') | |
298 |
< |
(format[i][0] == 'd')) { |
323 |
> |
if (bytsiz[i] > 1) { |
324 |
|
fputs(BIGEND, output[i]); |
325 |
|
fputs(nativebigendian() ^ swapped ? |
326 |
|
"1\n" : "0\n", output[i]); |
338 |
|
if (getbinary(buf, bytsiz[i], ncomp[i], |
339 |
|
stdin) < ncomp[i]) |
340 |
|
break; |
341 |
< |
if (putbinary(buf, bytsiz[i], ncomp[i], |
341 |
> |
if (output[i] != NULL && |
342 |
> |
putbinary(buf, bytsiz[i], ncomp[i], |
343 |
|
output[i]) < ncomp[i]) |
344 |
|
break; |
345 |
|
} else if (ncomp[i] > 1) { /* N-field output */ |
347 |
|
while (n--) { |
348 |
|
if (!scanOK(termc[i])) |
349 |
|
break; |
350 |
< |
if (fputs(buf, output[i]) == EOF) |
350 |
> |
if (output[i] != NULL && |
351 |
> |
fputs(buf, output[i]) == EOF) |
352 |
|
break; |
353 |
|
} |
354 |
|
if (n >= 0) /* fell short? */ |
355 |
|
break; |
356 |
< |
if (termc[i] != '\n') /* add EOL if none */ |
356 |
> |
if ((output[i] != NULL) & /* add EOL if none */ |
357 |
> |
(termc[i] != '\n')) |
358 |
|
fputc('\n', output[i]); |
359 |
|
} else { /* 1-field output */ |
360 |
|
if (!scanOK(termc[i])) |
361 |
|
break; |
362 |
< |
if (fputs(buf, output[i]) == EOF) |
363 |
< |
break; |
364 |
< |
if (termc[i] != '\n') /* add EOL if none */ |
365 |
< |
fputc('\n', output[i]); |
362 |
> |
if (output[i] != NULL) { |
363 |
> |
if (fputs(buf, output[i]) == EOF) |
364 |
> |
break; |
365 |
> |
if (termc[i] != '\n') /* add EOL? */ |
366 |
> |
fputc('\n', output[i]); |
367 |
> |
} |
368 |
|
} |
369 |
|
/* skip input EOL? */ |
370 |
|
if (!bininp && termc[nfiles-1] != '\n') { |