Subject: bug#18006: Simplify via set_binary_mode

From: Paul Eggert <[email-masked]>
Date: Mon, 14 Jul 2014 13:16:06 -0700

Eli Zaretskii wrote:
> Otherwise, the patch looks good to me.

Thanks; I installed it after fixing things up as per your comments.

Your explanation of _fmode led me to find a minor porting bug. 
Fx_load_color_file [!HAVE_X_WINDOWS] uses fopen with "rt"; unlike "rb" 
this isn't specified by POSIX and a web search suggests that it does 
fail on a few older platforms.  Emacs no longer modifies _fmode so "r" 
should suffice now anyway.  Also, there are two other places where some 
"rt"-related simplifications can be done.  Proposed further patch attached.
=== modified file 'lib-src/ChangeLog'
--- lib-src/ChangeLog	2014-07-14 19:23:18 +0000
+++ lib-src/ChangeLog	2014-07-14 19:46:54 +0000
@@ -1,5 +1,9 @@
 2014-07-14  Paul Eggert  <[email-masked]>
 
+	Use "b" flag more consistently; avoid "t" (Bug#18006).
+	* make-docfile.c (READ_TEXT): Remove; all uses replaced by "r".
+	(READ_BINARY): Remove; all uses replaced by "rb".
+
 	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.

=== modified file 'lib-src/make-docfile.c'
--- lib-src/make-docfile.c	2014-07-14 19:23:18 +0000
+++ lib-src/make-docfile.c	2014-07-14 19:46:54 +0000
@@ -55,12 +55,8 @@
    Similarly, msdos defines this as sys_chdir, but we're not linking with the
    file where that function is defined.  */
 #undef chdir
-#define READ_TEXT "rt"
-#define READ_BINARY "rb"
 #define IS_SLASH(c)  ((c) == '/' || (c) == '\\' || (c) == ':')
 #else  /* not DOS_NT */
-#define READ_TEXT "r"
-#define READ_BINARY "r"
 #define IS_SLASH(c)  ((c) == '/')
 #endif /* not DOS_NT */
 
@@ -216,11 +212,11 @@
   if (!generate_globals)
     put_filename (filename);
   if (len > 4 && !strcmp (filename + len - 4, ".elc"))
-    return scan_lisp_file (filename, READ_BINARY);
+    return scan_lisp_file (filename, "rb");
   else if (len > 3 && !strcmp (filename + len - 3, ".el"))
-    return scan_lisp_file (filename, READ_TEXT);
+    return scan_lisp_file (filename, "r");
   else
-    return scan_c_file (filename, READ_TEXT);
+    return scan_c_file (filename, "r");
 }
 
 static void

=== modified file 'src/ChangeLog'
--- src/ChangeLog	2014-07-14 19:23:18 +0000
+++ src/ChangeLog	2014-07-14 19:46:54 +0000
@@ -1,5 +1,10 @@
 2014-07-14  Paul Eggert  <[email-masked]>
 
+	Use "b" flag more consistently; avoid "t" (Bug#18006).
+	* lread.c (Fload) [DOS_NT]:
+	* xfaces.c (Fx_load_color_file) [!HAVE_X_WINDOWS]:
+	No longer need to use "rt" instead of "r".
+
 	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]:

=== modified file 'src/lread.c'
--- src/lread.c	2014-07-02 03:26:19 +0000
+++ src/lread.c	2014-07-14 19:46:54 +0000
@@ -1061,10 +1061,6 @@
   const char *fmode = "r";
   int version;
 
-#ifdef DOS_NT
-  fmode = "rt";
-#endif /* DOS_NT */
-
   CHECK_STRING (file);
 
   /* If file name is magic, call the handler.  */
@@ -1169,7 +1165,7 @@
 	 Tramp does not catch `load' operations for such files, so we
 	 end up with a nil as the `load' handler above.  If we would
 	 continue with fd = -2, we will behave wrongly, and in
-	 particular try reading a .elc file in the "rt" mode instead
+	 particular try reading a .elc file in the "r" mode instead
 	 of "rb".  See bug #9311 for the results.  To work around
 	 this, we try to open the file locally, and go with that if it
 	 succeeds.  */

=== modified file 'src/xfaces.c'
--- src/xfaces.c	2014-07-07 23:33:05 +0000
+++ src/xfaces.c	2014-07-14 19:46:54 +0000
@@ -6256,7 +6256,7 @@
   abspath = Fexpand_file_name (filename, Qnil);
 
   block_input ();
-  fp = emacs_fopen (SSDATA (abspath), "rt");
+  fp = emacs_fopen (SSDATA (abspath), "r");
   if (fp)
     {
       char buf[512];