From fc00c656c96528112d05cf0edf8631bd5eaea446 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 7 Dec 2025 20:10:31 +0100 Subject: Add build system scaffolding and libc headers --- lib/libc/stdio/fdopen.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/libc/stdio/fdopen.c (limited to 'lib/libc/stdio/fdopen.c') diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c new file mode 100644 index 00000000..f2ce0693 --- /dev/null +++ b/lib/libc/stdio/fdopen.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +weak void __stdio_cleanup(void) +{ +} + +FILE *fdopen(int fildes, const char *mode) +{ + FILE *fp; + + if (mode == NULL || + (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a')) { + return NULL; + } + + if ((fp = calloc(1, sizeof(FILE))) == NULL) + return NULL; + + fp->fd = fildes; + atomic_flag_clear(&fp->lock); + if (mode[0] == 'r') { + fp->type = _IONBF; + } else if (mode[0] == 'w') { + fp->type = _IOLBF; + } else if (mode[0] == 'a') { + fp->type = _IONBF; + off_t offset = lseek(fildes, 0, SEEK_END); + if (offset == (off_t)-1) { + free(fp); + return NULL; + } + } + + __libc_fadd(fp); + return fp; +} -- cgit v1.2.3