Arrange to call close_stdout upon exit. Don't close stdout explicitly.
authorJim Meyering <jim@meyering.net>
Sat, 20 May 2000 22:06:38 +0000 (22:06 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 20 May 2000 22:06:38 +0000 (22:06 +0000)
20 files changed:
src/cut.c
src/expand.c
src/fmt.c
src/fold.c
src/head.c
src/join.c
src/md5sum.c
src/nl.c
src/od.c
src/paste.c
src/pr.c
src/ptx.c
src/split.c
src/sum.c
src/tac.c
src/tr.c
src/tsort.c
src/unexpand.c
src/uniq.c
src/wc.c

index 02f1c6c..5bc0646 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -64,6 +64,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -698,6 +699,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   operating_mode = undefined_mode;
 
   /* By default, all non-delimited lines are printed.  */
@@ -798,8 +801,6 @@ main (int argc, char **argv)
       error (0, errno, "-");
       exit_status = 1;
     }
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 315fd9a..c829f77 100644 (file)
@@ -1,5 +1,5 @@
 /* expand - convert tabs to spaces
-   Copyright (C) 89, 91, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 89, 91, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -39,6 +39,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -339,6 +340,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   while ((c = getopt_long (argc, argv, "it:,0123456789", longopts, NULL)) != -1)
     {
       switch (c)
@@ -388,8 +391,6 @@ main (int argc, char **argv)
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 67aded8..49c6925 100644 (file)
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -1,5 +1,5 @@
 /* GNU fmt -- simple text formatter.
-   Copyright (C) 1994-1999 Free Software Foundation, Inc.
+   Copyright (C) 1994-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 #define word unused_word_type
 
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "xstrtol.h"
 
@@ -316,6 +317,8 @@ main (register int argc, register char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   crown = tagged = split = uniform = FALSE;
   max_width = WIDTH;
   prefix = "";
@@ -412,9 +415,6 @@ main (register int argc, register char **argv)
        }
     }
 
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
-
   exit (EXIT_SUCCESS);
 }
 
index a9b25f6..14596da 100644 (file)
@@ -1,5 +1,5 @@
 /* fold -- wrap each input line to fit in specified width.
-   Copyright (C) 91, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 91, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "xstrtol.h"
 
@@ -242,6 +243,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   break_spaces = count_bytes = have_read_stdin = 0;
 
   /* Turn any numeric options into -w options.  */
@@ -302,8 +305,6 @@ main (int argc, char **argv)
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 70f3b55..44e0ab2 100644 (file)
@@ -30,6 +30,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "xstrtol.h"
 #include "safe-read.h"
@@ -191,6 +192,7 @@ head_file (const char *filename, uintmax_t n_units, int count_lines)
       filename = _("standard input");
       if (print_headers)
        write_header (filename);
+      /* FIXME: use STDIN_FILENO, not `0' */
       return head (filename, 0, n_units, count_lines);
     }
   else
@@ -263,6 +265,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   have_read_stdin = 0;
 
   print_headers = 0;
@@ -383,8 +387,6 @@ main (int argc, char **argv)
 
   if (have_read_stdin && close (0) < 0)
     error (EXIT_FAILURE, errno, "-");
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 5fd02c6..fb2064d 100644 (file)
@@ -25,6 +25,7 @@
 #include <getopt.h>
 
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "hard-locale.h"
 #include "linebuffer.h"
@@ -737,6 +738,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
 #ifdef ENABLE_NLS
   hard_LC_COLLATE = hard_locale (LC_COLLATE);
 #endif
@@ -868,8 +871,6 @@ main (int argc, char **argv)
     error (EXIT_FAILURE, errno, "%s", names[1]);
   if ((fp1 == stdin || fp2 == stdin) && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (EXIT_SUCCESS);
 }
