fix unsynchronized access to FILE structure in fflush(0)
[musl.git] / src / stdio / fopen.c
blob252f08241b3a313c057b3c6d8fc6baa1e8278c43
1 #include "stdio_impl.h"
2 #include <fcntl.h>
3 #include <string.h>
4 #include <errno.h>
6 FILE *fopen(const char *restrict filename, const char *restrict mode)
8 FILE *f;
9 int fd;
10 int flags;
12 /* Check for valid initial mode character */
13 if (!strchr("rwa", *mode)) {
14 errno = EINVAL;
15 return 0;
18 /* Compute the flags to pass to open() */
19 flags = __fmodeflags(mode);
21 fd = sys_open(filename, flags, 0666);
22 if (fd < 0) return 0;
23 if (flags & O_CLOEXEC)
24 __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
26 f = __fdopen(fd, mode);
27 if (f) return f;
29 __syscall(SYS_close, fd);
30 return 0;
33 LFS64(fopen);