]> Savannah Git Hosting - gnulib.git/commitdiff
fopen-gnu: Simplify code.
authorBruno Haible <bruno@clisp.org>
Sat, 27 Jun 2020 09:08:51 +0000 (11:08 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 27 Jun 2020 09:08:51 +0000 (11:08 +0200)
* lib/fopen.c: Include <stdbool.h>.
(rpl_fopen): Use a single variable open_flags instead of
open_flags_standard and open_flags_gnu. Make open_flags_gnu a bool.
* modules/fopen (Depends-on): Add stdbool.

ChangeLog
lib/fopen.c
modules/fopen

index 945941e45c4ea43090096bc6d4add2a3d7c9576c..43506ffa2ce774a2d14e4a2fc809bdce1730dead 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2020-06-27  Bruno Haible  <bruno@clisp.org>
+
+       fopen-gnu: Simplify code.
+       * lib/fopen.c: Include <stdbool.h>.
+       (rpl_fopen): Use a single variable open_flags instead of
+       open_flags_standard and open_flags_gnu. Make open_flags_gnu a bool.
+       * modules/fopen (Depends-on): Add stdbool.
+
 2020-06-26  Bruno Haible  <bruno@clisp.org>
 
        canonicalize: Improve documentation.
index 47d7f194df7f9514751f5282c865ae19afbf41fd..0acffa25cf6c9fed358aa7687faedca921742068 100644 (file)
@@ -39,6 +39,7 @@ orig_fopen (const char *filename, const char *mode)
 
 #include <errno.h>
 #include <fcntl.h>
+#include <stdbool.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -48,13 +49,12 @@ FILE *
 rpl_fopen (const char *filename, const char *mode)
 {
   int open_direction;
-  int open_flags_standard;
+  int open_flags;
 #if GNULIB_FOPEN_GNU
-  int open_flags_gnu;
+  bool open_flags_gnu;
 # define BUF_SIZE 80
   char fdopen_mode_buf[BUF_SIZE + 1];
 #endif
-  int open_flags;
 
 #if defined _WIN32 && ! defined __CYGWIN__
   if (strcmp (filename, "/dev/null") == 0)
@@ -63,9 +63,9 @@ rpl_fopen (const char *filename, const char *mode)
 
   /* Parse the mode.  */
   open_direction = 0;
-  open_flags_standard = 0;
+  open_flags = 0;
 #if GNULIB_FOPEN_GNU
-  open_flags_gnu = 0;
+  open_flags_gnu = false;
 #endif
   {
     const char *p = mode;
@@ -86,7 +86,7 @@ rpl_fopen (const char *filename, const char *mode)
             continue;
           case 'w':
             open_direction = O_WRONLY;
-            open_flags_standard |= O_CREAT | O_TRUNC;
+            open_flags |= O_CREAT | O_TRUNC;
 #if GNULIB_FOPEN_GNU
             if (q < fdopen_mode_buf + BUF_SIZE)
               *q++ = *p;
@@ -94,7 +94,7 @@ rpl_fopen (const char *filename, const char *mode)
             continue;
           case 'a':
             open_direction = O_WRONLY;
-            open_flags_standard |= O_CREAT | O_APPEND;
+            open_flags |= O_CREAT | O_APPEND;
 #if GNULIB_FOPEN_GNU
             if (q < fdopen_mode_buf + BUF_SIZE)
               *q++ = *p;
@@ -104,7 +104,7 @@ rpl_fopen (const char *filename, const char *mode)
             /* While it is non-standard, O_BINARY is guaranteed by
                gnulib <fcntl.h>.  We can also assume that orig_fopen
                supports the 'b' flag.  */
-            open_flags_standard |= O_BINARY;
+            open_flags |= O_BINARY;
 #if GNULIB_FOPEN_GNU
             if (q < fdopen_mode_buf + BUF_SIZE)
               *q++ = *p;
@@ -119,10 +119,12 @@ rpl_fopen (const char *filename, const char *mode)
             continue;
 #if GNULIB_FOPEN_GNU
           case 'x':
-            open_flags_gnu |= O_EXCL;
+            open_flags |= O_EXCL;
+            open_flags_gnu = true;
             continue;
           case 'e':
-            open_flags_gnu |= O_CLOEXEC;
+            open_flags |= O_CLOEXEC;
+            open_flags_gnu = true;
             continue;
 #endif
           default:
@@ -145,11 +147,6 @@ rpl_fopen (const char *filename, const char *mode)
     *q = '\0';
 #endif
   }
-#if GNULIB_FOPEN_GNU
-  open_flags = open_flags_standard | open_flags_gnu;
-#else
-  open_flags = open_flags_standard;
-#endif
 
 #if FOPEN_TRAILING_SLASH_BUG
   /* Fail if the mode requires write access and the filename ends in a slash,
@@ -207,7 +204,7 @@ rpl_fopen (const char *filename, const char *mode)
 #endif
 
 #if GNULIB_FOPEN_GNU
-  if (open_flags_gnu != 0)
+  if (open_flags_gnu)
     {
       int fd;
       FILE *fp;
index 1bea86548455786da8b73a20bab5569f5afcc11c..425cb610377ddfdf69a865ce5d8c225f39ddc854 100644 (file)
@@ -8,6 +8,7 @@ m4/fopen.m4
 Depends-on:
 stdio
 largefile
+stdbool         [test $REPLACE_FOPEN = 1]
 unistd          [test $REPLACE_FOPEN = 1]
 fstat           [test $REPLACE_FOPEN = 1]