]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(close_stdout_wrapper): New, kludgey, function and file-scoped global.
authorJim Meyering <jim@meyering.net>
Mon, 26 Aug 2002 07:23:37 +0000 (07:23 +0000)
committerJim Meyering <jim@meyering.net>
Mon, 26 Aug 2002 07:23:37 +0000 (07:23 +0000)
(main): Register it with atexit.
Close STDOUT_FILENO, to avoid a problem when writing to
/dev/audio on at least Solaris 2.7 and 2.8 systems.

src/cat.c

index 72ec1e62c0c7a4611c812b0b7e1c200aa397589c..e78b4bde9285403bf9f27bfa249d234bd9a6478f 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -482,6 +482,17 @@ cat (
     }
 }
 
+/* This is gross, but necessary, because of the way close_stdout
+   works and because this program closes STDOUT_FILENO directly.  */
+static void (*closeout_func) (void) = close_stdout;
+
+static void
+close_stdout_wrapper (void)
+{
+  if (closeout_func)
+    (*closeout_func) ();
+}
+
 int
 main (int argc, char **argv)
 {
@@ -554,7 +565,9 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
-  atexit (close_stdout);
+  /* Arrange to close stdout if we exit via the
+     case_GETOPT_HELP_CHAR or case_GETOPT_VERSION_CHAR code.  */
+  atexit (close_stdout_wrapper);
 
   /* Parse command line options.  */
 
@@ -641,6 +654,9 @@ main (int argc, char **argv)
        }
     }
 
+  /* Don't close stdout on exit from here on.  */
+  closeout_func = NULL;
+
   /* Get device, i-node number, and optimal blocksize of output.  */
 
   if (fstat (STDOUT_FILENO, &stat_buf) < 0)
@@ -833,7 +849,10 @@ main (int argc, char **argv)
   while (++argind < argc);
 
   if (have_read_stdin && close (STDIN_FILENO) < 0)
-    error (EXIT_FAILURE, errno, "-");
+    error (EXIT_FAILURE, errno, _("closing standard input"));
+
+  if (close (STDOUT_FILENO) < 0)
+    error (EXIT_FAILURE, errno, _("closing standard output"));
 
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }