summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/stdio/fopen.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 13:54:38 +0000
commit8c1ab65c0f548d20b7f177bdb736daaf603340e1 (patch)
treedf55b7e75bf43f2bf500845b105afe3ac3a5157e /libc-top-half/musl/src/stdio/fopen.c
parentInitial commit. (diff)
downloadwasi-libc-upstream/0.0_git20221206.8b7148f.tar.xz
wasi-libc-upstream/0.0_git20221206.8b7148f.zip
Adding upstream version 0.0~git20221206.8b7148f.upstream/0.0_git20221206.8b7148f
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libc-top-half/musl/src/stdio/fopen.c')
-rw-r--r--libc-top-half/musl/src/stdio/fopen.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/stdio/fopen.c b/libc-top-half/musl/src/stdio/fopen.c
new file mode 100644
index 0000000..670f438
--- /dev/null
+++ b/libc-top-half/musl/src/stdio/fopen.c
@@ -0,0 +1,51 @@
+#ifdef __wasilibc_unmodified_upstream // WASI has no syscall
+#else
+#include <unistd.h>
+#include <wasi/libc.h>
+#endif
+#include "stdio_impl.h"
+#include <fcntl.h>
+#include <string.h>
+#include <errno.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() */
+ flags = __fmodeflags(mode);
+
+#ifdef __wasilibc_unmodified_upstream // WASI has no sys_open
+ fd = sys_open(filename, flags, 0666);
+#else
+ // WASI libc ignores the mode parameter anyway, so skip the varargs.
+ fd = __wasilibc_open_nomode(filename, flags);
+#endif
+ if (fd < 0) return 0;
+#ifdef __wasilibc_unmodified_upstream // WASI has no syscall
+ if (flags & O_CLOEXEC)
+ __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+#else
+ /* Avoid __syscall, but also, FD_CLOEXEC is not supported in WASI. */
+#endif
+
+ f = __fdopen(fd, mode);
+ if (f) return f;
+
+#ifdef __wasilibc_unmodified_upstream // WASI has no syscall
+ __syscall(SYS_close, fd);
+#else
+ close(fd);
+#endif
+ return 0;
+}
+
+weak_alias(fopen, fopen64);