index afdb240..da994f1 100644 (file)
@@ -1,6 +1,6 @@
 /* Compute MD5 checksum of files or strings according to the definition
    of MD5 in RFC 1321 from April 1992.
-   Copyright (C) 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -29,6 +29,7 @@
 #include "md5.h"
 #include "getline.h"
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -472,6 +473,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
     switch (opt)
       {
@@ -625,9 +628,6 @@ verifying checksums"));
        }
     }
 
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
-
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, _("standard input"));
 
index 75ecbb7..26f2e47 100644 (file)
--- a/src/nl.c
+++ b/src/nl.c
@@ -1,5 +1,5 @@
 /* nl -- number lines of files
-   Copyright (C) 89, 92, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 89, 92, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@
 #include <getopt.h>
 
 #include "system.h"
+#include "closeout.h"
 
 #include <regex.h>
 
@@ -362,7 +363,7 @@ static enum section
 check_section (void)
 {
   size_t len = line_buf.length - 1;
-  
+
   if (len < 2 || memcmp (line_buf.buffer, section_del, 2))
     return Text;
   if (len == header_del_len
@@ -453,6 +454,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   have_read_stdin = 0;
 
   while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
@@ -604,8 +607,6 @@ main (int argc, char **argv)
       error (0, errno, "-");
       exit_status = 1;
     }
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index d5b6cf8..1cfbbb8 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -1,5 +1,5 @@
 /* od -- dump files in octal and other formats
-   Copyright (C) 92, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 92, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "xstrtol.h"
 
@@ -1584,6 +1585,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   err = 0;
 
   for (i = 0; i <= MAX_INTEGRAL_TYPE_SIZE; i++)
@@ -1928,8 +1931,5 @@ cleanup:;
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, _("standard input"));
 
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
-
   exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index abd9be4..7816137 100644 (file)
@@ -41,6 +41,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -430,6 +431,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   have_read_stdin = 0;
   serial_merge = 0;
   delims = default_delims;
@@ -474,7 +477,5 @@ main (int argc, char **argv)
     exit_status = paste_serial (argc - optind, &argv[optind]);
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 115b6a4..b703b80 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
 #include <sys/types.h>
 #include <time.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "xstrtol.h"
 
@@ -853,6 +854,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   n_files = 0;
   file_names = (argc > 1
                ? (char **) xmalloc ((argc - 1) * sizeof (char *))
@@ -1162,8 +1165,6 @@ main (int argc, char **argv)
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, _("standard input"));
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
   if (failed_opens > 0)
     exit (EXIT_FAILURE);
   exit (EXIT_SUCCESS);
index 20c3c67..75bc7ed 100644 (file)
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -1,5 +1,5 @@
 /* Permuted index for GNU, with keywords in their context.
-   Copyright (C) 1990, 1991, 1993, 1998-1999 Free Software Foundation, Inc.
+   Copyright (C) 1990, 1991, 1993, 1998-2000 Free Software Foundation, Inc.
    François Pinard <pinard@iro.umontreal.ca>, 1988.
 
    This program is free software; you can redistribute it and/or modify
@@ -24,6 +24,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "argmatch.h"
 #include "bumpalloc.h"
 #include "diacrit.h"
@@ -1942,6 +1943,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
 #if HAVE_SETCHRCLASS
   setchrclass (NULL);
 #endif
@@ -2111,6 +2114,7 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"),
 
       if (optind < argc)
        {
+         /* FIXME: don't fclose here? */
          fclose (stdout);
          if (fopen (argv[optind], "w") == NULL)
            error (EXIT_FAILURE, errno, "%s", argv[optind]);
index 46ba9b3..c4823cc 100644 (file)
@@ -1,5 +1,5 @@
 /* split.c -- split a file into pieces.
-   Copyright (C) 88, 91, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 88, 91, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@
 #include <sys/types.h>
 
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "safe-read.h"
 #include "xstrtol.h"
@@ -348,6 +349,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   /* Parse command line options.  */
 
   infile = "-";
