master
 1#include "stdio_impl.h"
 2#include <fcntl.h>
 3#include <string.h>
 4#include <errno.h>
 5
 6FILE *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 = 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);
25
26	f = __fdopen(fd, mode);
27	if (f) return f;
28
29	__syscall(SYS_close, fd);
30	return 0;
31}