]>
Commit | Line | Data |
---|---|---|
e5f14be3 | 1 | #ifdef __wasilibc_unmodified_upstream // WASI has no syscall |
320054e8 DG |
2 | #else |
3 | #include <unistd.h> | |
4 | #endif | |
5 | #include "stdio_impl.h" | |
6 | #include <fcntl.h> | |
7 | #include <string.h> | |
8 | #include <errno.h> | |
9 | ||
10 | FILE *fopen(const char *restrict filename, const char *restrict mode) | |
11 | { | |
12 | FILE *f; | |
13 | int fd; | |
14 | int flags; | |
15 | ||
16 | /* Check for valid initial mode character */ | |
17 | if (!strchr("rwa", *mode)) { | |
18 | errno = EINVAL; | |
19 | return 0; | |
20 | } | |
21 | ||
22 | /* Compute the flags to pass to open() */ | |
23 | flags = __fmodeflags(mode); | |
24 | ||
e5f14be3 | 25 | #ifdef __wasilibc_unmodified_upstream // WASI has no sys_open |
320054e8 DG |
26 | fd = sys_open(filename, flags, 0666); |
27 | #else | |
28 | fd = open(filename, flags, 0666); | |
29 | #endif | |
30 | if (fd < 0) return 0; | |
e5f14be3 | 31 | #ifdef __wasilibc_unmodified_upstream // WASI has no syscall |
320054e8 DG |
32 | if (flags & O_CLOEXEC) |
33 | __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); | |
34 | #else | |
35 | /* Avoid __syscall, but also, FD_CLOEXEC is not supported in WASI. */ | |
36 | #endif | |
37 | ||
38 | f = __fdopen(fd, mode); | |
39 | if (f) return f; | |
40 | ||
e5f14be3 | 41 | #ifdef __wasilibc_unmodified_upstream // WASI has no syscall |
320054e8 DG |
42 | __syscall(SYS_close, fd); |
43 | #else | |
ec9f1c39 | 44 | close(fd); |
320054e8 DG |
45 | #endif |
46 | return 0; | |
47 | } | |
48 | ||
49 | weak_alias(fopen, fopen64); |