index cadcee0..b012182 100644 (file)
--- a/src/sum.c
+++ b/src/sum.c
@@ -1,5 +1,5 @@
 /* sum -- checksum and count the blocks in a file
-   Copyright (C) 86, 89, 91, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 86, 89, 91, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@
 #include <sys/types.h>
 #include <getopt.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "safe-read.h"
 
@@ -213,6 +214,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   have_read_stdin = 0;
 
   while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
index 87b9ca7..0960404 100644 (file)
--- a/src/tac.c
+++ b/src/tac.c
@@ -1,5 +1,5 @@
 /* tac - concatenate and print files in reverse
-   Copyright (C) 1988-1991, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 1988-1991, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -41,6 +41,7 @@ tac -r -s '.\|
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 
 #include <regex.h>
 
@@ -615,6 +616,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   errors = 0;
   separator = "\n";
   sentinel_length = 1;
@@ -711,7 +714,5 @@ main (int argc, char **argv)
 
   if (have_read_stdin && close (0) < 0)
     error (EXIT_FAILURE, errno, "-");
-  if (ferror (stdout) || fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 21a82ca..7bf050b 100644 (file)
--- a/src/tr.c
+++ b/src/tr.c
@@ -26,6 +26,7 @@
 #include <getopt.h>
 
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "safe-read.h"
 #include "xstrtol.h"
@@ -1783,6 +1784,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   while ((c = getopt_long (argc, argv, "cdst", long_options, NULL)) != -1)
     {
       switch (c)
@@ -2006,9 +2009,6 @@ construct in string1 must be aligned with a corresponding construct\n\
        }
     }
 
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
-
   if (close (0) != 0)
     error (EXIT_FAILURE, errno, _("standard input"));
 
index dca2918..b03e012 100644 (file)
@@ -30,6 +30,7 @@
 #include <getopt.h>
 
 #include "system.h"
+#include "closeout.h"
 #include "long-options.h"
 #include "error.h"
 #include "readtokens.h"
@@ -552,6 +553,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   exit_status = 0;
 
   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
@@ -579,9 +582,6 @@ main (int argc, char **argv)
   else
     tsort ("-");
 
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
-
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, _("standard input"));
 
index 02420ff..e1485b1 100644 (file)
@@ -41,6 +41,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 
 #include "error.h"
 
@@ -407,6 +408,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   have_read_stdin = 0;
   exit_status = 0;
   convert_entire_line = 0;
@@ -473,7 +476,5 @@ main (int argc, char **argv)
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  if (fclose (stdout) == EOF)
-    error (EXIT_FAILURE, errno, _("write error"));
   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
index 3639d93..fa0fa0e 100644 (file)
@@ -1,5 +1,5 @@
 /* uniq -- remove duplicate lines from a sorted file
-   Copyright (C) 86, 91, 1995-1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 86, 91, 1995-1998, 1999, 2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 
 #include "system.h"
+#include "closeout.h"
 #include "linebuffer.h"
 #include "error.h"
 #include "xstrtol.h"
@@ -313,7 +314,9 @@ check_file (const char *infile, const char *outfile)
   if (ferror (istream) || fclose (istream) == EOF)
     error (EXIT_FAILURE, errno, _("error reading %s"), infile);
 
-  if (ferror (ostream) || fclose (ostream) == EOF)
+  /* Close ostream only if it's not stdout -- the latter is closed
+     via the atexit-invoked close_stdout.  */
+  if (ostream != stdout && (ferror (ostream) || fclose (ostream) == EOF))
     error (EXIT_FAILURE, errno, _("error writing %s"), outfile);
 
   free (lb1.buffer);
@@ -331,6 +334,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   skip_chars = 0;
   skip_fields = 0;
   check_chars = 0;
index ced5300..bf78e7a 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -1,5 +1,5 @@
 /* wc - print the number of bytes, words, and lines in files
-   Copyright (C) 85, 91, 1995-1999 Free Software Foundation, Inc.
+   Copyright (C) 85, 91, 1995-2000 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
+#include "closeout.h"
 #include "error.h"
 #include "human.h"
 #include "safe-read.h"
@@ -309,6 +310,8 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  atexit (close_stdout);
+
   exit_status = 0;
   posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
   print_lines = print_words = print_chars = print_linelength = 0;