]> git.eshelyaron.com Git - emacs.git/commitdiff
Use binary-io module, O_BINARY, and "b" flag.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 14 Jul 2014 19:23:18 +0000 (12:23 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 14 Jul 2014 19:23:18 +0000 (12:23 -0700)
* admin/merge-gnulib (GNULIB_MODULES): Add binary-io.  It was already
present implicitly; this just makes the dependence explicit.
* lib-src/etags.c, lib-src/hexl.c, lib-src/make-docfile.c:
Include binary-io.h instead of fcntl.h and/or io.h.
(main): Use set_binary_mode or SET_BINARY
in place of handcrafted code.
* lib-src/etags.c (main) [DOS_NT]:
* lib-src/movemail.c (main) [WINDOWSNT]:
Don't mess with _fmode.
* lib-src/etags.c (main, process_file_name, analyse_regex):
Use fopen/popen's "b" flag instead.
* lib-src/movemail.c (main, popmail): Use open/lk_open/mkostemp's O_BINARY
instead.
* src/callproc.c (create_temp_file): Use mkostemp's O_BINARY flag.
* src/emacs.c [MSDOS]:
* src/emacs.c (main) [DOS_NT]: Don't mess with _fmode.
(main) [MSDOS]: Use SET_BINARY instead of setmode.
* src/minibuf.c: Include binary-io.h instead of fcntl.h.
(read_minibuf_noninteractive):
Use set_binary_mode instead of handcrafted code.
Don't call emacs_set_tty if emacs_get_tty failed.
* src/sysdep.c, src/systty.h (emacs_get_tty): Return int, not void.
* src/sysdep.c (emacs_open, emacs_pipe): Use O_BINARY.
* src/w32.c (pipe2): Adjust eassert to include O_BINARY.

Fixes: debbugs:18006
14 files changed:
admin/ChangeLog
admin/merge-gnulib
lib-src/ChangeLog
lib-src/etags.c
lib-src/hexl.c
lib-src/make-docfile.c
lib-src/movemail.c
src/ChangeLog
src/callproc.c
src/emacs.c
src/minibuf.c
src/sysdep.c
src/systty.h
src/w32.c

index a20e93d38c2378a5d9eff6da70e657592a015168..90d394ca9bdda339ca9c7db4f9667b49770b1226 100644 (file)
@@ -1,3 +1,9 @@
+2014-07-14  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Use binary-io module, O_BINARY, and "b" flag (Bug#18006).
+       * merge-gnulib (GNULIB_MODULES): Add binary-io.  It was already
+       present implicitly; this just makes the dependence explicit.
+
 2014-06-30  Glenn Morris  <rgm@gnu.org>
 
        * update_autogen: Find loaddefs targets rather than
index 5df2020f3aac9c5743dc47ed8a88227b79f947b5..a11b6e06d27fdb0340617c73fb8ac5441f0b851a 100755 (executable)
@@ -26,7 +26,7 @@
 GNULIB_URL=git://git.savannah.gnu.org/gnulib.git
 
 GNULIB_MODULES='
-  alloca-opt byteswap c-ctype c-strcase
+  alloca-opt binary-io byteswap c-ctype c-strcase
   careadlinkat close-stream count-one-bits count-trailing-zeros
   crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512
   dtoastr dtotimespec dup2 environ execinfo faccessat
index 0e0dc51f361322bd3b3f44a5776141ebd498a4ee..e108f5b2f5dc85605c9a918782100e4d057a1b0a 100644 (file)
@@ -1,3 +1,18 @@
+2014-07-14  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Use binary-io module, O_BINARY, and "b" flag (Bug#18006).
+       * etags.c, hexl.c, make-docfile.c:
+       Include binary-io.h instead of fcntl.h and/or io.h.
+       (main): Use set_binary_mode or SET_BINARY
+       in place of handcrafted code.
+       * etags.c (main) [DOS_NT]:
+       * movemail.c (main) [WINDOWSNT]:
+       Don't mess with _fmode.
+       * etags.c (main, process_file_name, analyse_regex):
+       Use fopen/popen's "b" flag instead.
+       * movemail.c (main, popmail): Use open/lk_open/mkostemp's O_BINARY
+       instead.
+
 2014-07-13  Paul Eggert  <eggert@cs.ucla.edu>
 
        * make-docfile.c: Simplify a bit, to simplify further refactoring.
index 5f1f99c46776aea9304c01211c61b021b164f79b..ae5491a82040e39f525d958a5ce2f055aa5c71c5 100644 (file)
@@ -105,17 +105,13 @@ char pot_etags_version[] = "@(#) pot revision number is 17.38.1.4";
 #ifdef MSDOS
 # undef MSDOS
 # define MSDOS true
-# include <fcntl.h>
 # include <sys/param.h>
-# include <io.h>
 #else
 # define MSDOS false
 #endif /* MSDOS */
 
 #ifdef WINDOWSNT
-# include <fcntl.h>
 # include <direct.h>
-# include <io.h>
 # define MAXPATHLEN _MAX_PATH
 # undef HAVE_NTGUI
 # undef  DOS_NT
@@ -131,6 +127,7 @@ char pot_etags_version[] = "@(#) pot revision number is 17.38.1.4";
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <binary-io.h>
 #include <c-strcase.h>
 
 #include <assert.h>
@@ -1002,13 +999,8 @@ main (int argc, char **argv)
   linebuffer filename_lb;
   bool help_asked = false;
   ptrdiff_t len;
- char *optstring;
- int opt;
-
-
-#ifdef DOS_NT
-  _fmode = O_BINARY;   /* all of files are treated as binary files */
-#endif /* DOS_NT */
+  char *optstring;
+  int opt;
 
   progname = argv[0];
   nincluded_files = 0;
@@ -1195,15 +1187,10 @@ main (int argc, char **argv)
       if (streq (tagfile, "-"))
        {
          tagf = stdout;
-#ifdef DOS_NT
-         /* Switch redirected `stdout' to binary mode (setting `_fmode'
-            doesn't take effect until after `stdout' is already open). */
-         if (!isatty (fileno (stdout)))
-           setmode (fileno (stdout), O_BINARY);
-#endif /* DOS_NT */
+         SET_BINARY (fileno (stdout));
        }
       else
-       tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
+       tagf = fopen (tagfile, append_to_tagfile ? "ab" : "wb");
       if (tagf == NULL)
        pfatal (tagfile);
     }
@@ -1306,7 +1293,7 @@ main (int argc, char **argv)
       append_to_tagfile = true;
     }
 
-  tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
+  tagf = fopen (tagfile, append_to_tagfile ? "ab" : "wb");
   if (tagf == NULL)
     pfatal (tagfile);
   put_entries (nodehead);      /* write all the tags (CTAGS) */
@@ -1547,11 +1534,11 @@ process_file_name (char *file, language *lang)
   if (real_name == compressed_name)
     {
       char *cmd = concat (compr->command, " ", real_name);
-      inf = (FILE *) popen (cmd, "r");
+      inf = popen (cmd, "rb");
       free (cmd);
     }
   else
-    inf = fopen (real_name, "r");
+    inf = fopen (real_name, "rb");
   if (inf == NULL)
     {
       perror (real_name);
@@ -5614,7 +5601,7 @@ analyse_regex (char *regex_arg)
        char *regexfile = regex_arg + 1;
 
        /* regexfile is a file containing regexps, one per line. */
-       regexfp = fopen (regexfile, "r");
+       regexfp = fopen (regexfile, "rb");
        if (regexfp == NULL)
          pfatal (regexfile);
        linebuffer_init (&regexbuf);
index 9e21ddf9de639aef1a0f0a7db83fdc1df0ca8d8f..51744ab08a2b2e552379f480fb504e7c9a80ac04 100644 (file)
@@ -24,15 +24,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <stdio.h>
 #include <ctype.h>
-#ifdef DOS_NT
-#include <fcntl.h>
-#if __DJGPP__ >= 2
-#include <io.h>
-#endif
-#endif
-#ifdef WINDOWSNT
-#include <io.h>
-#endif
+
+#include <binary-io.h>
 
 #define DEFAULT_GROUPING       0x01
 #define DEFAULT_BASE           16
@@ -155,20 +148,12 @@ main (int argc, char **argv)
 
       if (un_flag)
        {
-         char buf[18];
-
-#ifdef DOS_NT
-#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
-          if (!isatty (fileno (stdout)))
-           setmode (fileno (stdout), O_BINARY);
-#else
-         (stdout)->_flag &= ~_IOTEXT; /* print binary */
-         _setmode (fileno (stdout), O_BINARY);
-#endif
-#endif
+         SET_BINARY (fileno (stdout));
+
          for (;;)
            {
-             register int i, c = 0, d;
+             int i, c = 0, d;
+             char buf[18];
 
 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
 
@@ -210,15 +195,7 @@ main (int argc, char **argv)
        }
       else
        {
-#ifdef DOS_NT
-#if (__DJGPP__ >= 2) || (defined WINDOWSNT)
-          if (!isatty (fileno (fp)))
-           setmode (fileno (fp), O_BINARY);
-#else
-         (fp)->_flag &= ~_IOTEXT; /* read binary */
-         _setmode (fileno (fp), O_BINARY);
-#endif
-#endif
+         SET_BINARY (fileno (fp));
          address = 0;
          string[0] = ' ';
          string[17] = '\0';
index 6692a0a645046f8346ea3284675043c8abc54fe0..cb0a56607f8ea59d96c299c6be2b29a678718d17 100644 (file)
@@ -38,17 +38,16 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <stdio.h>
 #include <stdlib.h>   /* config.h unconditionally includes this anyway */
-#ifdef MSDOS
-#include <fcntl.h>
-#endif /* MSDOS */
+
 #ifdef WINDOWSNT
 /* Defined to be sys_fopen in ms-w32.h, but only #ifdef emacs, so this
    is really just insurance.  */
 #undef fopen
-#include <fcntl.h>
 #include <direct.h>
 #endif /* WINDOWSNT */
 
+#include <binary-io.h>
+
 #ifdef DOS_NT
 /* Defined to be sys_chdir in ms-w32.h, but only #ifdef emacs, so this
    is really just insurance.
@@ -167,19 +166,7 @@ main (int argc, char **argv)
       ++i;
     }
 
-  /* Don't put CRs in the output file.  */
-#ifdef MSDOS
-  _fmode = O_BINARY;
-#if 0  /* Suspicion is that this causes hanging.
-         So instead we require people to use -o on MSDOS.  */
-  (stdout)->_flag &= ~_IOTEXT;
-  _setmode (fileno (stdout), O_BINARY);
-#endif
-#endif /* MSDOS */
-#ifdef WINDOWSNT
-  _fmode = O_BINARY;
-  _setmode (fileno (stdout), O_BINARY);
-#endif /* WINDOWSNT */
+  set_binary_mode (fileno (stdout), O_BINARY);
 
   if (generate_globals)
     start_globals ();
index d0d00fcf4cc089c34cfd10442d88b527f9c15c9e..c600fc0ea536428792fdf6f24af1698193abb626 100644 (file)
@@ -191,11 +191,6 @@ main (int argc, char **argv)
   uid_t real_gid = getgid ();
   uid_t priv_gid = getegid ();
 
-#ifdef WINDOWSNT
-  /* Ensure all file i/o is in binary mode. */
-  _fmode = _O_BINARY;
-#endif
-
   delete_lockname = 0;
 
   while ((c = getopt (argc, argv, ARGSTR)) != EOF)
@@ -304,7 +299,7 @@ main (int argc, char **argv)
 
          memcpy (tempname, inname, inname_dirlen);
          strcpy (tempname + inname_dirlen, "EXXXXXX");
-         desc = mkostemp (tempname, 0);
+         desc = mkostemp (tempname, O_BINARY);
          if (desc < 0)
            {
              int mkostemp_errno = errno;
@@ -358,12 +353,12 @@ main (int argc, char **argv)
 
 #ifndef MAIL_USE_MMDF
 #ifdef MAIL_USE_SYSTEM_LOCK
-      indesc = open (inname, O_RDWR);
+      indesc = open (inname, O_RDWR | O_BINARY);
 #else  /* if not MAIL_USE_SYSTEM_LOCK */
-      indesc = open (inname, O_RDONLY);
+      indesc = open (inname, O_RDONLY | O_BINARY);
 #endif /* not MAIL_USE_SYSTEM_LOCK */
 #else  /* MAIL_USE_MMDF */
-      indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
+      indesc = lk_open (inname, O_RDONLY | O_BINARY, 0, 0, 10);
 #endif /* MAIL_USE_MMDF */
 
       if (indesc < 0)
@@ -372,7 +367,7 @@ main (int argc, char **argv)
       /* Make sure the user can read the output file.  */
       umask (umask (0) & 0377);
 
-      outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
+      outdesc = open (outname, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
       if (outdesc < 0)
        pfatal_with_name (outname);
 
@@ -675,7 +670,7 @@ popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse
       return EXIT_SUCCESS;
     }
 
-  mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
+  mbfi = open (outfile, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
   if (mbfi < 0)
     {
       pop_close (server);
index 53795742131ed34d52914ef96cef6fbf6f3c26cf..ee600602895b6e7628c943629bd827ce539e9ba0 100644 (file)
@@ -1,5 +1,18 @@
 2014-07-14  Paul Eggert  <eggert@cs.ucla.edu>
 
+       Use binary-io module, O_BINARY, and "b" flag (Bug#18006).
+       * callproc.c (create_temp_file): Use mkostemp's O_BINARY flag.
+       * emacs.c [MSDOS]:
+       * emacs.c (main) [DOS_NT]: Don't mess with _fmode.
+       (main) [MSDOS]: Use SET_BINARY instead of setmode.
+       * minibuf.c: Include binary-io.h instead of fcntl.h.
+       (read_minibuf_noninteractive):
+       Use set_binary_mode instead of handcrafted code.
+       Don't call emacs_set_tty if emacs_get_tty failed.
+       * sysdep.c, systty.h (emacs_get_tty): Return int, not void.
+       * sysdep.c (emacs_open, emacs_pipe): Use O_BINARY.
+       * w32.c (pipe2): Adjust eassert to include O_BINARY.
+
        * macros.c (Fstart_kbd_macro): Avoid need for overflow check.
        This works around a GCC compiler bug when Emacs is configured with
        --enable-gcc-warnings.
index 9fbc7e5b713a0e01cda8561b8dbce8aedde575f7..2f68ea6f32802e361b7507611206f6aada9fc502 100644 (file)
@@ -982,7 +982,7 @@ create_temp_file (ptrdiff_t nargs, Lisp_Object *args,
 
     count = SPECPDL_INDEX ();
     record_unwind_protect_nothing ();
-    fd = mkostemp (tempfile, O_CLOEXEC);
+    fd = mkostemp (tempfile, O_BINARY | O_CLOEXEC);
     if (fd < 0)
       report_file_error ("Failed to open temporary file using pattern",
                         pattern);
index 117ce4decdbbc3b58fd1761f3cda6de8305e2988..a53b4cfd4edd910fdd3d89f48aaf165dd3b9948d 100644 (file)
@@ -51,6 +51,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "cygw32.h"
 #endif
 
+#ifdef MSDOS
+#include <binary-io.h>
+#endif
+
 #ifdef HAVE_WINDOW_SYSTEM
 #include TERM_HEADER
 #endif /* HAVE_WINDOW_SYSTEM */
@@ -918,20 +922,10 @@ main (int argc, char **argv)
 
 #endif /* not SYSTEM_MALLOC */
 
-#if defined (MSDOS) || defined (WINDOWSNT)
-  /* We do all file input/output as binary files.  When we need to translate
-     newlines, we do that manually.  */
-  _fmode = O_BINARY;
-#endif /* MSDOS || WINDOWSNT */
-
 #ifdef MSDOS
-  if (!isatty (fileno (stdin)))
-    setmode (fileno (stdin), O_BINARY);
-  if (!isatty (fileno (stdout)))
-    {
-      fflush (stdout);
-      setmode (fileno (stdout), O_BINARY);
-    }
+  SET_BINARY (fileno (stdin));
+  fflush (stdout);
+  SET_BINARY (fileno (stdout));
 #endif /* MSDOS */
 
   /* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.
index e2450b53964ea0d345fff78ba58f418ba64d852c..b85d3f57df3237cba2db3b9bfdb9ab9929adc34c 100644 (file)
@@ -22,9 +22,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <errno.h>
 #include <stdio.h>
 
-#ifdef WINDOWSNT
-#include <fcntl.h>     /* For O_BINARY, O_TEXT. */
-#endif
+#include <binary-io.h>
 
 #include "lisp.h"
 #include "commands.h"
@@ -231,6 +229,7 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
   int c;
   unsigned char hide_char = 0;
   struct emacs_tty etty;
+  bool etty_valid;
 
   /* Check, whether we need to suppress echoing.  */
   if (CHARACTERP (Vread_hide_char))
@@ -239,11 +238,9 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
   /* Manipulate tty.  */
   if (hide_char)
     {
-      emacs_get_tty (fileno (stdin), &etty);
-#ifdef WINDOWSNT
-      if (isatty (fileno (stdin)))
-       _setmode (fileno (stdin), O_BINARY);
-#endif
+      etty_valid = emacs_get_tty (fileno (stdin), &etty) == 0;
+      if (etty_valid)
+       set_binary_mode (fileno (stdin), O_BINARY);
       suppress_echo_on_tty (fileno (stdin));
     }
 
@@ -281,11 +278,11 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial,
   if (hide_char)
     {
       fprintf (stdout, "\n");
-      emacs_set_tty (fileno (stdin), &etty, 0);
-#ifdef WINDOWSNT
-      if (isatty (fileno (stdin)))
-       _setmode (fileno (stdin), O_TEXT);
-#endif
+      if (etty_valid)
+       {
+         emacs_set_tty (fileno (stdin), &etty, 0);
+         set_binary_mode (fileno (stdin), O_TEXT);
+       }
     }
 
   if (len || c == '\n' || c == '\r')
index eae15ea1ec34171b9377a20ece4edf55d701c280..d5cfd5b88cf167e20e39b5de183bb5353679a6aa 100644 (file)
@@ -775,8 +775,9 @@ widen_foreground_group (int fd)
 /* Getting and setting emacs_tty structures.  */
 
 /* Set *TC to the parameters associated with the terminal FD,
-   or clear it if the parameters are not available.  */
-void
+   or clear it if the parameters are not available.
+   Return 0 on success, -1 on failure.  */
+int
 emacs_get_tty (int fd, struct emacs_tty *settings)
 {
   /* Retrieve the primary parameters - baud rate, character size, etcetera.  */
@@ -786,15 +787,16 @@ emacs_get_tty (int fd, struct emacs_tty *settings)
   HANDLE h = (HANDLE)_get_osfhandle (fd);
   DWORD console_mode;
 
-  if (h && h != INVALID_HANDLE_VALUE)
+  if (h && h != INVALID_HANDLE_VALUE && GetConsoleMode (h, &console_mode))
     {
-      if (GetConsoleMode (h, &console_mode))
-       settings->main = console_mode;
+      settings->main = console_mode;
+      return 0;
     }
 #endif /* WINDOWSNT */
+  return -1;
 #else  /* !DOS_NT */
   /* We have those nifty POSIX tcmumbleattr functions.  */
-  tcgetattr (fd, &settings->main);
+  return tcgetattr (fd, &settings->main);
 #endif
 }
 
@@ -2198,6 +2200,7 @@ emacs_abort (void)
 #endif
 
 /* Open FILE for Emacs use, using open flags OFLAG and mode MODE.
+   Use binary I/O on systems that care about text vs binary I/O.
    Arrange for subprograms to not inherit the file descriptor.
    Prefer a method that is multithread-safe, if available.
    Do not fail merely because the open was interrupted by a signal.
@@ -2207,6 +2210,8 @@ int
 emacs_open (const char *file, int oflags, int mode)
 {
   int fd;
+  if (! (oflags & O_TEXT))
+    oflags |= O_BINARY;
   oflags |= O_CLOEXEC;
   while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR)
     QUIT;
@@ -2254,7 +2259,7 @@ emacs_pipe (int fd[2])
 #ifdef MSDOS
   return pipe (fd);
 #else  /* !MSDOS */
-  int result = pipe2 (fd, O_CLOEXEC);
+  int result = pipe2 (fd, O_BINARY | O_CLOEXEC);
   if (! O_CLOEXEC && result == 0)
     {
       fcntl (fd[0], F_SETFD, FD_CLOEXEC);
index dcab02683932e433a00b94cdd69d2562522e3e47..a29c4933d7ba17a96f6d2f5e86baf4ff42e2c1f3 100644 (file)
@@ -80,7 +80,7 @@ struct emacs_tty {
 };
 \f
 /* From sysdep.c or w32.c  */
-extern void emacs_get_tty (int, struct emacs_tty *) EXTERNALLY_VISIBLE;
+extern int emacs_get_tty (int, struct emacs_tty *) EXTERNALLY_VISIBLE;
 extern int emacs_set_tty (int, struct emacs_tty *, bool) EXTERNALLY_VISIBLE;
 extern void suppress_echo_on_tty (int);
 extern int serial_open (Lisp_Object);
index 37a01a311a67a0add165f68703fb78ddbecbc35a..7cb9d8960c501e2987432de25e8d2545040eda0a 100644 (file)
--- a/src/w32.c
+++ b/src/w32.c
@@ -882,7 +882,7 @@ set_named_security_info (LPCTSTR lpObjectName,
          g_b_init_set_named_security_info_a = 1;
          hm_advapi32 = LoadLibrary ("Advapi32.dll");
          s_pfn_Set_Named_Security_InfoA =
-           (SetNamedSecurityInfoA_Proc) GetProcAddress (hm_advapi32, 
+           (SetNamedSecurityInfoA_Proc) GetProcAddress (hm_advapi32,
                                                         "SetNamedSecurityInfoA");
        }
       if (s_pfn_Set_Named_Security_InfoA == NULL)
@@ -7865,7 +7865,7 @@ pipe2 (int * phandles, int pipe2_flags)
   int rc;
   unsigned flags;
 
-  eassert (pipe2_flags == O_CLOEXEC);
+  eassert (pipe2_flags == (O_BINARY | O_CLOEXEC));
 
   /* make pipe handles non-inheritable; when we spawn a child, we
      replace the relevant handle with an inheritable one.  Also put