]> err.no Git - moreutils/commitdiff
sponge: Ensure that suspending/resuming doesn't result in partial writes of the data...
authorJoey Hess <joey@kodama.kitenet.net>
Thu, 10 Apr 2008 18:05:25 +0000 (14:05 -0400)
committerJoey Hess <joey@kodama.kitenet.net>
Thu, 10 Apr 2008 18:05:25 +0000 (14:05 -0400)
debian/changelog
sponge.c

index 6e74f8512649193cc1b1d0399bbf9ffb4e9da535..e3424edd52e75f1f293f1b75399f38184e8d2df7 100644 (file)
@@ -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 <joeyh@debian.org>  Thu, 20 Mar 2008 12:56:42 -0400
 
index a7914eadccc88d4a3cb0b08cf2245337aac58036..3fd3ab4e0df8bb8190ccb6e8e569ac879478dcd7 100644 (file)
--- 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);
        }