From 2f46e46aa4b218b034e424b4c5f9c646e037f8f8 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 11 Oct 2022 21:09:30 +0200 Subject: [PATCH] libc: Implement fwrite() Now that we have the write() syscall and libc wrapper, fwrite can finally be implemented. --- libs/libc/src/file.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libs/libc/src/file.cpp b/libs/libc/src/file.cpp index c98db0ee..8e70e47a 100644 --- a/libs/libc/src/file.cpp +++ b/libs/libc/src/file.cpp @@ -5,6 +5,9 @@ #include #include +FILE* __stderr; +FILE* __stdout; + extern "C" { int fclose(FILE* stream) @@ -29,7 +32,7 @@ extern "C" FILE* fopen(const char* pathname, const char*) { - int fd = open(pathname, O_RDONLY); // FIXME: Use the mode string. + int fd = open(pathname, O_RDWR); // FIXME: Use the mode string. if (fd < 0) { return 0; } FILE* stream = (FILE*)malloc(sizeof(FILE)); stream->f_fd = fd; @@ -74,9 +77,16 @@ extern "C" NOT_IMPLEMENTED("ftell"); } - size_t fwrite(const void*, size_t, size_t, FILE*) + size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream) { - NOT_IMPLEMENTED("fwrite"); + ssize_t status = write(stream->f_fd, buf, size * nmemb); + if (status < 0) + { + stream->f_err = 1; + return 0; + } + if (status == 0) stream->f_eof = 1; + return (size_t)status; } void setbuf(FILE*, char*)