From: Joey Hess Date: Thu, 10 Apr 2008 18:05:25 +0000 (-0400) Subject: sponge: Ensure that suspending/resuming doesn't result in partial writes of the data... X-Git-Tag: 0.29~23 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71349b5ae83878f4e59fc6517976998150856933;p=moreutils sponge: Ensure that suspending/resuming doesn't result in partial writes of the data, by using fwrite() rather than write(). --- diff --git a/debian/changelog b/debian/changelog index 6e74f85..e3424ed 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,8 @@ moreutils (0.29) UNRELEASED; urgency=low * Add ifne, contributed by Javier Merino. + * sponge: Ensure that suspending/resuming doesn't result in partial writes + of the data, by using fwrite() rather than write(). -- Joey Hess Thu, 20 Mar 2008 12:56:42 -0400 diff --git a/sponge.c b/sponge.c index a7914ea..3fd3ab4 100644 --- a/sponge.c +++ b/sponge.c @@ -39,7 +39,7 @@ int main(int argc, char **argv) { size_t bufsize = 8192; size_t bufused = 0; ssize_t i = 0; - int outfd; + FILE *outf; if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { usage(); @@ -69,25 +69,23 @@ int main(int argc, char **argv) { } if (argc == 2) { - outfd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666); - if (outfd == -1) { + outf = fopen(argv[1], "w"); + if (! outf) { fprintf(stderr, "Can't open %s: %s\n", argv[1], strerror(errno)); exit(1); } } else { - outfd = 1; + outf = stdout; } - i = write(outfd, bufstart, bufused); - if (i == -1) { - perror("write"); + if (fwrite(bufstart, bufused, 1, outf) < 1) { + perror("fwrite"); exit(1); } - i = close(outfd); - if (i == -1) { - perror("close"); + if (fclose(outf) != 0) { + perror("fclose"); exit(1); }