From 169daa11155988a210fac949297381743f3cb400 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 9 Dec 2025 23:14:53 +0100 Subject: feat: clang-tidy fixes --- lib/libc/stdio/clearerr.c | 7 ++++--- lib/libc/stdio/dtoa.c | 8 +++---- lib/libc/stdio/fclose.c | 8 +++---- lib/libc/stdio/fdopen.c | 16 +++++++------- lib/libc/stdio/feof.c | 6 +++--- lib/libc/stdio/ferror.c | 5 ++--- lib/libc/stdio/fflush.c | 39 +++++++++++++++++----------------- lib/libc/stdio/fileno.c | 14 ++++++------- lib/libc/stdio/fmemopen.c | 13 ++++++------ lib/libc/stdio/fopen.c | 15 +++++++------- lib/libc/stdio/fputc.c | 13 ++++++------ lib/libc/stdio/fread.c | 29 +++++++++++++------------- lib/libc/stdio/fseek.c | 21 +++++++++---------- lib/libc/stdio/fwrite.c | 53 +++++++++++++++++++++++------------------------ lib/libc/stdio/pclose.c | 4 ++-- lib/libc/stdio/perror.c | 4 +++- lib/libc/stdio/popen.c | 18 +++++++++------- lib/libc/stdio/remove.c | 4 +--- lib/libc/stdio/rename.c | 4 +++- lib/libc/stdio/renameat.c | 3 ++- lib/libc/stdio/setbuf.c | 4 +++- lib/libc/stdio/setvbuf.c | 14 ++++++------- lib/libc/stdio/stderr.c | 3 ++- lib/libc/stdio/stdin.c | 4 ++-- lib/libc/stdio/stdout.c | 1 + 25 files changed, 157 insertions(+), 153 deletions(-) (limited to 'lib/libc/stdio') diff --git a/lib/libc/stdio/clearerr.c b/lib/libc/stdio/clearerr.c index 87d733ae..02244466 100644 --- a/lib/libc/stdio/clearerr.c +++ b/lib/libc/stdio/clearerr.c @@ -1,13 +1,14 @@ -#include "__stdio.h" // for _IO_EOF, _IO_ERR +#include "__stdio.h" // for __FILE, _IO_EOF, _IO_ERR #include "stddef.h" // for NULL -#include // for __IMPL #include // for FILE, clearerr void clearerr(FILE *stream) { + struct __FILE *stream_impl = __FILE(stream); + if (stream == NULL) return; - __IMPL(stream)->flags &= ~(_IO_ERR | _IO_EOF); + stream_impl->flags &= ~(_IO_ERR | _IO_EOF); } diff --git a/lib/libc/stdio/dtoa.c b/lib/libc/stdio/dtoa.c index faf4bd8c..0322162b 100644 --- a/lib/libc/stdio/dtoa.c +++ b/lib/libc/stdio/dtoa.c @@ -210,9 +210,9 @@ * used for input more than STRTOD_DIGLIM digits long (default 40). */ -#include // for LIBC_LOCK, LIBC_UNLOCK -#include // for __IMPL -#include // for thrd_current +#include <__thread.h> // for __thread_self +#include // for LIBC_LOCK, LIBC_UNLOCK +#include // for thrd_current static atomic_flag dtoa_lock[2] = { ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT }; @@ -1556,7 +1556,7 @@ void set_max_dtoa_threads(unsigned int n) static ThInfo *get_TI(void) { - unsigned int thno = __IMPL(thrd_current())->tid; + unsigned int thno = ((struct __thread_self *)thrd_current())->tid; if (thno < maxthreads) return TI1 + thno; diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c index 8b0586c6..4534715b 100644 --- a/lib/libc/stdio/fclose.c +++ b/lib/libc/stdio/fclose.c @@ -1,6 +1,6 @@ -#include // for __IMPL -#include // for fflush, FILE, fclose, stderr, stdin, stdout -#include // for close +#include <__stdio.h> // for __FILE +#include // for fflush, FILE, fclose, stderr, stdin, stdout +#include // for close int fclose(FILE *stream) { @@ -8,7 +8,7 @@ int fclose(FILE *stream) return -1; if (stream != stdin && stream != stdout && stream != stderr) { - if (close(__IMPL(stream)->fd) == -1) + if (close((__FILE(stream))->fd) == -1) return -1; } diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index f762a83d..c5db3854 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -1,9 +1,8 @@ -#include "__stdio.h" // for __libc_fadd +#include "__stdio.h" // for __FILE, __libc_fadd #include "features.h" // for __weak #include "stdatomic.h" // for atomic_flag_clear #include "stddef.h" // for NULL -#include // for __IMPL #include // for FILE, _IONBF, SEEK_END, _IOLBF, fdopen #include // for calloc, free #include // for lseek, off_t @@ -21,17 +20,18 @@ FILE *fdopen(int fildes, const char *mode) return NULL; } - if ((stream = calloc(1, sizeof(FILE))) == NULL) + stream = calloc(1, sizeof(FILE)); + if (stream == NULL) return NULL; - __IMPL(stream)->fd = fildes; - atomic_flag_clear(&__IMPL(stream)->lock); + __FILE(stream)->fd = fildes; + atomic_flag_clear(&__FILE(stream)->lock); if (mode[0] == 'r') { - __IMPL(stream)->type = _IONBF; + __FILE(stream)->type = _IONBF; } else if (mode[0] == 'w') { - __IMPL(stream)->type = _IOLBF; + __FILE(stream)->type = _IOLBF; } else if (mode[0] == 'a') { - __IMPL(stream)->type = _IONBF; + __FILE(stream)->type = _IONBF; off_t offset = lseek(fildes, 0, SEEK_END); if (offset == (off_t)-1) { diff --git a/lib/libc/stdio/feof.c b/lib/libc/stdio/feof.c index f8478d03..f9245d06 100644 --- a/lib/libc/stdio/feof.c +++ b/lib/libc/stdio/feof.c @@ -1,12 +1,12 @@ #include "stddef.h" // for NULL -#include // for __IMPL -#include // for FILE, feof +#include <__stdio.h> // for __FILE +#include // for FILE, feof int feof(FILE *stream) { if (stream == NULL) return 0; - return __IMPL(stream)->eof; + return (__FILE(stream))->eof; } diff --git a/lib/libc/stdio/ferror.c b/lib/libc/stdio/ferror.c index 69ef439d..5e4b8dc4 100644 --- a/lib/libc/stdio/ferror.c +++ b/lib/libc/stdio/ferror.c @@ -1,7 +1,6 @@ -#include "__stdio.h" // for _IO_ERR +#include "__stdio.h" // for __FILE, _IO_ERR #include "stddef.h" // for NULL -#include // for __IMPL #include // for FILE, ferror int ferror(FILE *stream) @@ -9,5 +8,5 @@ int ferror(FILE *stream) if (stream == NULL) return 0; - return (__IMPL(stream)->flags & _IO_ERR) != 0; + return (__FILE(stream)->flags & _IO_ERR) != 0; } diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c index 001e3139..1cf4b7bd 100644 --- a/lib/libc/stdio/fflush.c +++ b/lib/libc/stdio/fflush.c @@ -1,10 +1,9 @@ -#include "__stdio.h" // for _IO_ERR +#include "__stdio.h" // for __FILE, _IO_ERR #include "stddef.h" // for NULL #include // for LIBC_UNLOCK, LIBC_LOCK #include // for errno, EBADF, EIO #include // for O_ACCMODE, O_RDONLY -#include // for __IMPL #include // for EOF, FILE, fflush #include // for memmove #include // for size_t, write, ssize_t @@ -17,39 +16,39 @@ int fflush(FILE *stream) return 0; } - if (__IMPL(stream)->buf_len == 0) { + if (__FILE(stream)->buf_len == 0) { return 0; } - if (__IMPL(stream)->fd == -1) { - stream->buf_len = 0; + if (__FILE(stream)->fd == -1) { + __FILE(stream)->buf_len = 0; return 0; } - if (__IMPL(stream)->flags & _IO_ERR) { + if (__FILE(stream)->flags & _IO_ERR) { errno = EIO; return EOF; } - if ((__IMPL(stream)->flags & O_ACCMODE) == O_RDONLY) { + if ((__FILE(stream)->flags & O_ACCMODE) == O_RDONLY) { errno = EBADF; return EOF; } - LIBC_LOCK(__IMPL(stream)->lock); + LIBC_LOCK(__FILE(stream)->lock); - size_t bytes_to_write = __IMPL(stream)->buf_len; + size_t bytes_to_write = __FILE(stream)->buf_len; size_t total_written = 0; - char *buf_ptr = __IMPL(stream)->buf; + char *buf_ptr = __FILE(stream)->buf; while (total_written < bytes_to_write) { - ssize_t result = write(__IMPL(stream)->fd, + ssize_t result = write(__FILE(stream)->fd, buf_ptr + total_written, bytes_to_write - total_written); if (result < 0) { - __IMPL(stream)->flags |= _IO_ERR; - LIBC_UNLOCK(__IMPL(stream)->lock); + __FILE(stream)->flags |= _IO_ERR; + LIBC_UNLOCK(__FILE(stream)->lock); return EOF; } @@ -61,17 +60,17 @@ int fflush(FILE *stream) } if (total_written == bytes_to_write) { - __IMPL(stream)->buf_len = 0; - __IMPL(stream)->buf_pos = 0; + __FILE(stream)->buf_len = 0; + __FILE(stream)->buf_pos = 0; } else { size_t remaining = bytes_to_write - total_written; - memmove(__IMPL(stream)->buf, - __IMPL(stream)->buf + total_written, remaining); - __IMPL(stream)->buf_len = remaining; - __IMPL(stream)->buf_pos = 0; + memmove(__FILE(stream)->buf, + __FILE(stream)->buf + total_written, remaining); + __FILE(stream)->buf_len = remaining; + __FILE(stream)->buf_pos = 0; } - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_UNLOCK(__FILE(stream)->lock); return (total_written == bytes_to_write) ? 0 : EOF; } diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c index ad3eda7f..e27b56de 100644 --- a/lib/libc/stdio/fileno.c +++ b/lib/libc/stdio/fileno.c @@ -1,15 +1,15 @@ -#include // for LIBC_LOCK, LIBC_UNLOCK -#include // for EBADF, errno -#include // for __IMPL -#include // for FILE, fileno +#include <__stdio.h> // for __FILE +#include // for LIBC_LOCK, LIBC_UNLOCK +#include // for EBADF, errno +#include // for FILE, fileno int fileno(FILE *stream) { int fd; - LIBC_LOCK(__IMPL(stream)->lock); - fd = __IMPL(stream)->fd; - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_LOCK(__FILE(stream)->lock); + fd = __FILE(stream)->fd; + LIBC_UNLOCK(__FILE(stream)->lock); if (fd < 0) { errno = EBADF; diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c index 21e811fd..920484b3 100644 --- a/lib/libc/stdio/fmemopen.c +++ b/lib/libc/stdio/fmemopen.c @@ -1,10 +1,9 @@ -#include "__stdio.h" // for __libc_fadd +#include "__stdio.h" // for __FILE, __libc_fadd #include "features.h" // for __weak #include "stddef.h" // for NULL #include // for EINVAL, errno #include // for O_WRONLY, O_CREAT, O_RDONLY, O_APPEND, O_RDWR -#include // for __IMPL #include // for FILE, _IOFBF, fmemopen, size_t #include // for calloc, free #include // for strchr @@ -21,10 +20,10 @@ FILE *fmemopen(void *restrict buf, size_t max_size, const char *restrict mode) if (stream == NULL) return stream; - __IMPL(stream)->fd = -1; - __IMPL(stream)->buf = buf; - __IMPL(stream)->buf_size = max_size; - __IMPL(stream)->type = _IOFBF; + __FILE(stream)->fd = -1; + __FILE(stream)->buf = buf; + __FILE(stream)->buf_size = max_size; + __FILE(stream)->type = _IOFBF; if (mode[0] == 'r') { flags = O_RDONLY; @@ -42,7 +41,7 @@ FILE *fmemopen(void *restrict buf, size_t max_size, const char *restrict mode) flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; } - __IMPL(stream)->flags = flags; + __FILE(stream)->flags = flags; __libc_fadd(stream); diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c index a56b907d..bef21f46 100644 --- a/lib/libc/stdio/fopen.c +++ b/lib/libc/stdio/fopen.c @@ -1,11 +1,10 @@ -#include "__stdio.h" // for __libc_fadd +#include "__stdio.h" // for __FILE, __libc_fadd #include "features.h" // for __weak #include "stdatomic.h" // for atomic_flag_clear #include "stddef.h" // for NULL #include // for EINVAL, errno #include // for O_WRONLY, O_CREAT, O_RDONLY, open, O_APPEND -#include // for __IMPL #include // for FILE, BUFSIZ, fopen, _IOLBF #include // for calloc, free, malloc #include // for strchr @@ -43,13 +42,13 @@ FILE *fopen(const char *restrict pathname, const char *restrict mode) if ((stream = calloc(1, sizeof(FILE))) == NULL) return NULL; - __IMPL(stream)->fd = fd; - __IMPL(stream)->buf_size = BUFSIZ; - __IMPL(stream)->flags = flags; - __IMPL(stream)->type = _IOLBF; - atomic_flag_clear(&stream->lock); + __FILE(stream)->fd = fd; + __FILE(stream)->buf_size = BUFSIZ; + __FILE(stream)->flags = flags; + __FILE(stream)->type = _IOLBF; + atomic_flag_clear(&__FILE(stream)->lock); - if ((__IMPL(stream)->buf = malloc(BUFSIZ)) == NULL) { + if ((__FILE(stream)->buf = malloc(BUFSIZ)) == NULL) { close(fd); free(stream); return NULL; diff --git a/lib/libc/stdio/fputc.c b/lib/libc/stdio/fputc.c index 08ab0cfc..922011f2 100644 --- a/lib/libc/stdio/fputc.c +++ b/lib/libc/stdio/fputc.c @@ -1,8 +1,8 @@ #include "stddef.h" // for NULL -#include // for EINVAL, errno -#include // for __IMPL -#include // for EOF, fwrite, FILE, fputc +#include <__stdio.h> // for __FILE +#include // for EINVAL, errno +#include // for EOF, fwrite, FILE, fputc int fputc(int c, FILE *stream) { @@ -11,12 +11,13 @@ int fputc(int c, FILE *stream) return EOF; } - if (__IMPL(stream)->fd == -1 && __IMPL(stream)->buf != NULL) { - if (__IMPL(stream)->buf_len >= __IMPL(stream)->buf_size - 1) { + if ((__FILE(stream))->fd == -1 && (__FILE(stream))->buf != NULL) { + if ((__FILE(stream))->buf_len >= + (__FILE(stream))->buf_size - 1) { return EOF; } - __IMPL(stream)->buf[__IMPL(stream)->buf_len++] = (char)c; + (__FILE(stream))->buf[(__FILE(stream))->buf_len++] = (char)c; return (unsigned char)c; } diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index fb4ca3f6..443b4874 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -1,7 +1,6 @@ -#include "__stdio.h" // for _IO_EOF, _IO_ERR +#include "__stdio.h" // for __FILE, _IO_EOF, _IO_ERR #include // for LIBC_LOCK, LIBC_UNLOCK -#include // for __IMPL #include // for fread, BUFSIZ, FILE #include // for memcpy #include // for size_t, read, ssize_t @@ -18,38 +17,38 @@ size_t fread(void *restrict ptr, size_t size, size_t nitems, size_t bytes_read = 0; char *p = ptr; - LIBC_LOCK(__IMPL(stream)->lock); + LIBC_LOCK(__FILE(stream)->lock); while (total > 0) { - if (__IMPL(stream)->buf_pos < __IMPL(stream)->buf_len) { - size_t available = __IMPL(stream)->buf_len - - __IMPL(stream)->buf_pos; + if (__FILE(stream)->buf_pos < __FILE(stream)->buf_len) { + size_t available = __FILE(stream)->buf_len - + __FILE(stream)->buf_pos; size_t to_copy = total < available ? total : available; - memcpy(p, __IMPL(stream)->buf + __IMPL(stream)->buf_pos, + memcpy(p, __FILE(stream)->buf + __FILE(stream)->buf_pos, to_copy); - __IMPL(stream)->buf_pos += to_copy; + __FILE(stream)->buf_pos += to_copy; p += to_copy; bytes_read += to_copy; total -= to_copy; continue; } - ssize_t ret = read(__IMPL(stream)->fd, __IMPL(stream)->buf, - __IMPL(stream)->buf_size); + ssize_t ret = read(__FILE(stream)->fd, __FILE(stream)->buf, + __FILE(stream)->buf_size); if (ret <= 0) { if (ret < 0) - __IMPL(stream)->flags |= _IO_ERR; + __FILE(stream)->flags |= _IO_ERR; else - __IMPL(stream)->flags |= _IO_EOF; + __FILE(stream)->flags |= _IO_EOF; break; } - __IMPL(stream)->buf_len = ret; - __IMPL(stream)->buf_pos = 0; + __FILE(stream)->buf_len = ret; + __FILE(stream)->buf_pos = 0; } - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_UNLOCK(__FILE(stream)->lock); return bytes_read / size; } diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c index 0e425e8e..7a6754f7 100644 --- a/lib/libc/stdio/fseek.c +++ b/lib/libc/stdio/fseek.c @@ -1,31 +1,30 @@ -#include "__stdio.h" // for _IO_EOF, _IO_ERR +#include "__stdio.h" // for __FILE, _IO_EOF, _IO_ERR #include "stddef.h" // for NULL #include // for LIBC_LOCK, LIBC_UNLOCK -#include // for __IMPL #include // for FILE, fseek #include // for lseek, off_t int fseek(FILE *stream, long offset, int whence) { - if (stream == NULL || __IMPL(stream)->fd < 0) + if (stream == NULL || __FILE(stream)->fd < 0) return -1; - LIBC_LOCK(__IMPL(stream)->lock); + LIBC_LOCK(__FILE(stream)->lock); - __IMPL(stream)->buf_pos = 0; - __IMPL(stream)->buf_len = 0; - __IMPL(stream)->flags &= ~_IO_EOF; + __FILE(stream)->buf_pos = 0; + __FILE(stream)->buf_len = 0; + __FILE(stream)->flags &= ~_IO_EOF; - off_t result = lseek(__IMPL(stream)->fd, offset, whence); + off_t result = lseek(__FILE(stream)->fd, offset, whence); - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_UNLOCK(__FILE(stream)->lock); if (result == (off_t)-1) { - __IMPL(stream)->flags |= _IO_ERR; + __FILE(stream)->flags |= _IO_ERR; return -1; } - __IMPL(stream)->offset = result; + __FILE(stream)->offset = result; return 0; } diff --git a/lib/libc/stdio/fwrite.c b/lib/libc/stdio/fwrite.c index 4adbca81..c21054da 100644 --- a/lib/libc/stdio/fwrite.c +++ b/lib/libc/stdio/fwrite.c @@ -1,10 +1,9 @@ -#include "__stdio.h" // for _IO_ERR +#include "__stdio.h" // for __FILE, _IO_ERR #include "stddef.h" // for NULL #include // for LIBC_UNLOCK, LIBC_LOCK #include // for errno, EOVERFLOW, EBADF, EINVAL, EIO #include // for O_ACCMODE, O_RDONLY -#include // for __IMPL #include // for fflush, fwrite, BUFSIZ, FILE, _IOFBF, _IOLBF #include // for memcpy, memchr #include // for size_t, ssize_t, write @@ -27,16 +26,16 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, return 0; } - if (__IMPL(stream)->fd == -1 && __IMPL(stream)->buf != NULL) { + if (__FILE(stream)->fd == -1 && __FILE(stream)->buf != NULL) { if (__builtin_mul_overflow(size, nitems, &total_bytes)) { errno = EOVERFLOW; return 0; } size_t space_left = - __IMPL(stream)->buf_size > __IMPL(stream)->buf_len ? - __IMPL(stream)->buf_size - - __IMPL(stream)->buf_len - 1 : + __FILE(stream)->buf_size > __FILE(stream)->buf_len ? + __FILE(stream)->buf_size - + __FILE(stream)->buf_len - 1 : 0; if (space_left == 0) { @@ -47,9 +46,9 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, space_left; if (to_copy > 0) { - memcpy(__IMPL(stream)->buf + __IMPL(stream)->buf_len, + memcpy(__FILE(stream)->buf + __FILE(stream)->buf_len, data, to_copy); - __IMPL(stream)->buf_len += to_copy; + __FILE(stream)->buf_len += to_copy; } return to_copy == total_bytes ? nitems : to_copy / size; @@ -60,24 +59,24 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, return 0; } - if ((__IMPL(stream)->flags & O_ACCMODE) == O_RDONLY) { + if ((__FILE(stream)->flags & O_ACCMODE) == O_RDONLY) { errno = EBADF; return 0; } - if (__IMPL(stream)->flags & _IO_ERR) { + if (__FILE(stream)->flags & _IO_ERR) { errno = EIO; return 0; } - LIBC_LOCK(__IMPL(stream)->lock); + LIBC_LOCK(__FILE(stream)->lock); - if (__IMPL(stream)->type == _IONBF) { - ssize_t result = write(__IMPL(stream)->fd, data, total_bytes); - LIBC_UNLOCK(__IMPL(stream)->lock); + if (__FILE(stream)->type == _IONBF) { + ssize_t result = write(__FILE(stream)->fd, data, total_bytes); + LIBC_UNLOCK(__FILE(stream)->lock); if (result < 0) { - __IMPL(stream)->flags |= _IO_ERR; + __FILE(stream)->flags |= _IO_ERR; return 0; } @@ -88,48 +87,48 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, while (remaining > 0) { size_t space_available = - __IMPL(stream)->buf_size - __IMPL(stream)->buf_len; + __FILE(stream)->buf_size - __FILE(stream)->buf_len; if (space_available == 0) { - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_UNLOCK(__FILE(stream)->lock); if (fflush(stream) != 0) { return written / size; } - space_available = __IMPL(stream)->buf_size; + space_available = __FILE(stream)->buf_size; } size_t to_copy = remaining < space_available ? remaining : space_available; - memcpy(__IMPL(stream)->buf + __IMPL(stream)->buf_len, + memcpy(__FILE(stream)->buf + __FILE(stream)->buf_len, data + written, to_copy); - __IMPL(stream)->buf_len += to_copy; + __FILE(stream)->buf_len += to_copy; written += to_copy; remaining -= to_copy; - if (__IMPL(stream)->type == _IOLBF) { + if (__FILE(stream)->type == _IOLBF) { char *newline_pos = memchr( - __IMPL(stream)->buf + __IMPL(stream)->buf_len - + __FILE(stream)->buf + __FILE(stream)->buf_len - to_copy, '\n', to_copy); if (newline_pos != NULL) { - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_UNLOCK(__FILE(stream)->lock); if (fflush(stream) != 0) { return written / size; } } } - else if (__IMPL(stream)->type == _IOFBF && - __IMPL(stream)->buf_len == __IMPL(stream)->buf_size) { - LIBC_UNLOCK(__IMPL(stream)->lock); + else if (__FILE(stream)->type == _IOFBF && + __FILE(stream)->buf_len == __FILE(stream)->buf_size) { + LIBC_UNLOCK(__FILE(stream)->lock); if (fflush(stream) != 0) { return written / size; } } } - LIBC_UNLOCK(__IMPL(stream)->lock); + LIBC_UNLOCK(__FILE(stream)->lock); return written == total_bytes ? nitems : written / size; } diff --git a/lib/libc/stdio/pclose.c b/lib/libc/stdio/pclose.c index 241d4de0..a515981c 100644 --- a/lib/libc/stdio/pclose.c +++ b/lib/libc/stdio/pclose.c @@ -1,4 +1,4 @@ -#include // for __IMPL +#include <__stdio.h> // for __FILE #include // for fclose, FILE, pclose #include // for waitpid @@ -6,5 +6,5 @@ int pclose(FILE *stream) { int stat; fclose(stream); - return (waitpid(__IMPL(stream)->pid, &stat, 0) < 0) ? -1 : stat; + return (waitpid(__FILE(stream)->pid, &stat, 0) < 0) ? -1 : stat; } diff --git a/lib/libc/stdio/perror.c b/lib/libc/stdio/perror.c index 63c708a9..ad314038 100644 --- a/lib/libc/stdio/perror.c +++ b/lib/libc/stdio/perror.c @@ -1,5 +1,7 @@ +#include "stddef.h" // for NULL + #include // for errno -#include // for NULL, perror +#include // for perror #include // for strlen, strerror #include // for iovec, writev #include // for STDERR_FILENO diff --git a/lib/libc/stdio/popen.c b/lib/libc/stdio/popen.c index 09c931ad..1fd5f991 100644 --- a/lib/libc/stdio/popen.c +++ b/lib/libc/stdio/popen.c @@ -1,10 +1,10 @@ #include "stddef.h" // for NULL -#include // for EINVAL, errno -#include // for O_RDONLY, O_CLOEXEC, O_WRONLY -#include // for __IMPL -#include // for FILE, fclose, fdopen, popen -#include // for close, dup2, _exit, execl, fork, pipe2, STDIN_FI... +#include <__stdio.h> // for __FILE +#include // for EINVAL, errno +#include // for O_RDONLY, O_CLOEXEC, O_WRONLY +#include // for FILE, fclose, fdopen, popen +#include // for close, dup2, _exit, execl, fork, pipe2, STDIN_F... FILE *popen(const char *command, const char *mode) { @@ -41,7 +41,9 @@ FILE *popen(const char *command, const char *mode) close(pipefd[1]); fclose(stream); return NULL; - } else if (pid == 0) { + } + + if (pid == 0) { if (oflag == O_RDONLY) { dup2(pipefd[1], STDOUT_FILENO); close(pipefd[0]); @@ -51,7 +53,7 @@ FILE *popen(const char *command, const char *mode) close(pipefd[0]); close(pipefd[1]); } - execl("/bin/sh", "sh", "-c", command, (char *)NULL); + execl("/bin/sh", "sh", "-c", command, (char *)0); _exit(127); } else { if (oflag == O_RDONLY) { @@ -60,7 +62,7 @@ FILE *popen(const char *command, const char *mode) close(pipefd[0]); } - __IMPL(stream)->pid = pid; + __FILE(stream)->pid = pid; return stream; } diff --git a/lib/libc/stdio/remove.c b/lib/libc/stdio/remove.c index 25bbd3bd..9afd47b4 100644 --- a/lib/libc/stdio/remove.c +++ b/lib/libc/stdio/remove.c @@ -1,4 +1,3 @@ -#include "asm/unistd_64.h" // for __NR_unlinkat #include "errno.h" // for EISDIR, errno #include // for AT_FDCWD, AT_REMOVEDIR @@ -10,9 +9,8 @@ int remove(const char *path) if (syscall(unlinkat, AT_FDCWD, path, 0) < 0) { if (errno == EISDIR) { return syscall(unlinkat, AT_FDCWD, path, AT_REMOVEDIR); - } else { - return -1; } + return -1; } return 0; diff --git a/lib/libc/stdio/rename.c b/lib/libc/stdio/rename.c index 8857f852..52426ab0 100644 --- a/lib/libc/stdio/rename.c +++ b/lib/libc/stdio/rename.c @@ -1,5 +1,7 @@ -#include "asm/unistd_64.h" // for __NR_rename + + +#include // for rename #include // for __syscall_2, syscall int rename(const char *old, const char *new) diff --git a/lib/libc/stdio/renameat.c b/lib/libc/stdio/renameat.c index fde966c4..c7e9a683 100644 --- a/lib/libc/stdio/renameat.c +++ b/lib/libc/stdio/renameat.c @@ -1,4 +1,5 @@ -#include "asm/unistd_64.h" // for __NR_renameat + + #include // for __syscall_4, syscall diff --git a/lib/libc/stdio/setbuf.c b/lib/libc/stdio/setbuf.c index ddddf4ac..48d15c51 100644 --- a/lib/libc/stdio/setbuf.c +++ b/lib/libc/stdio/setbuf.c @@ -1,4 +1,6 @@ -#include // for setvbuf, BUFSIZ, FILE, NULL, _IOFBF, _IONBF, setbuf +#include "stddef.h" // for NULL + +#include // for setvbuf, BUFSIZ, FILE, _IOFBF, _IONBF, setbuf void setbuf(FILE *restrict stream, char *restrict buf) { diff --git a/lib/libc/stdio/setvbuf.c b/lib/libc/stdio/setvbuf.c index 02928a35..686640f4 100644 --- a/lib/libc/stdio/setvbuf.c +++ b/lib/libc/stdio/setvbuf.c @@ -1,7 +1,7 @@ #include "stddef.h" // for NULL -#include // for __IMPL -#include // for _IONBF, FILE, _IOFBF, _IOLBF, setvbuf, size_t +#include <__stdio.h> // for __FILE +#include // for _IONBF, FILE, _IOFBF, _IOLBF, setvbuf, size_t int setvbuf(FILE *restrict stream, char *restrict buf, int type, size_t size) { @@ -10,13 +10,13 @@ int setvbuf(FILE *restrict stream, char *restrict buf, int type, size_t size) if (type != _IONBF && (buf == NULL || size == 0)) return -1; - if (__IMPL(stream)->fd < 0) + if (__FILE(stream)->fd < 0) return -1; - __IMPL(stream)->buf = buf; - __IMPL(stream)->buf_size = size; - __IMPL(stream)->buf_pos = 0; - __IMPL(stream)->type = type; + __FILE(stream)->buf = buf; + __FILE(stream)->buf_size = size; + __FILE(stream)->buf_pos = 0; + __FILE(stream)->type = type; return 0; } diff --git a/lib/libc/stdio/stderr.c b/lib/libc/stdio/stderr.c index 155f0e3e..ec115fd0 100644 --- a/lib/libc/stdio/stderr.c +++ b/lib/libc/stdio/stderr.c @@ -2,6 +2,7 @@ #include "stdatomic.h" // for ATOMIC_FLAG_INIT #include // for O_WRONLY +#include // for FILE, stderr #include // for STDERR_FILENO #define BUFSIZ 4096 @@ -19,4 +20,4 @@ struct __FILE __stderr = { .fd = STDERR_FILENO, .offset = 0, .lock = ATOMIC_FLAG_INIT }; -struct FILE *const stderr = (struct FILE *)&__stderr; +FILE *const stderr = (FILE *)&__stderr; diff --git a/lib/libc/stdio/stdin.c b/lib/libc/stdio/stdin.c index b4194704..45d42e27 100644 --- a/lib/libc/stdio/stdin.c +++ b/lib/libc/stdio/stdin.c @@ -2,10 +2,10 @@ #include "features.h" // for __weak #include // for O_RDONLY +#include // for FILE, stdin #include // for STDOUT_FILENO #define BUFSIZ 4096 -#define _IOLBF 0x1 __weak char __stdin_buffer[0]; struct __FILE __stdin = { .fd = STDOUT_FILENO, @@ -16,4 +16,4 @@ struct __FILE __stdin = { .fd = STDOUT_FILENO, .buf_pos = 0, .offset = 0 }; -struct FILE *const stdin = (struct FILE *)&__stdin; +FILE *const stdin = (FILE *)&__stdin; diff --git a/lib/libc/stdio/stdout.c b/lib/libc/stdio/stdout.c index a21125c3..14637368 100644 --- a/lib/libc/stdio/stdout.c +++ b/lib/libc/stdio/stdout.c @@ -5,6 +5,7 @@ #include // for LIBC_LOCK, LIBC_UNLOCK #include // for O_WRONLY #include // for NULL +#include // for stdout #include // for STDOUT_FILENO #define BUFSIZ 4096 -- cgit v1.2.3