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

index e3424edd52e75f1f293f1b75399f38184e8d2df7..ba8a707e168aed36b034bfc05cbca8e4938ba9a7 100644 (file)
@@ -1,8 +1,9 @@
 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().
+  * sponge, ifne: 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
 
diff --git a/ifne.c b/ifne.c
index 9af19fa0f905802af64b90b23fc1dd9963864a50..817b1c510c92e581233bc19dc3c920b6c2c17181 100644 (file)
--- a/ifne.c
+++ b/ifne.c
@@ -1,4 +1,3 @@
-
 /* 
  *
  * Copyright 2008 Javier Merino <cibervicho@gmail.com>
@@ -30,6 +29,7 @@ int main(int argc, char **argv) {
        int child_status;
        pid_t child_pid;
        char buf[BUFSIZ];
+       FILE *outf;
 
        if (argc < 2) {
                /* Noop */
@@ -68,11 +68,15 @@ int main(int argc, char **argv) {
                return EXIT_FAILURE;
        }
 
-       /* Parent: write in fds[1] our stdin */
+       /* Parent: write stdin to fds[1] */
        close(fds[0]);
-
+       outf = fdopen(fds[1], "w");
+       if (! outf) {
+               perror("fdopen");
+               exit(1);
+       }
        do {
-               if (write(fds[1], buf, r*sizeof(char)) == -1) {
+               if (fwrite(buf, r*sizeof(char), 1, outf) < 1) {
                        fprintf(stderr, "Write error to %s\n", argv[1]);
                        exit(EXIT_FAILURE);
                }
@@ -82,8 +86,8 @@ int main(int argc, char **argv) {
                perror("read");
                exit(EXIT_FAILURE);
        }
-       
-       close(fds[1]);
+       fclose(outf);
+
        if (waitpid(child_pid, &child_status, 0) != child_pid) {
                perror("waitpid");
                return EXIT_FAILURE;