diff options
author | 2025-04-28 14:40:14 +0200 | |
---|---|---|
committer | 2025-05-21 15:32:14 +0200 | |
commit | a009a0c6faa9e7ffac13d55ef6cdac1f9a68efbe (patch) | |
tree | 082ec060d6e79f6647a9a73cb8c91a8280764923 /tools/include/nolibc/stdio.h | |
parent | tools/nolibc: add namespace functionality (diff) | |
download | wireguard-linux-a009a0c6faa9e7ffac13d55ef6cdac1f9a68efbe.tar.xz wireguard-linux-a009a0c6faa9e7ffac13d55ef6cdac1f9a68efbe.zip |
tools/nolibc: add fopen()
This is used in various selftests and will be handy when integrating
those with nolibc.
Only the standard POSIX modes are supported.
No extensions nor the (noop) "b" from ISO C are accepted.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-13-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Diffstat (limited to 'tools/include/nolibc/stdio.h')
-rw-r--r-- | tools/include/nolibc/stdio.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index df5717d59182..c470d334ef3f 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -13,6 +13,7 @@ #include "std.h" #include "arch.h" #include "errno.h" +#include "fcntl.h" #include "types.h" #include "sys.h" #include "stdarg.h" @@ -55,6 +56,32 @@ FILE *fdopen(int fd, const char *mode __attribute__((unused))) return (FILE*)(intptr_t)~fd; } +static __attribute__((unused)) +FILE *fopen(const char *pathname, const char *mode) +{ + int flags, fd; + + switch (*mode) { + case 'r': + flags = O_RDONLY; + break; + case 'w': + flags = O_WRONLY | O_CREAT | O_TRUNC; + break; + case 'a': + flags = O_WRONLY | O_CREAT | O_APPEND; + break; + default: + SET_ERRNO(EINVAL); return NULL; + } + + if (mode[1] == '+') + flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; + + fd = open(pathname, flags, 0666); + return fdopen(fd, mode); +} + /* provides the fd of stream. */ static __attribute__((unused)) int fileno(FILE *stream) |