File sponge.c changed (mode: 100644) (index 242298d..ea343d0) |
... |
... |
int main (int argc, char **argv) { |
262 |
262 |
FILE *outfile, *tmpfile = 0; |
FILE *outfile, *tmpfile = 0; |
263 |
263 |
ssize_t i = 0; |
ssize_t i = 0; |
264 |
264 |
size_t mem_available = default_sponge_size(); |
size_t mem_available = default_sponge_size(); |
|
265 |
|
struct stat statbuf; |
265 |
266 |
|
|
266 |
267 |
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { |
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { |
267 |
268 |
usage(); |
usage(); |
|
... |
... |
int main (int argc, char **argv) { |
269 |
270 |
if (argc == 2) { |
if (argc == 2) { |
270 |
271 |
outname = argv[1]; |
outname = argv[1]; |
271 |
272 |
} |
} |
272 |
|
|
|
|
273 |
|
|
|
274 |
|
tmpfile = open_tmpfile(); |
273 |
275 |
bufstart = buf = malloc(bufsize); |
bufstart = buf = malloc(bufsize); |
274 |
276 |
if (!buf) { |
if (!buf) { |
275 |
277 |
perror("failed to allocate memory"); |
perror("failed to allocate memory"); |
|
... |
... |
int main (int argc, char **argv) { |
279 |
281 |
bufused = bufused+i; |
bufused = bufused+i; |
280 |
282 |
if (bufused == bufsize) { |
if (bufused == bufsize) { |
281 |
283 |
if ((bufsize*2) >= mem_available) { |
if ((bufsize*2) >= mem_available) { |
282 |
|
if (!tmpfile) { |
|
283 |
|
tmpfile=open_tmpfile(); |
|
284 |
|
} |
|
285 |
284 |
write_buff_tmp(bufstart, bufused, tmpfile); |
write_buff_tmp(bufstart, bufused, tmpfile); |
286 |
285 |
bufused = 0; |
bufused = 0; |
287 |
286 |
} |
} |
|
... |
... |
int main (int argc, char **argv) { |
300 |
299 |
perror("failed to read from stdin"); |
perror("failed to read from stdin"); |
301 |
300 |
exit(1); |
exit(1); |
302 |
301 |
} |
} |
303 |
|
if (tmpfile) { |
|
304 |
|
struct stat statbuf; |
|
305 |
302 |
|
|
306 |
303 |
/* write whatever we have in memory to tmpfile */ |
/* write whatever we have in memory to tmpfile */ |
307 |
304 |
if (bufused) |
if (bufused) |
|
... |
... |
int main (int argc, char **argv) { |
319 |
316 |
! S_ISLNK(statbuf.st_mode) |
! S_ISLNK(statbuf.st_mode) |
320 |
317 |
) || errno == ENOENT) && |
) || errno == ENOENT) && |
321 |
318 |
rename(tmpname, outname) == 0) { |
rename(tmpname, outname) == 0) { |
|
319 |
|
tmpname=NULL; |
322 |
320 |
/* Fix renamed file mode to match either |
/* Fix renamed file mode to match either |
323 |
321 |
* the old file mode, or the default file |
* the old file mode, or the default file |
324 |
322 |
* mode for a newly created file. */ |
* mode for a newly created file. */ |
|
... |
... |
int main (int argc, char **argv) { |
345 |
343 |
exit(1); |
exit(1); |
346 |
344 |
} |
} |
347 |
345 |
copy_tmpfile(tmpfile, outfile, bufstart, bufsize); |
copy_tmpfile(tmpfile, outfile, bufstart, bufsize); |
|
346 |
|
fclose(outfile); |
348 |
347 |
} |
} |
349 |
348 |
else { |
else { |
350 |
349 |
copy_tmpfile(tmpfile, stdout, bufstart, bufsize); |
copy_tmpfile(tmpfile, stdout, bufstart, bufsize); |
351 |
350 |
} |
} |
352 |
|
} |
|
353 |
|
else { |
|
354 |
|
if (outname) { |
|
355 |
|
outfile = fopen(outname, "w"); |
|
356 |
|
if (!outfile) { |
|
357 |
|
perror("error opening output file"); |
|
358 |
|
exit(1); |
|
359 |
|
} |
|
360 |
|
} |
|
361 |
|
else { |
|
362 |
|
outfile = stdout; |
|
363 |
|
} |
|
364 |
|
if (bufused) |
|
365 |
|
write_buff_out(bufstart, bufused, outfile); |
|
366 |
|
fclose(outfile); |
|
367 |
|
} |
|
368 |
351 |
|
|
369 |
352 |
return 0; |
return 0; |
370 |
353 |
} |
} |
File sponge.docbook changed (mode: 100644) (index 24c432a..ab2c42d) |
... |
... |
USA |
58 |
58 |
redirect, sponge soaks up all its input before |
redirect, sponge soaks up all its input before |
59 |
59 |
opening the output file. This allows constricting |
opening the output file. This allows constricting |
60 |
60 |
pipelines that read from and write to |
pipelines that read from and write to |
61 |
|
the same file. </para> |
|
|
61 |
|
the same file. It also creates the output file |
|
62 |
|
atomically by renaming a temp file into place.</para> |
62 |
63 |
<para>If no output file is specified, sponge outputs to |
<para>If no output file is specified, sponge outputs to |
63 |
64 |
stdout.</para> |
stdout.</para> |
64 |
65 |
|
|