bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Use O_BINARY to detect whether to pass "rb" to popen


From: Filipe Brandenburger
Subject: [PATCH 1/2] Use O_BINARY to detect whether to pass "rb" to popen
Date: Fri, 22 May 2015 08:50:04 -0700

Get rid of freadonly_mode and fwriteonly_mode which were set from a
define set by a configure AC_RUN_IFELSE check.

Instead, define FOPEN_BINARY and FOPEN_TEXT to expand to the "b" and "t"
modifiers to be used with fopen() and popen().  Only define them to the
modifiers if O_BINARY is defined to a non-zero value, which indicates
that the platform differentiates between opening files in text and
binary mode, otherwise define them to empty strings.

Remove the now unnecessary configure check for whether popen works with
binary mode.  That particular check required running a program built by
configure, which made it hard or impossible to cross-compile sharutils
without patching the sources.

Replace previously defined FOPEN_READ_BINARY and FOPEN_WRITE_BINARY with
a concatenation of "r" or "w" with FOPEN_BINARY for uniformity.

Tested that it still builds and that the testsuite of `make check`
passes. Checked that `make distcheck` works as expected.

Tested it on a Linux/glibc system and on Windows and cygwin.

Tested that cross-compiling works on the pristine sources from the dist
tarball without any modifications to the source tree.

Signed-off-by: Filipe Brandenburger <address@hidden>
---
 ChangeLog        | 14 ++++++++++++++
 configure.ac.git |  6 ------
 lib/system.h     | 14 +++++++++-----
 src/local.h.git  | 10 ----------
 src/shar.c       | 25 ++++++++++++-------------
 src/uudecode.c   |  2 +-
 src/uuencode.c   |  2 +-
 7 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 043c5abfa5ce..13bf58b448f0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2015-05-20  Filipe Brandenburger  <address@hidden>
+       Bruce Korb  <address@hidden>
+       Paul Eggert  <address@hidden>
+
+       * lib/system.h: Replace the FOPEN_{READ,WRITE}_BINARY defines with
+       more generic FOPEN_BINARY and FOPEN_TEXT similar to the ones used by
+       Emacs.
+       * src/local.h.git: Remove the no longer needed f{read,write}only_mode.
+       * src/shar.c: Use FOPEN_BINARY (and FOPEN_TEXT) for popen instead of
+       trying to derive whether popen takes a "b" modifier via Autoconf.
+       * src/uuencode.c: Use FOPEN_BINARY instead of FOPEN_READ_BINARY.
+       * src/uudecode.c: Use FOPEN_BINARY instead of FOPEN_WRITE_BINARY.
+       * configure.ac.git: Drop the check for popen with "rb" mode.
+
 2015-01-06  Eric Blake  <address@hidden>
 
        build: avoid error message failure on 64-bit hosts
diff --git a/configure.ac.git b/configure.ac.git
index 6ab67d9ed2e2..a669db093e67 100644
--- a/configure.ac.git
+++ b/configure.ac.git
@@ -94,12 +94,6 @@ AM_WITH_DMALLOC
   CATALOGS="$new_CATALOGS"
 fi]
 
-AC_RUN_IFELSE(
-  [AC_LANG_PROGRAM([], [dnl
-    FILE * fp = popen ("date", "rb");
-    exit (fp == NULL);])],
-  [AC_DEFINE([BINARY_MODE_POPEN], [1], [define if popen supports "rb" mode])]
-)
 # INVOKE_LIBOPTS_MACROS
 INVOKE_LIBOPTS_MACROS
 
diff --git a/lib/system.h b/lib/system.h
index 8274c2be9581..f22cafaab623 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -99,10 +99,14 @@ char *get_submitter (char *);
 # include <io.h> /* for setmode */
 #endif
 
-#if ! defined(O_BINARY) || (O_BINARY == 0)
-# define  FOPEN_READ_BINARY   "r"
-# define  FOPEN_WRITE_BINARY  "w"
+#ifndef O_BINARY
+# define O_BINARY 0
+#endif
+
+#if O_BINARY
+# define FOPEN_BINARY "b"
+# define FOPEN_TEXT "t"
 #else
-# define  FOPEN_READ_BINARY   "rb"
-# define  FOPEN_WRITE_BINARY  "wb"
+# define FOPEN_BINARY ""
+# define FOPEN_TEXT ""
 #endif
diff --git a/src/local.h.git b/src/local.h.git
index 8f234cbe5d6e..f778c09f773d 100644
--- a/src/local.h.git
+++ b/src/local.h.git
@@ -22,14 +22,4 @@
 
 INCLUDE-LIST
 
-#ifdef SHAR_C
-# ifdef BINARY_MODE_POPEN
-static char const freadonly_mode[]  = "rb";
-static char const fwriteonly_mode[] = "wb";
-# else
-static char const freadonly_mode[]  = "r";
-static char const fwriteonly_mode[] = "w";
-# endif
-#endif
-
 #endif /* SHARUTILS_LOCAL_H */
diff --git a/src/shar.c b/src/shar.c
index 5507b3b71962..439237ac18e2 100644
--- a/src/shar.c
+++ b/src/shar.c
@@ -1170,7 +1170,7 @@ file_needs_encoding (char const * fname)
      average file size, even reading the whole file (if it is text) would
      usually be faster than invoking 'file'.  */
 
