gzip: fix bug with -l output to pipes
[debian/gzip] / gzip.c
diff --git a/gzip.c b/gzip.c
index d66530a70bdce1d19a2687658e9ef0b9f19a1e18..3b8de4d9af28fc3d559b99c1f4021e4e38e77113 100644 (file)
--- a/gzip.c
+++ b/gzip.c
@@ -664,14 +664,16 @@ int main (int argc, char **argv)
     } else {  /* Standard input */
         treat_stdin();
     }
-    if (list && !quiet && file_count > 1) {
-        do_list(-1, -1); /* print totals */
-    }
-    if (to_stdout
-        && ((synchronous
-             && (fdatasync (STDOUT_FILENO) != 0 && errno != EINVAL))
-            || close (STDOUT_FILENO) != 0)
-        && errno != EBADF)
+    if (list)
+      {
+        /* Output any totals, and check for output errors.  */
+        if (!quiet && 1 < file_count)
+          do_list (-1, -1);
+        if (fflush (stdout) != 0)
+          write_error ();
+      }
+    if (to_stdout && synchronous && fdatasync (STDOUT_FILENO) != 0
+        && errno != EINVAL && errno != EBADF)
       write_error ();
     do_exit(exit_code);
     return exit_code; /* just to avoid lint warning */