From 2980ee3973cd51cc8cb1adfca12dba5a70f291fe Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 6 Nov 2022 14:53:05 +0100 Subject: [PATCH] libc: Make fread() and fwrite() less awkward --- libs/libc/src/file.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/libc/src/file.cpp b/libs/libc/src/file.cpp index d6836ed3..d686d8da 100644 --- a/libs/libc/src/file.cpp +++ b/libs/libc/src/file.cpp @@ -127,15 +127,15 @@ extern "C" size_t fread(void* buf, size_t size, size_t nmemb, FILE* stream) { - ssize_t status = - read(stream->f_fd, buf, - size * nmemb); // FIXME: This function should use file_read_buf() to not conflict with fgets(). + size_t rsize = size * nmemb; + ssize_t status = read(stream->f_fd, buf, rsize); if (status < 0) { stream->f_err = 1; return 0; } - if (status == 0) stream->f_eof = 1; + if (status == 0 && rsize) stream->f_eof = 1; + if (status == 0) return (size_t)status; return (size_t)status / size; } @@ -281,13 +281,15 @@ extern "C" size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream) { - ssize_t status = write(stream->f_fd, buf, size * nmemb); + size_t rsize = size * nmemb; + ssize_t status = write(stream->f_fd, buf, rsize); if (status < 0) { stream->f_err = 1; return 0; } - if (status == 0) stream->f_eof = 1; + if (status == 0 && rsize) stream->f_eof = 1; + if (status == 0) return (size_t)status; return (size_t)status / size; }