]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
text-utils: verify writing to streams was successful
authorSami Kerola <kerolasa@iki.fi>
Wed, 4 Apr 2012 17:39:15 +0000 (19:39 +0200)
committerSami Kerola <kerolasa@iki.fi>
Wed, 4 Apr 2012 17:45:41 +0000 (19:45 +0200)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
text-utils/col.c
text-utils/colcrt.c
text-utils/colrm.c
text-utils/column.c
text-utils/hexdump.c
text-utils/more.c
text-utils/pg.c
text-utils/rev.c
text-utils/tailf.c
text-utils/ul.c

index baec1945ad02f5fcc32b6700b9ce7a30dc0b6ea0..dc9e9c8b2bcc573cd6cc4e9fe988cf60bde6421d 100644 (file)
@@ -56,6 +56,7 @@
 #include "xalloc.h"
 #include "widechar.h"
 #include "strutils.h"
+#include "closestream.h"
 
 #define        BS      '\b'            /* backspace */
 #define        TAB     '\t'            /* tab */
@@ -174,6 +175,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        max_bufd_lines = 128 * 2;
        compress_spaces = 1;            /* compress spaces into tabs */
@@ -388,8 +390,6 @@ int main(int argc, char **argv)
                /* missing a \n on the last line? */
                nblank_lines = 2;
        flush_blanks();
-       if (ferror(stdout) || fclose(stdout))
-               return EXIT_FAILURE;
        return ret;
 }
 
index d9806515efff1a2811181c952c670f19735a0353..3393d0cf3ada0f7900d95a506eb4758a399deee9 100644 (file)
@@ -47,6 +47,7 @@
 
 #include "widechar.h"
 #include "c.h"
+#include "closestream.h"
 
 int plus(wchar_t c, wchar_t d);
 void move(int l, int m);
@@ -93,6 +94,7 @@ int main(int argc, char **argv) {
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        /* Take care of lonely hyphen option. */
        for (i = 0; i < argc; i++)
@@ -141,8 +143,6 @@ int main(int argc, char **argv) {
                        fclose(f);
        } while (argc > 0);
        fflush(stdout);
-       if (ferror(stdout) || fclose(stdout))
-               return EXIT_FAILURE;
        return EXIT_SUCCESS;
 }
 
index d454643a0fb8ef6b26ae9f6c5ad04468da853248..910d933a294b2268f70fa18d311201beeb94220b 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <getopt.h>
+#include <unistd.h>
 
 #include "nls.h"
 #include "widechar.h"
 #include "strutils.h"
 #include "c.h"
+#include "closestream.h"
 
 /*
 COLRM removes unwanted columns from a file
@@ -162,6 +164,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        while ((opt =
                getopt_long(argc, argv, "bfhl:pxVH", longopts,
@@ -187,7 +190,5 @@ int main(int argc, char **argv)
                ;
 
        fflush(stdout);
-       if (ferror(stdout) || fclose(stdout))
-               return EXIT_FAILURE;
        return EXIT_SUCCESS;
 }
index 9d5befc13ca6297e4ea51dda0fe66cdeb06dba68..b210a93811518c681c543f34c2111b6de36ceea2 100644 (file)
@@ -55,6 +55,7 @@
 #include "c.h"
 #include "xalloc.h"
 #include "strutils.h"
+#include "closestream.h"
 
 #ifdef HAVE_WIDECHAR
 #define wcs_width(s) wcswidth(s,wcslen(s))
@@ -131,6 +132,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
                char *p;
@@ -203,9 +205,6 @@ int main(int argc, char **argv)
                free(list[i]);
        free(list);
 
-       if (ferror(stdout) || fclose(stdout))
-               eval += EXIT_FAILURE;
-
        if (eval == 0)
                return EXIT_SUCCESS;
        else
index 7edb053dbac8e4aedc78233a1a4dfe415d1dcdc0..6f533590031cde75b58518c54ba483fe45266af8 100644 (file)
@@ -43,6 +43,7 @@
 
 #include "nls.h"
 #include "c.h"
+#include "closestream.h"
 
 FS *fshead;                            /* head of format strings */
 int blocksize;                         /* data block size */
