From 85c61491382e4478b9b16e089c47234be195ce8f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 17 Aug 2023 12:03:59 +0300 Subject: [PATCH] LibC: add fileno() and fdopen() fdopen() doesn't currently care about mode and will have same mode as the underlying file descriptor. --- libc/stdio.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/libc/stdio.cpp b/libc/stdio.cpp index a151c8edf0..339f591a61 100644 --- a/libc/stdio.cpp +++ b/libc/stdio.cpp @@ -49,8 +49,23 @@ int fclose(FILE* file) return 0; } -// TODO -FILE* fdopen(int, const char*); +FILE* fdopen(int fd, const char* mode) +{ + // FIXME + (void)mode; + + for (int i = 0; i < FOPEN_MAX; i++) + { + if (s_files[i].fd == -1) + { + s_files[i] = { .fd = fd }; + return &s_files[i]; + } + } + + errno = EMFILE; + return nullptr; +} int feof(FILE* file) { @@ -144,8 +159,12 @@ char* fgets(char* str, int size, FILE* file) return str; } -// TODO -int fileno(FILE*); +int fileno(FILE* fp) +{ + if (fp == nullptr) + return EBADF; + return fp->fd; +} // TODO void flockfile(FILE*);