diff options
author | Rich Felker <dalias@aerifal.cx> | 2012-10-24 21:16:06 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2012-10-24 21:16:06 -0400 |
commit | 892cafff665b44d238e3b664f61ca38dd965cba6 (patch) | |
tree | c65353fe88b0d78025e113b77ed8728a2b4bc8d4 /src/stdio/__fmodeflags.c | |
parent | 708c91f4e9be2cfd6d35e71361956e13f3201b85 (diff) | |
download | musl-892cafff665b44d238e3b664f61ca38dd965cba6.tar.gz musl-892cafff665b44d238e3b664f61ca38dd965cba6.tar.bz2 musl-892cafff665b44d238e3b664f61ca38dd965cba6.tar.xz musl-892cafff665b44d238e3b664f61ca38dd965cba6.zip |
greatly improve freopen behavior
1. don't open /dev/null just as a basis to copy flags; use shared
__fmodeflags function to get the right file flags for the mode.
2. handle the case (probably invalid, but whatever) case where the
original stream's file descriptor was closed; previously, the logic
re-closed it.
3. accept the "e" mode flag for close-on-exec; update dup3 to fallback
to using dup2 so we can simply call __dup3 instead of putting fallback
logic in freopen itself.
Diffstat (limited to 'src/stdio/__fmodeflags.c')
-rw-r--r-- | src/stdio/__fmodeflags.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/stdio/__fmodeflags.c b/src/stdio/__fmodeflags.c new file mode 100644 index 00000000..da9f23b6 --- /dev/null +++ b/src/stdio/__fmodeflags.c @@ -0,0 +1,16 @@ +#include <fcntl.h> +#include <string.h> + +int __fmodeflags(const char *mode) +{ + int flags; + if (strchr(mode, '+')) flags = O_RDWR; + else if (*mode == 'r') flags = O_RDONLY; + else flags = O_WRONLY; + if (strchr(mode, 'x')) flags |= O_EXCL; + if (strchr(mode, 'e')) flags |= O_CLOEXEC; + if (*mode != 'r') flags |= O_CREAT; + if (*mode == 'w') flags |= O_TRUNC; + if (*mode == 'a') flags |= O_APPEND; + return flags; +} |