mirror of https://git.musl-libc.org/git/musl
Browse Source
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.rs-1.0
5 changed files with 41 additions and 17 deletions
@ -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; |
|||
} |
|||
@ -1,10 +1,21 @@ |
|||
#define _GNU_SOURCE |
|||
#include <unistd.h> |
|||
#include <errno.h> |
|||
#include <fcntl.h> |
|||
#include "syscall.h" |
|||
#include "libc.h" |
|||
|
|||
int dup3(int old, int new, int flags) { |
|||
int __dup3(int old, int new, int flags) |
|||
{ |
|||
int r; |
|||
while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY); |
|||
if (old==new) return __syscall_ret(-EINVAL); |
|||
if (flags & O_CLOEXEC) { |
|||
while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY); |
|||
if (r!=-ENOSYS) return __syscall_ret(r); |
|||
} |
|||
while ((r=__syscall(SYS_dup2, old, new))==-EBUSY); |
|||
if (flags & O_CLOEXEC) __syscall(SYS_fcntl, new, F_SETFD, FD_CLOEXEC); |
|||
return __syscall_ret(r); |
|||
} |
|||
|
|||
weak_alias(__dup3, dup3); |
|||
|
|||
Loading…
Reference in new issue