]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: use xsetmode, not xfreopen
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Feb 2017 23:58:08 +0000 (15:58 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Feb 2017 23:59:16 +0000 (15:59 -0800)
This fixes a bug noted by Eric Blake.  Code was using xfreopen to
change files to binary mode, but this fails for stdout when in
append mode.  Such code should use xsetmode instead.  This affects
only the port on platforms like MS-Windows which distiguish text
from binary I/O.
* bootstrap.conf (gnulib_modules):
Remove xfreopen and add xsetmode.  Sort.
* src/base64.c (main):
* src/cat.c (main):
* src/cksum.c (cksum):
* src/head.c (head_file, main):
* src/md5sum.c (digest_file):
* src/od.c (open_next_file):
* src/split.c (main):
* src/sum.c (bsd_sum_file, sysv_sum_file):
* src/tac.c (tac_file, main):
* src/tail.c (tail_file):
* src/tee.c (tee_files):
* src/tr.c (main):
* src/wc.c (wc_file): Use xsetmode, not xfreopen.

14 files changed:
bootstrap.conf
src/base64.c
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 acec6f08cc9788b4248a2a4d4bcd468271a0aac5..a1e3523537d49fdfb28703ecefe6c870fc1e3779 100644 (file)
@@ -35,8 +35,8 @@ gnulib_modules="
   assert
   autobuild
   backupfile
-  base64
   base32
+  base64
   buffer-lcm
   c-strcase
   c-strtod
@@ -217,10 +217,10 @@ gnulib_modules="
   sigaction
   smack
   ssize_t
-  statat
   stat-macros
   stat-size
   stat-time
+  statat
   stdbool
   stdlib-safer
   stpcpy
@@ -269,7 +269,7 @@ gnulib_modules="
   winsz-termios
   write-any-file
   xalloc
-  xfreopen
+  xdectoint
   xfts
   xgetcwd
   xgetgroups
@@ -279,7 +279,7 @@ gnulib_modules="
   xprintf
   xprintf-posix
   xreadlink
-  xdectoint
+  xsetmode
   xstrtod
   xstrtoimax
   xstrtol
index 5deaec5b67d935ddba2efd2a146b36024311a77e..d5d75dcc39051dc4337c557e07e9d72e7dde57e4 100644 (file)
@@ -31,7 +31,7 @@
 #include "quote.h"
 #include "xstrtol.h"
 #include "xdectoint.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 #define AUTHORS proper_name ("Simon Josefsson")
 
@@ -320,8 +320,7 @@ main (int argc, char **argv)
 
   if (STREQ (infile, "-"))
     {
-      if (O_BINARY)
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
       input_fh = stdin;
     }
   else
index 001408576c4c17a156c1e8761ed2d9c96aa3d0cf..fba721f288e530708f9a9265d746af625d6e3fa7 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -39,7 +39,7 @@
 #include "fadvise.h"
 #include "full-write.h"
 #include "safe-read.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "cat"
@@ -645,8 +645,7 @@ main (int argc, char **argv)
   if (! (number || show_ends || squeeze_blank))
     {
       file_open_mode |= O_BINARY;
-      if (O_BINARY && ! isatty (STDOUT_FILENO))
-        xfreopen (NULL, "wb", stdout);
+      xsetmode (STDOUT_FILENO, O_BINARY);
     }
 
   /* Check if any of the input files are the same as the output file.  */
@@ -665,8 +664,8 @@ main (int argc, char **argv)
         {
           have_read_stdin = true;
           input_desc = STDIN_FILENO;
-          if ((file_open_mode & O_BINARY) && ! isatty (STDIN_FILENO))
-            xfreopen (NULL, "rb", stdin);
+          if (file_open_mode & O_BINARY)
+            xsetmode (STDIN_FILENO, O_BINARY);
         }
       else
         {
index 0785e727d4eaaa9d310c8eb7eadbd15705fa63c0..65702a3176a654f4bb39bd6220ea678d3b45213e 100644 (file)
@@ -44,7 +44,7 @@
 #include <stdint.h>
 #include "system.h"
 #include "fadvise.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 #ifdef CRCTAB
 
@@ -194,8 +194,7 @@ cksum (const char *file, bool print_name)
     {
       fp = stdin;
       have_read_stdin = true;
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
     }
   else
     {
index 7639ab97407b0643831606da0b47607ecfabd75b..49c942fea35c5553ef1ff5a7591899d85d277961 100644 (file)
@@ -37,8 +37,8 @@
 #include "quote.h"
 #include "safe-read.h"
 #include "stat-size.h"
-#include "xfreopen.h"
 #include "xdectoint.h"
+#include "xsetmode.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "head"
@@ -878,8 +878,7 @@ head_file (const char *filename, uintmax_t n_units, bool count_lines,
       have_read_stdin = true;
       fd = STDIN_FILENO;
       filename = _("standard input");
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
     }
   else
     {
@@ -1083,8 +1082,7 @@ main (int argc, char **argv)
                ? (char const *const *) &argv[optind]
                : default_file_list);
 
-  if (O_BINARY && ! isatty (STDOUT_FILENO))
-    xfreopen (NULL, "wb", stdout);
+  xsetmode (STDOUT_FILENO, O_BINARY);
 
   for (i = 0; file_list[i]; ++i)
     ok &= head_file (file_list[i], n_units, count_lines, elide_from_end);
index ffcf74302c226885a4a65a6f2422b48fbb21566a..19dac0861c7846e13289af8b59b926da278c0dc7 100644 (file)
@@ -46,7 +46,7 @@
 #include "error.h"
 #include "fadvise.h"
 #include "stdio--.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #if HASH_ALGO_MD5
@@ -598,7 +598,7 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result,
           if (*binary < 0)
             *binary = ! isatty (STDIN_FILENO);
           if (*binary)
-            xfreopen (NULL, "rb", stdin);
+            xsetmode (STDIN_FILENO, O_BINARY);
         }
     }
   else
index 04736bf8f22577700e5164577857d6a28e85720f..0da85dae593fa652c7a4c7fda8b0b96e59d229ac 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -29,8 +29,8 @@
 #include "ftoastr.h"
 #include "quote.h"
 #include "stat-size.h"
-#include "xfreopen.h"
 #include "xprintf.h"
+#include "xsetmode.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
@@ -914,8 +914,7 @@ open_next_file (void)
           input_filename = _("standard input");
           in_stream = stdin;
           have_read_stdin = true;
-          if (O_BINARY && ! isatty (STDIN_FILENO))
-            xfreopen (NULL, "rb", stdin);
+          xsetmode (STDIN_FILENO, O_BINARY);
         }
       else
         {
index 316387b1a13f805224ae905fc29fd46ede6b7a8a..8bed1d38a070fc8558952b00f8a69e73528e9e83 100644 (file)
@@ -38,8 +38,8 @@
 #include "quote.h"
 #include "safe-read.h"
 #include "sig2str.h"
-#include "xfreopen.h"
 #include "xdectoint.h"
+#include "xsetmode.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
@@ -1553,8 +1553,7 @@ main (int argc, char **argv)
          quoteaf (infile));
 
   /* Binary I/O is safer when byte counts are used.  */
