+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.
#include <errno.h>
#include <fcntl.h>
+#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
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)
/* 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;
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;
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;
/* 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;
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:
*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,
#endif
#if GNULIB_FOPEN_GNU
- if (open_flags_gnu != 0)
+ if (open_flags_gnu)
{
int fd;
FILE *fp;