use xfreopen in place of unchecked freopen
authorJim Meyering <meyering@redhat.com>
Sun, 12 Oct 2008 12:50:02 +0000 (14:50 +0200)
committerJim Meyering <meyering@redhat.com>
Mon, 10 Nov 2008 07:11:59 +0000 (08:11 +0100)
* bootstrap.conf (modules): Add xfreopen.
* src/cat.c (main): Include "xfreopen.h".  Use xfreopen.
* src/cksum.c (cksum): Likewise.
* src/head.c (head_file, main): Likewise.
* src/md5sum.c (digest_file): Likewise.
* src/od.c (open_next_file): Likewise.
* src/split.c (type_undef): Likewise.
* src/sum.c (bsd_sum_file, sysv_sum_file): Likewise.
* src/tac.c (tac_file, main): Likewise.
* src/tail.c (tail_file, main): Likewise.
* src/tee.c (tee_files): Likewise.
* src/tr.c (main): Likewise.
* src/wc.c (wc_file): Likewise.
* po/POTFILES.in: Add lib/xfreopen.c

14 files changed:
bootstrap.conf
po/POTFILES.in
src/cat.c
src/cksum.c
src/head.c
src/md5sum.c
src/od.c
src/split.c
src/sum.c
src/tac.c
src/tail.c
src/tee.c
src/tr.c
src/wc.c

index 338dada..aae307c 100644 (file)
@@ -99,7 +99,9 @@ gnulib_modules="
        vc-list-files
        verify version-etc-fsf
        wcwidth winsz-ioctl winsz-termios write-any-file
-       xalloc xgetcwd xgethostname
+       xalloc
+       xfreopen
+       xgetcwd xgethostname
        xmemcoll xnanosleep
        xprintf
        xprintf-posix
index c8e97d6..8a6e541 100644 (file)
@@ -24,6 +24,7 @@ lib/unicodeio.c
 lib/userspec.c
 lib/version-etc.c
 lib/xalloc-die.c
+lib/xfreopen.c
 lib/xfts.c
 lib/xmemcoll.c
 lib/xmemxfrm.c
index 9c1996a..543e5cf 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -39,6 +39,7 @@
 #include "full-write.h"
 #include "quote.h"
 #include "safe-read.h"
+#include "xfreopen.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "cat"
@@ -664,7 +665,7 @@ main (int argc, char **argv)
     {
       file_open_mode |= O_BINARY;
       if (O_BINARY && ! isatty (STDOUT_FILENO))
-       freopen (NULL, "wb", stdout);
+       xfreopen (NULL, "wb", stdout);
     }
 
   /* Check if any of the input files are the same as the output file.  */
@@ -684,7 +685,7 @@ main (int argc, char **argv)
          have_read_stdin = true;
          input_desc = STDIN_FILENO;
          if ((file_open_mode & O_BINARY) && ! isatty (STDIN_FILENO))
-           freopen (NULL, "rb", stdin);
+           xfreopen (NULL, "rb", stdin);
        }
       else
        {
index 8bbed37..2893d30 100644 (file)
@@ -43,6 +43,7 @@
 #include <sys/types.h>
 #include <stdint.h>
 #include "system.h"
+#include "xfreopen.h"
 
 #ifdef CRCTAB
 
@@ -192,7 +193,7 @@ cksum (const char *file, bool print_name)
       fp = stdin;
       have_read_stdin = true;
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
     }
   else
     {
index 0ef912a..7f90d4b 100644 (file)
@@ -36,6 +36,7 @@
 #include "full-read.h"
 #include "quote.h"
 #include "safe-read.h"
+#include "xfreopen.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -838,7 +839,7 @@ head_file (const char *filename, uintmax_t n_units, bool count_lines,
       fd = STDIN_FILENO;
       filename = _("standard input");
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
     }
   else
     {
@@ -1051,7 +1052,7 @@ main (int argc, char **argv)
               : default_file_list);
 
   if (O_BINARY && ! isatty (STDOUT_FILENO))
-    freopen (NULL, "wb", stdout);
+    xfreopen (NULL, "wb", stdout);
 
   for (i = 0; file_list[i]; ++i)
     ok &= head_file (file_list[i], n_units, count_lines, elide_from_end);
index 238c02e..d31a780 100644 (file)
@@ -37,6 +37,7 @@
 #endif
 #include "error.h"
 #include "stdio--.h"
+#include "xfreopen.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #if HASH_ALGO_MD5
@@ -391,7 +392,7 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result)
          if (*binary < 0)
            *binary = ! isatty (STDIN_FILENO);
          if (*binary)
-           freopen (NULL, "rb", stdin);
+           xfreopen (NULL, "rb", stdin);
        }
     }
   else
index ed3b50a..2dcb398 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -25,6 +25,7 @@
 #include "system.h"
 #include "error.h"
 #include "quote.h"
+#include "xfreopen.h"
 #include "xprintf.h"
 #include "xstrtol.h"
 
@@ -864,7 +865,7 @@ open_next_file (void)
          in_stream = stdin;
          have_read_stdin = true;
          if (O_BINARY && ! isatty (STDIN_FILENO))
