From 0f30d227497418c6d3bef7d52244407e30454504 Mon Sep 17 00:00:00 2001 From: Kacper Date: Mon, 22 Dec 2025 23:27:56 +0100 Subject: Added c11 threads, fixed some locks and add *_unlocked functions --- lib/libc/stdio/fflush.c | 73 +++++-------------------------------------------- 1 file changed, 7 insertions(+), 66 deletions(-) (limited to 'lib/libc/stdio/fflush.c') diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c index 1cf4b7bd..b3fca8bd 100644 --- a/lib/libc/stdio/fflush.c +++ b/lib/libc/stdio/fflush.c @@ -3,74 +3,15 @@ #include // for LIBC_UNLOCK, LIBC_LOCK #include // for errno, EBADF, EIO -#include // for O_ACCMODE, O_RDONLY -#include // for EOF, FILE, fflush -#include // for memmove -#include // for size_t, write, ssize_t +#include -int fflush(FILE *stream) +int fflush(struct __FILE *stream) { - if (stream == NULL) { - // TODO: Implement flushing all open streams - // For now, just return success - return 0; - } + int r; - if (__FILE(stream)->buf_len == 0) { - return 0; - } + flockfile(stream); + r = fflush_unlocked(stream); + funlockfile(stream); - if (__FILE(stream)->fd == -1) { - __FILE(stream)->buf_len = 0; - return 0; - } - - if (__FILE(stream)->flags & _IO_ERR) { - errno = EIO; - return EOF; - } - - if ((__FILE(stream)->flags & O_ACCMODE) == O_RDONLY) { - errno = EBADF; - return EOF; - } - - LIBC_LOCK(__FILE(stream)->lock); - - size_t bytes_to_write = __FILE(stream)->buf_len; - size_t total_written = 0; - char *buf_ptr = __FILE(stream)->buf; - - while (total_written < bytes_to_write) { - ssize_t result = write(__FILE(stream)->fd, - buf_ptr + total_written, - bytes_to_write - total_written); - - if (result < 0) { - __FILE(stream)->flags |= _IO_ERR; - LIBC_UNLOCK(__FILE(stream)->lock); - return EOF; - } - - if (result == 0) { - break; - } - - total_written += result; - } - - if (total_written == bytes_to_write) { - __FILE(stream)->buf_len = 0; - __FILE(stream)->buf_pos = 0; - } else { - size_t remaining = bytes_to_write - total_written; - memmove(__FILE(stream)->buf, - __FILE(stream)->buf + total_written, remaining); - __FILE(stream)->buf_len = remaining; - __FILE(stream)->buf_pos = 0; - } - - LIBC_UNLOCK(__FILE(stream)->lock); - - return (total_written == bytes_to_write) ? 0 : EOF; + return r; } -- cgit v1.2.3