-  if (O_BINARY && ! isatty (STDIN_FILENO))
-    xfreopen (NULL, "rb", stdin);
+  xsetmode (STDIN_FILENO, O_BINARY);
 
   /* Get the optimal block size of input device and make a buffer.  */
 
index 91af738dd1037ec89a77ab8549b7835bd2f2ea53..7a13abe01dab28a75bde58c91bf4938ed3f33073 100644 (file)
--- a/src/sum.c
+++ b/src/sum.c
@@ -29,7 +29,7 @@
 #include "fadvise.h"
 #include "human.h"
 #include "safe-read.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "sum"
@@ -98,8 +98,7 @@ bsd_sum_file (const char *file, int print_name)
     {
       fp = stdin;
       have_read_stdin = true;
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
     }
   else
     {
@@ -168,8 +167,7 @@ sysv_sum_file (const char *file, int print_name)
     {
       fd = STDIN_FILENO;
       have_read_stdin = true;
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
     }
   else
     {
index 5c1b3e8a2ec08477d25ae3f7f40edd4a431d2949..41c4f998abb736e155065b1e5957e09e3f4021c5 100644 (file)
--- a/src/tac.c
+++ b/src/tac.c
@@ -48,7 +48,7 @@ tac -r -s '.\|
 #include "filenamecat.h"
 #include "safe-read.h"
 #include "stdlib--.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "tac"
@@ -572,8 +572,7 @@ tac_file (const char *filename)
       have_read_stdin = true;
       fd = STDIN_FILENO;
       filename = _("standard input");
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
     }
   else
     {
@@ -688,8 +687,7 @@ main (int argc, char **argv)
           ? (char const *const *) &argv[optind]
           : default_file_list);
 
-  if (O_BINARY && ! isatty (STDOUT_FILENO))
-    xfreopen (NULL, "wb", stdout);
+  xsetmode (STDOUT_FILENO, O_BINARY);
 
   {
     size_t i;
index b24757f3d7e715bc94dd5fcf5ac4cc933babf450..dbd2104586b19ce552b06def3f4f649b9fb78283 100644 (file)
@@ -43,9 +43,9 @@
 #include "safe-read.h"
 #include "stat-size.h"
 #include "stat-time.h"
-#include "xfreopen.h"
 #include "xnanosleep.h"
 #include "xdectoint.h"
+#include "xsetmode.h"
 #include "xstrtol.h"
 #include "xstrtod.h"
 
@@ -1894,8 +1894,7 @@ tail_file (struct File_spec *f, uintmax_t n_units)
     {
       have_read_stdin = true;
       fd = STDIN_FILENO;
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
     }
   else
     fd = open (f->name, O_RDONLY | O_BINARY);
@@ -2324,8 +2323,7 @@ main (int argc, char **argv)
       || (header_mode == multiple_files && n_files > 1))
     print_headers = true;
 
-  if (O_BINARY && ! isatty (STDOUT_FILENO))
-    xfreopen (NULL, "wb", stdout);
+  xsetmode (STDOUT_FILENO, O_BINARY);
 
   for (i = 0; i < n_files; i++)
     ok &= tail_file (&F[i], n_units);
index 74477cb7e3fd8cb57f052b2ac6670b1439ce71c8..5f04bfc86243911be2fb22c7c28447ae6fb0f993 100644 (file)
--- a/src/tee.c
+++ b/src/tee.c
@@ -27,7 +27,7 @@
 #include "error.h"
 #include "fadvise.h"
 #include "stdio--.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "tee"
@@ -194,11 +194,8 @@ tee_files (int nfiles, char **files)
      ? (append ? "ab" : "wb")
      : (append ? "a" : "w"));
 