-  infp = fopen (fname, freadonly_mode);
+  infp = fopen (fname, "r" FOPEN_BINARY);
 
   if (infp == NULL)
     {
@@ -1265,11 +1265,11 @@ encode_file_to_pipe (
     {
       sprintf (cmdline, cmpr_state->cmpr_cmd_fmt,
                cmpr_state->cmpr_level, q_local_name);
-      in_fp = popen (cmdline, freadonly_mode);
+      in_fp = popen (cmdline, "r" FOPEN_BINARY);
     }
   else
     {
-      in_fp = fopen (local_name, freadonly_mode);
+      in_fp = fopen (local_name, "r" FOPEN_BINARY);
       open_fmt = "fopen";
       open_txt = local_name;
     }
@@ -1277,7 +1277,7 @@ encode_file_to_pipe (
   if (in_fp == NULL)
     fserr (SHAR_EXIT_FAILED, open_fmt, open_txt);
 
-  out_fp = fdopen (out_fd, fwriteonly_mode);
+  out_fp = fdopen (out_fd, "w" FOPEN_BINARY);
 
   fprintf (out_fp, mode_fmt_z, restore_name);
 
@@ -1321,7 +1321,7 @@ open_encoded_file (char const * local_name, char const * 
q_local_name,
   close (pipex[1]);
 
   {
-    FILE * fp = fdopen (pipex[0], freadonly_mode);
+    FILE * fp = fdopen (pipex[0], "r" FOPEN_BINARY);
     if (fp == NULL)
       fserr (SHAR_EXIT_FAILED, "fdopen", _("pipe fd"));
     return fp;
@@ -1402,12 +1402,11 @@ open_encoded_file (char const * local_name,
       sprintf (p, uu_cmd_fmt, restore_name);
     }
 
-  /* Don't use freadonly_mode because it might be "rb", while we need
-     text-mode read here, because we will be reading pure text from
-     uuencode, and we want to drop any CR characters from the CRLF
+  /* We need text-mode read here, because we will be reading pure text
+     from uuencode, and we want to drop any CR characters from the CRLF
      line endings, when we write the result into the shar.  */
   {
-    FILE * in_fp = popen (cmdline, "r");
+    FILE * in_fp = popen (cmdline, "r" FOPEN_TEXT);
 
     if (in_fp == NULL)
       fserr (SHAR_EXIT_FAILED, "popen", cmdline);
@@ -1441,7 +1440,7 @@ open_shar_input (
       *file_type_p = _("text");
       *file_type_remote_p = SM_type_text;
 
-      infp = fopen (local_name, freadonly_mode);
+      infp = fopen (local_name, "r" FOPEN_BINARY);
       if (infp == NULL)
         fserr (SHAR_EXIT_FAILED, "fopen", local_name);
       *pipe_p = 0;
@@ -1822,7 +1821,7 @@ finish_sharing_file (const char * lname, const char * 
lname_q,
       echo_status ("test $? -ne 0", SM_restore_failed, NULL, rname, 0);
 
       if (   ! HAVE_OPT(NO_MD5_DIGEST)
-          && (fp = fopen (lname, freadonly_mode)) != NULL
+          && (fp = fopen (lname, "r" FOPEN_BINARY)) != NULL
          && md5_stream (fp, md5buffer) == 0)
        {
          /* Validate the transferred file using 'md5sum' command.  */
@@ -2050,7 +2049,7 @@ open_output (void)
   if (output_filename == NULL)
     parse_output_base_name (OPT_ARG(OUTPUT_PREFIX));
   sprintf (output_filename, OPT_ARG(OUTPUT_PREFIX), ++part_number);
-  output = fopen (output_filename, fwriteonly_mode);
+  output = fopen (output_filename, "w" FOPEN_BINARY);
 
   if (output == NULL)
     fserr (SHAR_EXIT_FAILED, _("Opening"), output_filename);
@@ -2152,7 +2151,7 @@ configure_shar (int * argc_p, char *** argv_p)
        * Redirect stderr to /dev/null in that case so that
        * the results are not cluttered with chatter.
        */
-      FILE * fp = freopen ("/dev/null", fwriteonly_mode, stderr);
+      FILE * fp = freopen ("/dev/null", "w" FOPEN_BINARY, stderr);
       if (fp != stderr)
         error (SHAR_EXIT_FAILED, errno,
                _("reopening stderr to /dev/null"));
diff --git a/src/uudecode.c b/src/uudecode.c
index 9213cc429b34..fa214d3d3a13 100644
--- a/src/uudecode.c
+++ b/src/uudecode.c
@@ -304,7 +304,7 @@ reopen_output (char const * outname, int mode)
     }
 
   {
-    FILE * fp = freopen (outname, FOPEN_WRITE_BINARY, stdout);
+    FILE * fp = freopen (outname, "w" FOPEN_BINARY, stdout);
     if (fp != stdout)
       fserr (UUDECODE_EXIT_NO_OUTPUT, "freopen", outname);
   }
diff --git a/src/uuencode.c b/src/uuencode.c
index f7265418dc0a..c6517985c51c 100644
--- a/src/uuencode.c
+++ b/src/uuencode.c
@@ -222,7 +222,7 @@ process_opts (int argc, char ** argv, int * mode)
         choke me - Must translate mode argument
 #endif
 
-       FILE * fp = freopen (*argv, FOPEN_READ_BINARY, stdin);
+       FILE * fp = freopen (*argv, "r" FOPEN_BINARY, stdin);
         input_name = *argv;
        if (fp != stdin)
           fserr (UUENCODE_EXIT_FAILURE, _("freopen of stdin"), input_name);
-- 
2.2.0.rc0.207.ga3a616c




reply via email to

[Prev in Thread] Current Thread [Next in Thread]