-           freopen (NULL, "rb", stdin);
+           xfreopen (NULL, "rb", stdin);
        }
       else
        {
index 85687c4..be182ef 100644 (file)
@@ -34,6 +34,7 @@
 #include "full-write.h"
 #include "quote.h"
 #include "safe-read.h"
+#include "xfreopen.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -544,7 +545,7 @@ main (int argc, char **argv)
 
   /* Binary I/O is safer when bytecounts are used.  */
   if (O_BINARY && ! isatty (STDIN_FILENO))
-    freopen (NULL, "rb", stdin);
+    xfreopen (NULL, "rb", stdin);
 
   /* No output file is open now.  */
   output_desc = -1;
index 314656f..4cb8f31 100644 (file)
--- a/src/sum.c
+++ b/src/sum.c
@@ -28,6 +28,7 @@
 #include "error.h"
 #include "human.h"
 #include "safe-read.h"
+#include "xfreopen.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "sum"
@@ -97,7 +98,7 @@ bsd_sum_file (const char *file, int print_name)
       fp = stdin;
       have_read_stdin = true;
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
     }
   else
     {
@@ -165,7 +166,7 @@ sysv_sum_file (const char *file, int print_name)
       fd = STDIN_FILENO;
       have_read_stdin = true;
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
     }
   else
     {
index c83986f..be8f3ab 100644 (file)
--- a/src/tac.c
+++ b/src/tac.c
@@ -48,6 +48,7 @@ tac -r -s '.\|
 #include "quotearg.h"
 #include "safe-read.h"
 #include "stdlib--.h"
+#include "xfreopen.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "tac"
@@ -533,7 +534,7 @@ tac_file (const char *filename)
       fd = STDIN_FILENO;
       filename = _("standard input");
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
     }
   else
     {
@@ -647,7 +648,7 @@ main (int argc, char **argv)
          : default_file_list);
 
   if (O_BINARY && ! isatty (STDOUT_FILENO))
-    freopen (NULL, "wb", stdout);
+    xfreopen (NULL, "wb", stdout);
 
   {
     size_t i;
index 43fd6d4..6ad6d43 100644 (file)
@@ -40,6 +40,7 @@
 #include "quote.h"
 #include "safe-read.h"
 #include "stat-time.h"
+#include "xfreopen.h"
 #include "xnanosleep.h"
 #include "xstrtol.h"
 #include "xstrtod.h"
@@ -1273,7 +1274,7 @@ tail_file (struct File_spec *f, uintmax_t n_units)
       have_read_stdin = true;
       fd = STDIN_FILENO;
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
     }
   else
     fd = open (f->name, O_RDONLY | O_BINARY);
@@ -1683,7 +1684,7 @@ main (int argc, char **argv)
     print_headers = true;
 
   if (O_BINARY && ! isatty (STDOUT_FILENO))
-    freopen (NULL, "wb", stdout);
+    xfreopen (NULL, "wb", stdout);
 
   for (i = 0; i < n_files; i++)
     ok &= tail_file (&F[i], n_units);
index 4e46aab..2d13f6d 100644 (file)
--- a/src/tee.c
+++ b/src/tee.c
@@ -24,6 +24,7 @@
 #include "system.h"
 #include "error.h"
 #include "stdio--.h"
+#include "xfreopen.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "tee"
@@ -152,9 +153,9 @@ tee_files (int nfiles, const char **files)
     files[i] = files[i - 1];
 
   if (O_BINARY && ! isatty (STDIN_FILENO))
-    freopen (NULL, "rb", stdin);
+    xfreopen (NULL, "rb", stdin);
   if (O_BINARY && ! isatty (STDOUT_FILENO))
-    freopen (NULL, "wb", stdout);
+    xfreopen (NULL, "wb", stdout);
 
   /* In the array of NFILES + 1 descriptors, make
      the first one correspond to standard output.   */
index 0f94eef..f4b5317 100644 (file)
--- a/src/tr.c
+++ b/src/tr.c
@@ -27,6 +27,7 @@
 #include "error.h"
 #include "quote.h"
 #include "safe-read.h"
+#include "xfreopen.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -1750,9 +1751,9 @@ main (int argc, char **argv)
      non-printable characters, or characters which are stripped away
      by text-mode reads (like CR and ^Z).  */
   if (O_BINARY && ! isatty (STDIN_FILENO))
-    freopen (NULL, "rb", stdin);
+    xfreopen (NULL, "rb", stdin);
   if (O_BINARY && ! isatty (STDOUT_FILENO))
-    freopen (NULL, "wb", stdout);
+    xfreopen (NULL, "wb", stdout);
 
   if (squeeze_repeats && non_option_args == 1)
     {
index 0bb1929..280d7ac 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -32,6 +32,7 @@
 #include "quotearg.h"
 #include "readtokens0.h"
 #include "safe-read.h"
+#include "xfreopen.h"
 
 #if !defined iswspace && !HAVE_ISWSPACE
 # define iswspace(wc) \
@@ -486,7 +487,7 @@ wc_file (char const *file, struct fstatus *fstatus)
     {
       have_read_stdin = true;
       if (O_BINARY && ! isatty (STDIN_FILENO))
-       freopen (NULL, "rb", stdin);
+       xfreopen (NULL, "rb", stdin);
       return wc (STDIN_FILENO, file, fstatus);
     }
   else