Blob Blame History Raw
diff -up gzip-1.3.12/gzip.c.close-stdout gzip-1.3.12/gzip.c
--- gzip-1.3.12/gzip.c.close-stdout	2009-12-01 15:26:22.669387579 +0100
+++ gzip-1.3.12/gzip.c	2009-12-01 15:33:03.303387353 +0100
@@ -63,6 +63,7 @@ static char rcsid[] = "$Id: gzip.c,v 1.1
 #include <signal.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <stdio_ext.h>
 
 #include "tailor.h"
 #include "gzip.h"
@@ -382,6 +383,160 @@ local void progerror (string)
     exit_code = ERROR;
 }
 
+/* Assuming the stream STREAM is open for reading:
+   Return the number of bytes waiting in the input buffer of STREAM.
+
+   If this number is 0 and the stream is not currently writing,
+   fflush (STREAM) is known to be a no-op.
+
+   STREAM must not be wide-character oriented.  */
+local size_t freadahead (FILE *fp)
+{
+  if (fp->_IO_write_ptr > fp->_IO_write_base)
+    return 0;
+  return fp->_IO_read_end - fp->_IO_read_ptr;
+}
+
+
+/* Close STREAM.  Return 0 if successful, EOF (setting errno)
+   otherwise.  A failure might set errno to 0 if the error number
+   cannot be determined.
+
+   If a program writes *anything* to STREAM, that program should close
+   STREAM and make sure that it succeeds before exiting.  Otherwise,
+   suppose that you go to the extreme of checking the return status
+   of every function that does an explicit write to STREAM.  The last
+   printf can succeed in writing to the internal stream buffer, and yet
+   the fclose(STREAM) could still fail (due e.g., to a disk full error)
+   when it tries to write out that buffered data.  Thus, you would be
+   left with an incomplete output file and the offending program would
+   exit successfully.  Even calling fflush is not always sufficient,
+   since some file systems (NFS and CODA) buffer written/flushed data
+   until an actual close call.
+
+   Besides, it's wasteful to check the return value from every call
+   that writes to STREAM -- just let the internal stream state record
+   the failure.  That's what the ferror test is checking below.  */
+
+local int close_stream (FILE *stream)
+{
+  int some_pending = (__fpending (stream) != 0);
+  int prev_fail = (ferror (stream) != 0);
+  int fclose_fail = (fclose (stream) != 0);
+
+  /* Return an error indication if there was a previous failure or if
+     fclose failed, with one exception: ignore an fclose failure if
+     there was no previous error, no data remains to be flushed, and
+     fclose failed with EBADF.  That can happen when a program like cp
+     is invoked like this `cp a b >&-' (i.e., with standard output
+     closed) and doesn't generate any output (hence no previous error
+     and nothing to be flushed).  */
+
+  if (prev_fail || (fclose_fail && (some_pending || errno != EBADF)))
+    {
+      if (! fclose_fail)
+	errno = 0;
+      return EOF;
+    }
+
+  return 0;
+}
+
+
+/* Close standard output.  On error, issue a diagnostic and _exit
+   with status 'exit_failure'.
+
+   Also close standard error.  On error, _exit with status 'exit_failure'.
+
+   Since close_stdout is commonly registered via 'atexit', POSIX
+   and the C standard both say that it should not call 'exit',
+   because the behavior is undefined if 'exit' is called more than
+   once.  So it calls '_exit' instead of 'exit'.  If close_stdout
+   is registered via atexit before other functions are registered,
+   the other functions can act before this _exit is invoked.
+
+   Applications that use close_stdout should flush any streams
+   other than stdout and stderr before exiting, since the call to
+   _exit will bypass other buffer flushing.  Applications should
+   be flushing and closing other streams anyway, to check for I/O
+   errors.  Also, applications should not use tmpfile, since _exit
+   can bypass the removal of these files.
+
+   It's important to detect such failures and exit nonzero because many
+   tools (most notably `make' and other build-management systems) depend
+   on being able to detect failure in other tools via their exit status.  */
+
+local void close_stdout (void)
+{
+  if (close_stream (stdout) != 0)
+    {
+      fprintf(stderr, "%d: write error\n", errno);
+      _exit (ERROR);
+    }
+
+   if (close_stream (stderr) != 0)
+     _exit (ERROR);
+}
+
+/* Close standard input, rewinding any unused input if stdin is
+   seekable.  On error, issue a diagnostic and _exit with status
+   'exit_failure'.  Then call close_stdout.
+
+   Most programs can get by with close_stdout.  close_stdin is only
+   needed when a program wants to guarantee that partially read input
+   from seekable stdin is not consumed, for any subsequent clients.
+   For example, POSIX requires that these two commands behave alike:
+
+     (sed -ne 1q; cat) < file
+     tail -n 1 file
+
+   Since close_stdin is commonly registered via 'atexit', POSIX
+   and the C standard both say that it should not call 'exit',
+   because the behavior is undefined if 'exit' is called more than
+   once.  So it calls '_exit' instead of 'exit'.  If close_stdin
+   is registered via atexit before other functions are registered,
+   the other functions can act before this _exit is invoked.
+
+   Applications that use close_stdout should flush any streams other
+   than stdin, stdout, and stderr before exiting, since the call to
+   _exit will bypass other buffer flushing.  Applications should be
+   flushing and closing other streams anyway, to check for I/O errors.
+   Also, applications should not use tmpfile, since _exit can bypass
+   the removal of these files.
+
+   It's important to detect such failures and exit nonzero because many
+   tools (most notably `make' and other build-management systems) depend
+   on being able to detect failure in other tools via their exit status.  */
+
+local void close_stdin (void)
+{
+  int fail = 0;
+
+  /* There is no need to flush stdin if we can determine quickly that stdin's
+     input buffer is empty; in this case we know that if stdin is seekable,
+     fseeko (stdin, 0, SEEK_CUR) == lseek (0, 0, SEEK_CUR).  */
+  if (freadahead (stdin) > 0)
+    {
+      /* Only attempt flush if stdin is seekable, as fflush is entitled to
+	 fail on non-seekable streams.  */
+      if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
+	fail = 1;
+    }
+  if (close_stream (stdin) != 0)
+    fail = 1;
+  if (fail)
+    {
+      /* Report failure, but defer exit until after closing stdout,
+	 since the failure report should still be flushed.  */
+      fprintf(stderr, "%d: error closing file\n", errno);
+    }
+
+  close_stdout ();
+
+  if (fail)
+    _exit (ERROR);
+}
+
 /* ======================================================================== */
 int main (argc, argv)
     int argc;
@@ -396,6 +551,8 @@ int main (argc, argv)
     program_name = gzip_base_name (argv[0]);
     proglen = strlen (program_name);
 
+    atexit(close_stdin);
+
     /* Suppress .exe for MSDOS, OS/2 and VMS: */
     if (4 < proglen && strequ (program_name + proglen - 4, ".exe"))
       program_name[proglen - 4] = '\0';