diff options
author | Jim Meyering <jim@meyering.net> | 2000-05-20 22:06:38 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2000-05-20 22:06:38 +0000 |
commit | 24f2bd8c9f62409ef7d7c919c9a53f438ee991cb (patch) | |
tree | 0d30aa82a2c6c12ab2224c5ecffb9ded6a11caaa /src | |
parent | ac8180336348bee878584b013d71f84e5d047f21 (diff) | |
download | coreutils-24f2bd8c9f62409ef7d7c919c9a53f438ee991cb.tar.xz |
Arrange to call close_stdout upon exit. Don't close stdout explicitly.
Diffstat (limited to 'src')
-rw-r--r-- | src/cut.c | 5 | ||||
-rw-r--r-- | src/expand.c | 7 | ||||
-rw-r--r-- | src/fmt.c | 8 | ||||
-rw-r--r-- | src/fold.c | 7 | ||||
-rw-r--r-- | src/head.c | 6 | ||||
-rw-r--r-- | src/join.c | 5 | ||||
-rw-r--r-- | src/md5sum.c | 8 | ||||
-rw-r--r-- | src/nl.c | 9 | ||||
-rw-r--r-- | src/od.c | 8 | ||||
-rw-r--r-- | src/paste.c | 5 | ||||
-rw-r--r-- | src/pr.c | 5 | ||||
-rw-r--r-- | src/ptx.c | 6 | ||||
-rw-r--r-- | src/split.c | 5 | ||||
-rw-r--r-- | src/sum.c | 5 | ||||
-rw-r--r-- | src/tac.c | 7 | ||||
-rw-r--r-- | src/tr.c | 6 | ||||
-rw-r--r-- | src/tsort.c | 6 | ||||
-rw-r--r-- | src/unexpand.c | 5 | ||||
-rw-r--r-- | src/uniq.c | 9 | ||||
-rw-r--r-- | src/wc.c | 5 |
20 files changed, 78 insertions, 49 deletions
@@ -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); } diff --git a/src/expand.c b/src/expand.c index 315fd9ac3..c829f776e 100644 --- a/src/expand.c +++ b/src/expand.c @@ -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); } @@ -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); } diff --git a/src/fold.c b/src/fold.c index a9b25f662..14596daf7 100644 --- a/src/fold.c +++ b/src/fold.c @@ -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); } diff --git a/src/head.c b/src/head.c index 70f3b5598..44e0ab294 100644 --- a/src/head.c +++ b/src/head.c @@ -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); } diff --git a/src/join.c b/src/join.c index 5fd02c60f..fb2064d4d 100644 --- a/src/join.c +++ b/src/join.c @@ -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); } diff --git a/src/md5sum.c b/src/md5sum.c index afdb2406e..da994f18d 100644 --- a/src/md5sum.c +++ b/src/md5sum.c @@ -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")); @@ -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); } @@ -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); } diff --git a/src/paste.c b/src/paste.c index abd9be49d..781613715 100644 --- a/src/paste.c +++ b/src/paste.c @@ -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); } @@ -318,6 +318,7 @@ #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); @@ -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]); diff --git a/src/split.c b/src/split.c index 46ba9b3ca..c4823cc1c 100644 --- a/src/split.c +++ b/src/split.c @@ -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 = "-"; @@ -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) @@ -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); } @@ -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")); diff --git a/src/tsort.c b/src/tsort.c index dca29189b..b03e012a5 100644 --- a/src/tsort.c +++ b/src/tsort.c @@ -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")); diff --git a/src/unexpand.c b/src/unexpand.c index 02420fff8..e1485b1ad 100644 --- a/src/unexpand.c +++ b/src/unexpand.c @@ -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); } diff --git a/src/uniq.c b/src/uniq.c index 3639d9315..fa0fa0e5f 100644 --- a/src/uniq.c +++ b/src/uniq.c @@ -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; @@ -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; |