From 934c30d35a3ca0e0bf6cd8709d779e060e27f6d2 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sat, 23 Nov 2024 14:59:26 +0100 Subject: libc: Add append to f(d)open and other fixes --- userland/libc/stdio/fopen.c | 49 +++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 33 deletions(-) (limited to 'userland/libc/stdio/fopen.c') diff --git a/userland/libc/stdio/fopen.c b/userland/libc/stdio/fopen.c index 52e78d1..fe0b5cf 100644 --- a/userland/libc/stdio/fopen.c +++ b/userland/libc/stdio/fopen.c @@ -1,16 +1,10 @@ #include -#include #include -#include -#include +#include -// FIXME: All modes not implemented // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html FILE *fopen(const char *pathname, const char *mode) { - uint8_t read = 0; - uint8_t write = 0; -// uint8_t append = 0; - // FIXME: Not parsed correctly + int flag = 0; for (; *mode; mode++) { // r or rb // Open file for reading. @@ -21,41 +15,30 @@ FILE *fopen(const char *pathname, const char *mode) { // end-of-file. switch (*mode) { case 'r': - read = 1; + flag |= O_READ; break; case 'w': - write = 1; + flag |= O_WRITE; break; case 'a': -// append = 1; + flag |= O_APPEND; break; } } - int flag = 0; - if (read) - flag |= O_READ; - if (write) - flag |= O_WRITE; int fd = open(pathname, flag, 0); - if (-1 == fd) + if (-1 == fd) { return NULL; + } - struct stat s; - stat(pathname, &s); - - FILE *r = calloc(1, sizeof(FILE)); - r->read = read_fd; - r->write = write_fd; - r->seek = seek_fd; - r->has_error = 0; - r->is_eof = 0; - r->offset_in_file = 0; - r->file_size = s.st_size; - r->cookie = NULL; - r->fd = fd; - r->read_buffer = NULL; - r->read_buffer_stored = 0; - r->fflush = fflush_fd; + FILE *r = fdopen(fd, mode); + if (!r) { + close(fd); + return NULL; + } + if(flag & O_WRITE) { + ftruncate(fd, 0); + } + r->has_control_over_the_fd = 1; return r; } -- cgit v1.2.3