-  if (O_BINARY && ! isatty (STDIN_FILENO))
-    xfreopen (NULL, "rb", stdin);
-  if (O_BINARY && ! isatty (STDOUT_FILENO))
-    xfreopen (NULL, "wb", stdout);
-
+  xsetmode (STDIN_FILENO, O_BINARY);
+  xsetmode (STDOUT_FILENO, O_BINARY);
   fadvise (stdin, FADVISE_SEQUENTIAL);
 
   /* Set up FILES[0 .. NFILES] and DESCRIPTORS[0 .. NFILES].
index bb86a89399901e571b15bda6004c05c52f875f0d..724297c9b4d58f1dd98c8d82fa40689040bb136c 100644 (file)
--- a/src/tr.c
+++ b/src/tr.c
@@ -29,7 +29,7 @@
 #include "fadvise.h"
 #include "quote.h"
 #include "safe-read.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
@@ -1786,11 +1786,8 @@ main (int argc, char **argv)
   /* Use binary I/O, since 'tr' is sometimes used to transliterate
      non-printable characters, or characters which are stripped away
      by text-mode reads (like CR and ^Z).  */
-  if (O_BINARY && ! isatty (STDIN_FILENO))
-    xfreopen (NULL, "rb", stdin);
-  if (O_BINARY && ! isatty (STDOUT_FILENO))
-    xfreopen (NULL, "wb", stdout);
-
+  xsetmode (STDIN_FILENO, O_BINARY);
+  xsetmode (STDOUT_FILENO, O_BINARY);
   fadvise (stdin, FADVISE_SEQUENTIAL);
 
   if (squeeze_repeats && non_option_args == 1)
index 40fce685dc422dc7718d39de99c152beeb103bf4..a587b2ca4eae6ec05b125abb61c47646bd8a27cb 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -36,7 +36,7 @@
 #include "readtokens0.h"
 #include "safe-read.h"
 #include "stat-size.h"
-#include "xfreopen.h"
+#include "xsetmode.h"
 
 #if !defined iswspace && !HAVE_ISWSPACE
 # define iswspace(wc) \
@@ -556,8 +556,7 @@ wc_file (char const *file, struct fstatus *fstatus)
   if (! file || STREQ (file, "-"))
     {
       have_read_stdin = true;
-      if (O_BINARY && ! isatty (STDIN_FILENO))
-        xfreopen (NULL, "rb", stdin);
+      xsetmode (STDIN_FILENO, O_BINARY);
       return wc (STDIN_FILENO, file, fstatus, -1);
     }
   else