mirror of
https://github.com/fluencelabs/musl
synced 2025-05-23 20:51:32 +00:00
this feature will be in the next version of POSIX, and can be used internally immediately. there are many internal uses of fopen where close-on-exec is needed to fix bugs.
36 lines
772 B
C
36 lines
772 B
C
#include "stdio_impl.h"
|
|
|
|
FILE *fopen(const char *restrict filename, const char *restrict mode)
|
|
{
|
|
FILE *f;
|
|
int fd;
|
|
int flags;
|
|
|
|
/* Check for valid initial mode character */
|
|
if (!strchr("rwa", *mode)) {
|
|
errno = EINVAL;
|
|
return 0;
|
|
}
|
|
|
|
/* 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;
|
|
|
|
fd = syscall_cp(SYS_open, filename, flags|O_LARGEFILE, 0666);
|
|
if (fd < 0) return 0;
|
|
|
|
f = __fdopen(fd, mode);
|
|
if (f) return f;
|
|
|
|
__syscall(SYS_close, fd);
|
|
return 0;
|
|
}
|
|
|
|
LFS64(fopen);
|