@@ -57,6 +58,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        if (!(p = strrchr(argv[0], 'o')) || strcmp(p, "od")) {
                newsyntax(argc, &argv);
index 32eb765e9b761ffe3ce252525e73fbede6469664..aa1e4cc5c985059ae3a6e93c091d853d2368533c 100644 (file)
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/wait.h>
-#include "strutils.h"
 
+#include "strutils.h"
 #include "nls.h"
 #include "xalloc.h"
 #include "widechar.h"
+#include "closestream.h"
 
 #define _REGEX_RE_COMP
 #include <regex.h>
@@ -90,7 +91,7 @@ void doclear(void);
 void cleareol(void);
 void clreos(void);
 void home(void);
-void error (char *mess);
+void more_error (char *mess);
 void do_shell (char *filename);
 int  colon (char *filename, int cmd, int nlines);
 int  expand (char **outbuf, char *inbuf);
@@ -330,6 +331,7 @@ int main(int argc, char **argv) {
     setlocale(LC_ALL, "");
     bindtextdomain(PACKAGE, LOCALEDIR);
     textdomain(PACKAGE);
+    atexit(close_stdout);
 
     nfiles = argc;
     fnames = argv;
@@ -1589,7 +1591,7 @@ void search(char buf[], FILE *file, register int n)
     context.chrctr = startline;
     lncount = 0;
     if ((s = re_comp (buf)) != 0)
-       error (s);
+       more_error (s);
     while (!feof (file)) {
        line3 = line2;
        line2 = line1;
@@ -1632,7 +1634,7 @@ void search(char buf[], FILE *file, register int n)
                    break;
                }
        } else if (rv == -1)
-           error (_("Regular expression botch"));
+           more_error (_("Regular expression botch"));
     }
     if (feof (file)) {
        if (!no_intty) {
@@ -1643,7 +1645,7 @@ void search(char buf[], FILE *file, register int n)
            putsout(_("\nPattern not found\n"));
            end_it (0);
        }
-       error (_("Pattern not found"));
+       more_error (_("Pattern not found"));
     }
 }
 
@@ -2025,7 +2027,7 @@ void ttyin (char buf[], register int nmax, char pchar) {
     *--sp = '\0';
     if (!eraseln) promptlen = maxlen;
     if (sp - buf >= nmax - 1)
-       error (_("Line too long"));
+       more_error (_("Line too long"));
 }
 
 /* return: 0 - unchanged, 1 - changed, -1 - overflow (unchanged) */
@@ -2060,7 +2062,7 @@ int expand (char **outbuf, char *inbuf) {
            break;
        case '!':
            if (!shellp)
-               error (_("No previous command to substitute for"));
+               more_error (_("No previous command to substitute for"));
            strcpy (outstr, shell_line);
            outstr += strlen (shell_line);
            changed++;
@@ -2089,7 +2091,7 @@ void show (char c) {
     promptlen++;
 }
 
-void error (char *mess)
+void more_error (char *mess)
 {
     if (clreol)
        cleareol ();
index b61b1504f6944e275fcd69860594486e724dda92..2b5144252aa2a4aad8cf319fc431f66a1042f884 100644 (file)
@@ -63,6 +63,7 @@
 #include "xalloc.h"
 #include "widechar.h"
 #include "writeall.h"
+#include "closestream.h"
 
 #define        READBUF         LINE_MAX        /* size of input buffer */
 #define CMDBUF         255             /* size of command buffer */
@@ -1594,6 +1595,7 @@ main(int argc, char **argv)
        setlocale(LC_MESSAGES, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        if (tcgetattr(1, &otio) == 0) {
                ontty = 1;
index 8a16668000ac9b810356e55c1a50cc87aa06c6a3..3c64c12d144d528d5cf6482627cefaefd60f6de0 100644 (file)
@@ -62,6 +62,7 @@
 #include "xalloc.h"
 #include "widechar.h"
 #include "c.h"
+#include "closestream.h"
 
 wchar_t *buf;
 
@@ -96,6 +97,7 @@ int main(int argc, char *argv[])
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        signal(SIGINT, sig_handler);
        signal(SIGTERM, sig_handler);
index c8f3e91db5336bf0dae5d9f860ae47abdb5beedb..10589d8bc1524250dacdb8fab9f917cd90f01520 100644 (file)
@@ -45,6 +45,7 @@
 #include "usleep.h"
 #include "strutils.h"
 #include "c.h"
+#include "closestream.h"
 
 #define DEFAULT_LINES  10
 
@@ -240,6 +241,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        static const struct option longopts[] = {
                { "lines",   required_argument, 0, 'n' },
index 6132166d0142b7cdf94317da5d040a89126b58a9..c15e48091e4b7f18f33d0230a639888d7f9e1436 100644 (file)
@@ -54,6 +54,7 @@
 #include "xalloc.h"
 #include "widechar.h"
 #include "c.h"
+#include "closestream.h"
 
 #ifdef HAVE_WIDECHAR
 /* Output an ASCII character as a wide character */
@@ -162,6 +163,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       atexit(close_stdout);
 
        signal(SIGINT, sig_handler);
        signal(SIGTERM, sig_handler);
@@ -231,9 +233,6 @@ int main(int argc, char **argv)
                        filter(f);
                        fclose(f);
                }
-       if (ferror(stdout) || fclose(stdout))
-               return EXIT_FAILURE;
-
        free(obuf);
        return EXIT_SUCCESS;
 }