rewrite popen to use posix_spawn instead of fragile vfork hacks
[musl] / src / stdio / fopen.c
1 #include "stdio_impl.h"
2 #include <fcntl.h>
3 #include <string.h>
4 #include <errno.h>
5
6 FILE *fopen(const char *restrict filename, const char *restrict mode)
7 {
8         FILE *f;
9         int fd;
10         int flags;
11
12         /* Check for valid initial mode character */
13         if (!strchr("rwa", *mode)) {
14                 errno = EINVAL;
15                 return 0;
16         }
17
18         /* Compute the flags to pass to open() */
19         flags = __fmodeflags(mode);
20
21         fd = syscall_cp(SYS_open, filename, flags|O_LARGEFILE, 0666);
22         if (fd < 0) return 0;
23
24         f = __fdopen(fd, mode);
25         if (f) return f;
26
27         __syscall(SYS_close, fd);
28         return 0;
29 }
30
31 LFS64(fopen);