From 892cafff665b44d238e3b664f61ca38dd965cba6 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 24 Oct 2012 21:16:06 -0400 Subject: 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. --- src/stdio/fopen.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'src/stdio/fopen.c') diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c index 03c10cd1..c741aede 100644 --- a/src/stdio/fopen.c +++ b/src/stdio/fopen.c @@ -13,14 +13,7 @@ FILE *fopen(const char *restrict filename, const char *restrict mode) } /* Compute the flags to pass to open() */ - 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; + flags = __fmodeflags(mode); fd = syscall_cp(SYS_open, filename, flags|O_LARGEFILE, 0666); if (fd < 0) return 0; -- cgit v1.2.1