summaryrefslogtreecommitdiff
path: root/lib/libc/stdio
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdio')
-rw-r--r--lib/libc/stdio/asprintf.c4
-rw-r--r--lib/libc/stdio/clearerr.c9
-rw-r--r--lib/libc/stdio/dprintf.c4
-rw-r--r--lib/libc/stdio/dtoa.c36
-rw-r--r--lib/libc/stdio/fclose.c8
-rw-r--r--lib/libc/stdio/fdopen.c34
-rw-r--r--lib/libc/stdio/feof.c10
-rw-r--r--lib/libc/stdio/ferror.c11
-rw-r--r--lib/libc/stdio/fflush.c64
-rw-r--r--lib/libc/stdio/fgetc.c2
-rw-r--r--lib/libc/stdio/fgets.c6
-rw-r--r--lib/libc/stdio/fileno.c14
-rw-r--r--lib/libc/stdio/fmemopen.c39
-rw-r--r--lib/libc/stdio/fopen.c43
-rw-r--r--lib/libc/stdio/fprintf.c4
-rw-r--r--lib/libc/stdio/fputc.c21
-rw-r--r--lib/libc/stdio/fputs.c4
-rw-r--r--lib/libc/stdio/fread.c37
-rw-r--r--lib/libc/stdio/fseek.c29
-rw-r--r--lib/libc/stdio/ftell.c7
-rw-r--r--lib/libc/stdio/ftello.c6
-rw-r--r--lib/libc/stdio/fwrite.c100
-rw-r--r--lib/libc/stdio/getc.c4
-rw-r--r--lib/libc/stdio/getchar.c4
-rw-r--r--lib/libc/stdio/pclose.c8
-rw-r--r--lib/libc/stdio/perror.c10
-rw-r--r--lib/libc/stdio/popen.c14
-rw-r--r--lib/libc/stdio/printf.c4
-rw-r--r--lib/libc/stdio/putc.c4
-rw-r--r--lib/libc/stdio/putchar.c4
-rw-r--r--lib/libc/stdio/puts.c4
-rw-r--r--lib/libc/stdio/remove.c9
-rw-r--r--lib/libc/stdio/rename.c4
-rw-r--r--lib/libc/stdio/renameat.c4
-rw-r--r--lib/libc/stdio/rewind.c2
-rw-r--r--lib/libc/stdio/setbuf.c3
-rw-r--r--lib/libc/stdio/setvbuf.c16
-rw-r--r--lib/libc/stdio/snprintf.c4
-rw-r--r--lib/libc/stdio/sprintf.c5
-rw-r--r--lib/libc/stdio/stderr.c8
-rw-r--r--lib/libc/stdio/stdin.c9
-rw-r--r--lib/libc/stdio/stdout.c13
-rw-r--r--lib/libc/stdio/vasprintf.c6
-rw-r--r--lib/libc/stdio/vdprintf.c16
-rw-r--r--lib/libc/stdio/vfprintf.c20
-rw-r--r--lib/libc/stdio/vprintf.c2
-rw-r--r--lib/libc/stdio/vsnprintf.c53
-rw-r--r--lib/libc/stdio/vsprintf.c3
48 files changed, 375 insertions, 350 deletions
diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c
index b868cce1..c82ef019 100644
--- a/lib/libc/stdio/asprintf.c
+++ b/lib/libc/stdio/asprintf.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <stdarg.h>
+#include <stdarg.h> // for va_end, va_start
+#include <stdio.h> // for vasprintf, asprintf, va_list
int asprintf(char **restrict ptr, const char *restrict format, ...)
{
diff --git a/lib/libc/stdio/clearerr.c b/lib/libc/stdio/clearerr.c
index a49e5c2d..87d733ae 100644
--- a/lib/libc/stdio/clearerr.c
+++ b/lib/libc/stdio/clearerr.c
@@ -1,10 +1,13 @@
-#include <stdio.h>
-#include <io.h>
+#include "__stdio.h" // for _IO_EOF, _IO_ERR
+#include "stddef.h" // for NULL
+
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, clearerr
void clearerr(FILE *stream)
{
if (stream == NULL)
return;
- stream->flags &= ~(_IO_ERR | _IO_EOF);
+ __IMPL(stream)->flags &= ~(_IO_ERR | _IO_EOF);
}
diff --git a/lib/libc/stdio/dprintf.c b/lib/libc/stdio/dprintf.c
index 10877b22..f9a73654 100644
--- a/lib/libc/stdio/dprintf.c
+++ b/lib/libc/stdio/dprintf.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <stdarg.h>
+#include <stdarg.h> // for va_end, va_start
+#include <stdio.h> // for vdprintf, dprintf, va_list
int dprintf(int fildes, const char *restrict format, ...)
{
diff --git a/lib/libc/stdio/dtoa.c b/lib/libc/stdio/dtoa.c
index 9b0eea99..faf4bd8c 100644
--- a/lib/libc/stdio/dtoa.c
+++ b/lib/libc/stdio/dtoa.c
@@ -210,17 +210,15 @@
* used for input more than STRTOD_DIGLIM digits long (default 40).
*/
-#include <thread.h>
-#include <threads.h>
-#include <atomic.h>
+#include <atomic.h> // for LIBC_LOCK, LIBC_UNLOCK
+#include <libc.h> // for __IMPL
+#include <threads.h> // for thrd_current
static atomic_flag dtoa_lock[2] = { ATOMIC_FLAG_INIT, ATOMIC_FLAG_INIT };
#define ACQUIRE_DTOA_LOCK(n) LIBC_LOCK(dtoa_lock[(n)])
#define FREE_DTOA_LOCK(n) LIBC_UNLOCK(dtoa_lock[(n)])
-#define dtoa_get_threadno() thrd_current()->tid
-
#define IEEE_8087
#define Honor_FLT_ROUNDS
#define MULTIPLE_THREADS
@@ -233,8 +231,10 @@ typedef unsigned Long ULong;
#endif
#ifdef DEBUG
-#include <assert.h>
#include "stdio.h"
+
+#include <assert.h>
+
#define Bug(x) \
{ \
fprintf(stderr, "%s\n", x); \
@@ -247,8 +247,11 @@ int dtoa_stats[7]; /* strtod_{64,96,bigcomp},dtoa_{exact,64,96,bigcomp} */
#define Debug(x) /*nothing*/
#endif
-#include "stdlib.h"
-#include "string.h"
+#include "__stdio.h" // for size_t
+#include "bits/fenv.h" // for FE_DOWNWARD, FE_TOWARDZERO, FE_UPWARD
+#include "stdatomic.h" // for ATOMIC_FLAG_INIT, atomic_flag
+#include "stdlib.h" // for malloc, free, realloc, strtod
+#include "string.h" // for memcpy, memset
#ifdef USE_LOCALE
#include "locale.h"
@@ -256,7 +259,7 @@ int dtoa_stats[7]; /* strtod_{64,96,bigcomp},dtoa_{exact,64,96,bigcomp} */
#ifdef Honor_FLT_ROUNDS
#ifndef Trust_FLT_ROUNDS
-#include <fenv.h>
+#include <fenv.h> // for fegetround
#endif
#endif
@@ -310,7 +313,7 @@ static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
#define NO_STRTOD_BIGCOMP
#endif
-#include "errno.h"
+#include "errno.h" // for errno, ERANGE
#ifdef NO_ERRNO /*{*/
#undef Set_errno
@@ -350,13 +353,9 @@ static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
#define LONG_MAX 2147483647
#endif
-#else /* ifndef Bad_float_h */
-#include "float.h"
-#endif /* Bad_float_h */
-
-#ifndef __MATH_H__
-#include "math.h"
-#endif
+#else /* ifndef Bad_float_h */
+#include "float.h" // for DBL_MAX_EXP, FLT_RADIX, DBL_DIG, FLT_ROUNDS
+#endif /* Bad_float_h */
#ifdef __cplusplus
extern "C" {
@@ -1557,7 +1556,8 @@ void set_max_dtoa_threads(unsigned int n)
static ThInfo *get_TI(void)
{
- unsigned int thno = dtoa_get_threadno();
+ unsigned int thno = __IMPL(thrd_current())->tid;
+
if (thno < maxthreads)
return TI1 + thno;
if (thno == 0)
diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c
index 8f0b4cef..8b0586c6 100644
--- a/lib/libc/stdio/fclose.c
+++ b/lib/libc/stdio/fclose.c
@@ -1,6 +1,6 @@
-#include <io.h>
-#include <stdio.h>
-#include <unistd.h>
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for fflush, FILE, fclose, stderr, stdin, stdout
+#include <unistd.h> // 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(stream->fd) == -1)
+ if (close(__IMPL(stream)->fd) == -1)
return -1;
}
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c
index 23f94843..f762a83d 100644
--- a/lib/libc/stdio/fdopen.c
+++ b/lib/libc/stdio/fdopen.c
@@ -1,7 +1,12 @@
-#include <io.h>
-#include <libc.h>
-#include <unistd.h>
-#include <stdio.h>
+#include "__stdio.h" // for __libc_fadd
+#include "features.h" // for __weak
+#include "stdatomic.h" // for atomic_flag_clear
+#include "stddef.h" // for NULL
+
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, _IONBF, SEEK_END, _IOLBF, fdopen
+#include <stdlib.h> // for calloc, free
+#include <unistd.h> // for lseek, off_t
__weak void __stdio_cleanup(void)
{
@@ -9,31 +14,32 @@ __weak void __stdio_cleanup(void)
FILE *fdopen(int fildes, const char *mode)
{
- FILE *fp;
+ FILE *stream;
if (mode == NULL ||
(mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a')) {
return NULL;
}
- if ((fp = calloc(1, sizeof(FILE))) == NULL)
+ if ((stream = calloc(1, sizeof(FILE))) == NULL)
return NULL;
- fp->fd = fildes;
- atomic_flag_clear(&fp->lock);
+ __IMPL(stream)->fd = fildes;
+ atomic_flag_clear(&__IMPL(stream)->lock);
if (mode[0] == 'r') {
- fp->type = _IONBF;
+ __IMPL(stream)->type = _IONBF;
} else if (mode[0] == 'w') {
- fp->type = _IOLBF;
+ __IMPL(stream)->type = _IOLBF;
} else if (mode[0] == 'a') {
- fp->type = _IONBF;
+ __IMPL(stream)->type = _IONBF;
+
off_t offset = lseek(fildes, 0, SEEK_END);
if (offset == (off_t)-1) {
- free(fp);
+ free(stream);
return NULL;
}
}
- __libc_fadd(fp);
- return fp;
+ __libc_fadd(stream);
+ return stream;
}
diff --git a/lib/libc/stdio/feof.c b/lib/libc/stdio/feof.c
index 0b3eb1ec..f8478d03 100644
--- a/lib/libc/stdio/feof.c
+++ b/lib/libc/stdio/feof.c
@@ -1,10 +1,12 @@
-#include <stdio.h>
-#include <io.h>
+#include "stddef.h" // for NULL
+
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, feof
int feof(FILE *stream)
{
- if (!stream)
+ if (stream == NULL)
return 0;
- return stream->eof;
+ return __IMPL(stream)->eof;
}
diff --git a/lib/libc/stdio/ferror.c b/lib/libc/stdio/ferror.c
index b4e438b3..69ef439d 100644
--- a/lib/libc/stdio/ferror.c
+++ b/lib/libc/stdio/ferror.c
@@ -1,10 +1,13 @@
-#include <stdio.h>
-#include <io.h>
+#include "__stdio.h" // for _IO_ERR
+#include "stddef.h" // for NULL
+
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, ferror
int ferror(FILE *stream)
{
- if (!stream)
+ if (stream == NULL)
return 0;
- return (stream->flags & _IO_ERR) != 0;
+ return (__IMPL(stream)->flags & _IO_ERR) != 0;
}
diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c
index 9af9ca2d..001e3139 100644
--- a/lib/libc/stdio/fflush.c
+++ b/lib/libc/stdio/fflush.c
@@ -1,85 +1,77 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <atomic.h>
-#include <io.h>
-#include <errno.h>
-#include <string.h>
-#include <fcntl.h>
+#include "__stdio.h" // for _IO_ERR
+#include "stddef.h" // for NULL
+
+#include <atomic.h> // for LIBC_UNLOCK, LIBC_LOCK
+#include <errno.h> // for errno, EBADF, EIO
+#include <fcntl.h> // for O_ACCMODE, O_RDONLY
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for EOF, FILE, fflush
+#include <string.h> // for memmove
+#include <unistd.h> // for size_t, write, ssize_t
int fflush(FILE *stream)
{
- // Handle NULL stream - flush all open streams
if (stream == NULL) {
// TODO: Implement flushing all open streams
// For now, just return success
return 0;
}
- // Nothing to flush if buffer is empty
- if (stream->buf_len == 0) {
+ if (__IMPL(stream)->buf_len == 0) {
return 0;
}
- // Handle special case of invalid file descriptor
- if (stream->fd == -1) {
+ if (__IMPL(stream)->fd == -1) {
stream->buf_len = 0;
return 0;
}
- // Check if stream is in error state
- if (stream->flags & _IO_ERR) {
+ if (__IMPL(stream)->flags & _IO_ERR) {
errno = EIO;
return EOF;
}
- // Check if stream is writable
- if ((stream->flags & O_ACCMODE) == O_RDONLY) {
+ if ((__IMPL(stream)->flags & O_ACCMODE) == O_RDONLY) {
errno = EBADF;
return EOF;
}
- LIBC_LOCK(stream->lock);
+ LIBC_LOCK(__IMPL(stream)->lock);
- size_t bytes_to_write = stream->buf_len;
+ size_t bytes_to_write = __IMPL(stream)->buf_len;
size_t total_written = 0;
- char *buf_ptr = stream->buf;
+ char *buf_ptr = __IMPL(stream)->buf;
- // Write all buffered data
while (total_written < bytes_to_write) {
- ssize_t result = write(stream->fd, buf_ptr + total_written,
+ ssize_t result = write(__IMPL(stream)->fd,
+ buf_ptr + total_written,
bytes_to_write - total_written);
if (result < 0) {
- // Write error occurred
- stream->flags |= _IO_ERR;
- LIBC_UNLOCK(stream->lock);
+ __IMPL(stream)->flags |= _IO_ERR;
+ LIBC_UNLOCK(__IMPL(stream)->lock);
return EOF;
}
if (result == 0) {
- // No progress made (shouldn't happen with regular
- // files)
break;
}
total_written += result;
}
- // Update buffer state
if (total_written == bytes_to_write) {
- // All data was written successfully
- stream->buf_len = 0;
- stream->buf_pos = 0;
+ __IMPL(stream)->buf_len = 0;
+ __IMPL(stream)->buf_pos = 0;
} else {
- // Partial write - move remaining data to beginning of buffer
size_t remaining = bytes_to_write - total_written;
- memmove(stream->buf, stream->buf + total_written, remaining);
- stream->buf_len = remaining;
- stream->buf_pos = 0;
+ memmove(__IMPL(stream)->buf,
+ __IMPL(stream)->buf + total_written, remaining);
+ __IMPL(stream)->buf_len = remaining;
+ __IMPL(stream)->buf_pos = 0;
}
- LIBC_UNLOCK(stream->lock);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
- // Return success if all data was written, error otherwise
return (total_written == bytes_to_write) ? 0 : EOF;
}
diff --git a/lib/libc/stdio/fgetc.c b/lib/libc/stdio/fgetc.c
index 658eaffe..a7612f42 100644
--- a/lib/libc/stdio/fgetc.c
+++ b/lib/libc/stdio/fgetc.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#include <stdio.h> // for fread, FILE, fgetc
int fgetc(FILE *stream)
{
diff --git a/lib/libc/stdio/fgets.c b/lib/libc/stdio/fgets.c
index 75ffb064..adeae1b4 100644
--- a/lib/libc/stdio/fgets.c
+++ b/lib/libc/stdio/fgets.c
@@ -1,5 +1,7 @@
-#include <libc.h>
-#include <stdio.h>
+#include "stddef.h" // for NULL
+
+#include <libc.h> // for weak_reference
+#include <stdio.h> // for fread, FILE, fgets
char *fgets(char *restrict s, int n, FILE *restrict stream)
{
diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c
index 09db0ad5..ad3eda7f 100644
--- a/lib/libc/stdio/fileno.c
+++ b/lib/libc/stdio/fileno.c
@@ -1,15 +1,15 @@
-#include <io.h>
-#include <errno.h>
-#include <atomic.h>
-#include <stdio.h>
+#include <atomic.h> // for LIBC_LOCK, LIBC_UNLOCK
+#include <errno.h> // for EBADF, errno
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, fileno
int fileno(FILE *stream)
{
int fd;
- LIBC_LOCK(stream->lock);
- fd = stream->fd;
- LIBC_UNLOCK(stream->lock);
+ LIBC_LOCK(__IMPL(stream)->lock);
+ fd = __IMPL(stream)->fd;
+ LIBC_UNLOCK(__IMPL(stream)->lock);
if (fd < 0) {
errno = EBADF;
diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c
index b1fa0f8a..21e811fd 100644
--- a/lib/libc/stdio/fmemopen.c
+++ b/lib/libc/stdio/fmemopen.c
@@ -1,10 +1,13 @@
-#include <io.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <libc.h>
+#include "__stdio.h" // for __libc_fadd
+#include "features.h" // for __weak
+#include "stddef.h" // for NULL
+
+#include <errno.h> // for EINVAL, errno
+#include <fcntl.h> // for O_WRONLY, O_CREAT, O_RDONLY, O_APPEND, O_RDWR
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, _IOFBF, fmemopen, size_t
+#include <stdlib.h> // for calloc, free
+#include <string.h> // for strchr
__weak void __stdio_cleanup(void)
{
@@ -13,15 +16,15 @@ __weak void __stdio_cleanup(void)
FILE *fmemopen(void *restrict buf, size_t max_size, const char *restrict mode)
{
int flags;
- FILE *f = calloc(1, sizeof(FILE));
+ FILE *stream = calloc(1, sizeof(FILE));
- if (f == NULL)
- return f;
+ if (stream == NULL)
+ return stream;
- f->fd = -1;
- f->buf = buf;
- f->buf_size = max_size;
- f->type = _IOFBF;
+ __IMPL(stream)->fd = -1;
+ __IMPL(stream)->buf = buf;
+ __IMPL(stream)->buf_size = max_size;
+ __IMPL(stream)->type = _IOFBF;
if (mode[0] == 'r') {
flags = O_RDONLY;
@@ -30,7 +33,7 @@ FILE *fmemopen(void *restrict buf, size_t max_size, const char *restrict mode)
} else if (mode[0] == 'a') {
flags = O_WRONLY | O_CREAT | O_APPEND;
} else {
- free(f);
+ free(stream);
errno = EINVAL;
return NULL;
}
@@ -39,9 +42,9 @@ FILE *fmemopen(void *restrict buf, size_t max_size, const char *restrict mode)
flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
}
- f->flags = flags;
+ __IMPL(stream)->flags = flags;
- __libc_fadd(f);
+ __libc_fadd(stream);
- return f;
+ return stream;
}
diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c
index 1f15820b..a56b907d 100644
--- a/lib/libc/stdio/fopen.c
+++ b/lib/libc/stdio/fopen.c
@@ -1,11 +1,15 @@
-#include <io.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <libc.h>
+#include "__stdio.h" // for __libc_fadd
+#include "features.h" // for __weak
+#include "stdatomic.h" // for atomic_flag_clear
+#include "stddef.h" // for NULL
+
+#include <errno.h> // for EINVAL, errno
+#include <fcntl.h> // for O_WRONLY, O_CREAT, O_RDONLY, open, O_APPEND
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, BUFSIZ, fopen, _IOLBF
+#include <stdlib.h> // for calloc, free, malloc
+#include <string.h> // for strchr
+#include <unistd.h> // for close
__weak void __stdio_cleanup(void)
{
@@ -14,7 +18,7 @@ __weak void __stdio_cleanup(void)
FILE *fopen(const char *restrict pathname, const char *restrict mode)
{
int fd, flags, _mode;
- FILE *fp;
+ FILE *stream;
_mode = 0;
if (mode[0] == 'r') {
@@ -36,21 +40,22 @@ FILE *fopen(const char *restrict pathname, const char *restrict mode)
if ((fd = open(pathname, flags, _mode)) < 0)
return NULL;
- if ((fp = calloc(1, sizeof(FILE))) == NULL)
+ if ((stream = calloc(1, sizeof(FILE))) == NULL)
return NULL;
- fp->fd = fd;
- fp->buf_size = BUFSIZ;
- fp->flags = flags;
- fp->type = _IOLBF;
- atomic_flag_clear(&fp->lock);
- if ((fp->buf = malloc(BUFSIZ)) == NULL) {
+ __IMPL(stream)->fd = fd;
+ __IMPL(stream)->buf_size = BUFSIZ;
+ __IMPL(stream)->flags = flags;
+ __IMPL(stream)->type = _IOLBF;
+ atomic_flag_clear(&stream->lock);
+
+ if ((__IMPL(stream)->buf = malloc(BUFSIZ)) == NULL) {
close(fd);
- free(fp);
+ free(stream);
return NULL;
}
- __libc_fadd(fp);
+ __libc_fadd(stream);
- return fp;
+ return stream;
}
diff --git a/lib/libc/stdio/fprintf.c b/lib/libc/stdio/fprintf.c
index c0d14a48..c7f31f34 100644
--- a/lib/libc/stdio/fprintf.c
+++ b/lib/libc/stdio/fprintf.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <stdarg.h>
+#include <stdarg.h> // for va_end, va_start
+#include <stdio.h> // for fprintf, vfprintf, FILE, va_list
int fprintf(FILE *restrict stream, const char *restrict format, ...)
{
diff --git a/lib/libc/stdio/fputc.c b/lib/libc/stdio/fputc.c
index c544d5dd..08ab0cfc 100644
--- a/lib/libc/stdio/fputc.c
+++ b/lib/libc/stdio/fputc.c
@@ -1,27 +1,24 @@
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <io.h>
+#include "stddef.h" // for NULL
+
+#include <errno.h> // for EINVAL, errno
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for EOF, fwrite, FILE, fputc
int fputc(int c, FILE *stream)
{
- if (!stream) {
+ if (stream == NULL) {
errno = EINVAL;
return EOF;
}
- // Special case for string buffer operations (snprintf)
- // When fd is -1, we're writing to a string buffer
- if (stream->fd == -1 && stream->buf != NULL) {
- // Check if there's space (leave room for null terminator)
- if (stream->buf_len >= stream->buf_size - 1) {
+ if (__IMPL(stream)->fd == -1 && __IMPL(stream)->buf != NULL) {
+ if (__IMPL(stream)->buf_len >= __IMPL(stream)->buf_size - 1) {
return EOF;
}
- stream->buf[stream->buf_len++] = (char)c;
+ __IMPL(stream)->buf[__IMPL(stream)->buf_len++] = (char)c;
return (unsigned char)c;
}
- // For regular file operations, use fwrite
return fwrite(&c, 1, 1, stream) ? c : EOF;
}
diff --git a/lib/libc/stdio/fputs.c b/lib/libc/stdio/fputs.c
index 8538b9c2..2ba0d4d1 100644
--- a/lib/libc/stdio/fputs.c
+++ b/lib/libc/stdio/fputs.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <string.h>
+#include <stdio.h> // for fwrite, EOF, FILE, fputs
+#include <string.h> // for strlen
int fputs(const char *restrict s, FILE *restrict stream)
{
diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c
index a27b64c4..fb4ca3f6 100644
--- a/lib/libc/stdio/fread.c
+++ b/lib/libc/stdio/fread.c
@@ -1,8 +1,10 @@
-#include <io.h>
-#include <unistd.h>
-#include <string.h>
-#include <atomic.h>
-#include <stdio.h>
+#include "__stdio.h" // for _IO_EOF, _IO_ERR
+
+#include <atomic.h> // for LIBC_LOCK, LIBC_UNLOCK
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for fread, BUFSIZ, FILE
+#include <string.h> // for memcpy
+#include <unistd.h> // for size_t, read, ssize_t
char __stdin_buffer[BUFSIZ];
@@ -16,35 +18,38 @@ size_t fread(void *restrict ptr, size_t size, size_t nitems,
size_t bytes_read = 0;
char *p = ptr;
- LIBC_LOCK(stream->lock);
+ LIBC_LOCK(__IMPL(stream)->lock);
while (total > 0) {
- if (stream->buf_pos < stream->buf_len) {
- size_t available = stream->buf_len - stream->buf_pos;
+ if (__IMPL(stream)->buf_pos < __IMPL(stream)->buf_len) {
+ size_t available = __IMPL(stream)->buf_len -
+ __IMPL(stream)->buf_pos;
size_t to_copy = total < available ? total : available;
- memcpy(p, stream->buf + stream->buf_pos, to_copy);
- stream->buf_pos += to_copy;
+ memcpy(p, __IMPL(stream)->buf + __IMPL(stream)->buf_pos,
+ to_copy);
+ __IMPL(stream)->buf_pos += to_copy;
p += to_copy;
bytes_read += to_copy;
total -= to_copy;
continue;
}
- ssize_t ret = read(stream->fd, stream->buf, stream->buf_size);
+ ssize_t ret = read(__IMPL(stream)->fd, __IMPL(stream)->buf,
+ __IMPL(stream)->buf_size);
if (ret <= 0) {
if (ret < 0)
- stream->flags |= _IO_ERR;
+ __IMPL(stream)->flags |= _IO_ERR;
else
- stream->flags |= _IO_EOF;
+ __IMPL(stream)->flags |= _IO_EOF;
break;
}
- stream->buf_len = ret;
- stream->buf_pos = 0;
+ __IMPL(stream)->buf_len = ret;
+ __IMPL(stream)->buf_pos = 0;
}
- LIBC_UNLOCK(stream->lock);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
return bytes_read / size;
}
diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c
index af12cd2c..0e425e8e 100644
--- a/lib/libc/stdio/fseek.c
+++ b/lib/libc/stdio/fseek.c
@@ -1,28 +1,31 @@
-#include <unistd.h>
-#include <io.h>
-#include <stdio.h>
-#include <atomic.h>
+#include "__stdio.h" // for _IO_EOF, _IO_ERR
+#include "stddef.h" // for NULL
+
+#include <atomic.h> // for LIBC_LOCK, LIBC_UNLOCK
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, fseek
+#include <unistd.h> // for lseek, off_t
int fseek(FILE *stream, long offset, int whence)
{
- if (stream == NULL || stream->fd < 0)
+ if (stream == NULL || __IMPL(stream)->fd < 0)
return -1;
- LIBC_LOCK(stream->lock);
+ LIBC_LOCK(__IMPL(stream)->lock);
- stream->buf_pos = 0;
- stream->buf_len = 0;
- stream->flags &= ~_IO_EOF;
+ __IMPL(stream)->buf_pos = 0;
+ __IMPL(stream)->buf_len = 0;
+ __IMPL(stream)->flags &= ~_IO_EOF;
- off_t result = lseek(stream->fd, offset, whence);
+ off_t result = lseek(__IMPL(stream)->fd, offset, whence);
- LIBC_UNLOCK(stream->lock);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
if (result == (off_t)-1) {
- stream->flags |= _IO_ERR;
+ __IMPL(stream)->flags |= _IO_ERR;
return -1;
}
- stream->offset = result;
+ __IMPL(stream)->offset = result;
return 0;
}
diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c
index c28fe143..d26ba071 100644
--- a/lib/libc/stdio/ftell.c
+++ b/lib/libc/stdio/ftell.c
@@ -1,6 +1,7 @@
-#include <stdio.h>
-#include <limits.h>
-#include <errno.h>
+#include <errno.h> // for EOVERFLOW, errno
+#include <limits.h> // for LONG_MAX
+#include <stdio.h> // for ftello, FILE, ftell
+#include <sys/types.h> // for off_t
long ftell(FILE *stream)
{
diff --git a/lib/libc/stdio/ftello.c b/lib/libc/stdio/ftello.c
index d0e5dfa1..d69008cf 100644
--- a/lib/libc/stdio/ftello.c
+++ b/lib/libc/stdio/ftello.c
@@ -1,7 +1,5 @@
-#include <io.h>
-#include <atomic.h>
-#include <stdio.h>
-#include <unistd.h>
+#include <stdio.h> // for fseek, FILE, SEEK_CUR, ftello
+#include <sys/types.h> // for off_t
off_t ftello(FILE *stream)
{
diff --git a/lib/libc/stdio/fwrite.c b/lib/libc/stdio/fwrite.c
index 709b1c72..4adbca81 100644
--- a/lib/libc/stdio/fwrite.c
+++ b/lib/libc/stdio/fwrite.c
@@ -1,11 +1,13 @@
-#include <io.h>
-#include <libc.h>
-#include <atomic.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
+#include "__stdio.h" // for _IO_ERR
+#include "stddef.h" // for NULL
+
+#include <atomic.h> // for LIBC_UNLOCK, LIBC_LOCK
+#include <errno.h> // for errno, EOVERFLOW, EBADF, EINVAL, EIO
+#include <fcntl.h> // for O_ACCMODE, O_RDONLY
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for fflush, fwrite, BUFSIZ, FILE, _IOFBF, _IOLBF
+#include <string.h> // for memcpy, memchr
+#include <unistd.h> // for size_t, ssize_t, write
char __stdout_buffer[BUFSIZ];
void (*__stdio_cleanup)(void);
@@ -17,131 +19,117 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems,
size_t written = 0;
const char *data = (const char *)ptr;
- // Validate parameters
- if (!ptr || !stream || size == 0) {
- if (size == 0)
+ if (ptr == NULL || stream == NULL || size == 0) {
+ if (size == 0) {
return nitems;
+ }
errno = EINVAL;
return 0;
}
- // Special case for string buffer operations (snprintf)
- // When fd is -1, we're writing to a string buffer
- if (stream->fd == -1 && stream->buf != NULL) {
- // Calculate total bytes with overflow check
+ if (__IMPL(stream)->fd == -1 && __IMPL(stream)->buf != NULL) {
if (__builtin_mul_overflow(size, nitems, &total_bytes)) {
errno = EOVERFLOW;
return 0;
}
- // Check remaining buffer space (leave room for null terminator)
size_t space_left =
- stream->buf_size > stream->buf_len ?
- stream->buf_size - stream->buf_len - 1 :
+ __IMPL(stream)->buf_size > __IMPL(stream)->buf_len ?
+ __IMPL(stream)->buf_size -
+ __IMPL(stream)->buf_len - 1 :
0;
if (space_left == 0) {
return 0;
}
- // Copy as much as fits
size_t to_copy = total_bytes < space_left ? total_bytes :
space_left;
if (to_copy > 0) {
- memcpy(stream->buf + stream->buf_len, data, to_copy);
- stream->buf_len += to_copy;
+ memcpy(__IMPL(stream)->buf + __IMPL(stream)->buf_len,
+ data, to_copy);
+ __IMPL(stream)->buf_len += to_copy;
}
- // Return number of complete items written
return to_copy == total_bytes ? nitems : to_copy / size;
}
- // Calculate total bytes with overflow check
if (__builtin_mul_overflow(size, nitems, &total_bytes)) {
errno = EOVERFLOW;
return 0;
}
- // Check if stream is writable (fix the flag check)
- if ((stream->flags & O_ACCMODE) == O_RDONLY) {
+ if ((__IMPL(stream)->flags & O_ACCMODE) == O_RDONLY) {
errno = EBADF;
return 0;
}
- // Check for error state
- if (stream->flags & _IO_ERR) {
+ if (__IMPL(stream)->flags & _IO_ERR) {
errno = EIO;
return 0;
}
- LIBC_LOCK(stream->lock);
+ LIBC_LOCK(__IMPL(stream)->lock);
- // Handle unbuffered I/O
- if (stream->type == _IONBF) {
- ssize_t result = write(stream->fd, data, total_bytes);
- LIBC_UNLOCK(stream->lock);
+ if (__IMPL(stream)->type == _IONBF) {
+ ssize_t result = write(__IMPL(stream)->fd, data, total_bytes);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
if (result < 0) {
- stream->flags |= _IO_ERR;
+ __IMPL(stream)->flags |= _IO_ERR;
return 0;
}
return result == (ssize_t)total_bytes ? nitems : result / size;
}
- // Handle buffered I/O (both _IOFBF and _IOLBF)
size_t remaining = total_bytes;
while (remaining > 0) {
- // Check if buffer is full
- size_t space_available = stream->buf_size - stream->buf_len;
+ size_t space_available =
+ __IMPL(stream)->buf_size - __IMPL(stream)->buf_len;
if (space_available == 0) {
- // Flush the buffer
- LIBC_UNLOCK(stream->lock);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
if (fflush(stream) != 0) {
return written / size;
}
- space_available = stream->buf_size;
+ space_available = __IMPL(stream)->buf_size;
}
- // Determine how much to copy this iteration
size_t to_copy = remaining < space_available ? remaining :
space_available;
- // Copy data to buffer
- memcpy(stream->buf + stream->buf_len, data + written, to_copy);
- stream->buf_len += to_copy;
+ memcpy(__IMPL(stream)->buf + __IMPL(stream)->buf_len,
+ data + written, to_copy);
+ __IMPL(stream)->buf_len += to_copy;
written += to_copy;
remaining -= to_copy;
- // For line buffered streams, check if we need to flush
- if (stream->type == _IOLBF) {
- // Look for newlines in the data we just added
- char *newline_pos =
- memchr(stream->buf + stream->buf_len - to_copy,
- '\n', to_copy);
+ if (__IMPL(stream)->type == _IOLBF) {
+ char *newline_pos = memchr(
+ __IMPL(stream)->buf + __IMPL(stream)->buf_len -
+ to_copy,
+ '\n', to_copy);
if (newline_pos != NULL) {
- LIBC_UNLOCK(stream->lock);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
if (fflush(stream) != 0) {
return written / size;
}
}
}
- // For fully buffered streams, flush if buffer is full
- else if (stream->type == _IOFBF &&
- stream->buf_len == stream->buf_size) {
- LIBC_UNLOCK(stream->lock);
+ else if (__IMPL(stream)->type == _IOFBF &&
+ __IMPL(stream)->buf_len == __IMPL(stream)->buf_size) {
+ LIBC_UNLOCK(__IMPL(stream)->lock);
if (fflush(stream) != 0) {
return written / size;
}
}
}
- LIBC_UNLOCK(stream->lock);
+ LIBC_UNLOCK(__IMPL(stream)->lock);
- // Return number of complete items written
return written == total_bytes ? nitems : written / size;
}
diff --git a/lib/libc/stdio/getc.c b/lib/libc/stdio/getc.c
index 251675aa..242c8396 100644
--- a/lib/libc/stdio/getc.c
+++ b/lib/libc/stdio/getc.c
@@ -1,5 +1,5 @@
-#include <libc.h>
-#include <stdio.h>
+#include <libc.h> // for weak_reference
+#include <stdio.h> // for fgetc, FILE, getc, getc_unlocked
int getc(FILE *stream)
{
diff --git a/lib/libc/stdio/getchar.c b/lib/libc/stdio/getchar.c
index 8c690d56..d26c3c6b 100644
--- a/lib/libc/stdio/getchar.c
+++ b/lib/libc/stdio/getchar.c
@@ -1,5 +1,5 @@
-#include <libc.h>
-#include <stdio.h>
+#include <libc.h> // for weak_reference
+#include <stdio.h> // for fgetc, getchar, getchar_unlocked, stdin
int getchar(void)
{
diff --git a/lib/libc/stdio/pclose.c b/lib/libc/stdio/pclose.c
index daae5bf5..241d4de0 100644
--- a/lib/libc/stdio/pclose.c
+++ b/lib/libc/stdio/pclose.c
@@ -1,10 +1,10 @@
-#include <io.h>
-#include <stdio.h>
-#include <sys/wait.h>
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for fclose, FILE, pclose
+#include <sys/wait.h> // for waitpid
int pclose(FILE *stream)
{
int stat;
fclose(stream);
- return (waitpid(stream->pid, &stat, 0) < 0) ? -1 : stat;
+ return (waitpid(__IMPL(stream)->pid, &stat, 0) < 0) ? -1 : stat;
}
diff --git a/lib/libc/stdio/perror.c b/lib/libc/stdio/perror.c
index f3f4bb19..63c708a9 100644
--- a/lib/libc/stdio/perror.c
+++ b/lib/libc/stdio/perror.c
@@ -1,8 +1,8 @@
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/uio.h>
+#include <errno.h> // for errno
+#include <stdio.h> // for NULL, perror
+#include <string.h> // for strlen, strerror
+#include <sys/uio.h> // for iovec, writev
+#include <unistd.h> // for STDERR_FILENO
void perror(const char *s)
{
diff --git a/lib/libc/stdio/popen.c b/lib/libc/stdio/popen.c
index 92b6a099..09c931ad 100644
--- a/lib/libc/stdio/popen.c
+++ b/lib/libc/stdio/popen.c
@@ -1,8 +1,10 @@
-#include <io.h>
-#include <errno.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include "stddef.h" // for NULL
+
+#include <errno.h> // for EINVAL, errno
+#include <fcntl.h> // for O_RDONLY, O_CLOEXEC, O_WRONLY
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for FILE, fclose, fdopen, popen
+#include <unistd.h> // for close, dup2, _exit, execl, fork, pipe2, STDIN_FI...
FILE *popen(const char *command, const char *mode)
{
@@ -58,7 +60,7 @@ FILE *popen(const char *command, const char *mode)
close(pipefd[0]);
}
- stream->pid = pid;
+ __IMPL(stream)->pid = pid;
return stream;
}
diff --git a/lib/libc/stdio/printf.c b/lib/libc/stdio/printf.c
index e8e7c388..e45a5118 100644
--- a/lib/libc/stdio/printf.c
+++ b/lib/libc/stdio/printf.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <stdarg.h>
+#include <stdarg.h> // for va_end, va_start
+#include <stdio.h> // for printf, vfprintf, stdout, va_list
int printf(const char *restrict format, ...)
{
diff --git a/lib/libc/stdio/putc.c b/lib/libc/stdio/putc.c
index 2486926a..421de457 100644
--- a/lib/libc/stdio/putc.c
+++ b/lib/libc/stdio/putc.c
@@ -1,5 +1,5 @@
-#include <libc.h>
-#include <stdio.h>
+#include <libc.h> // for weak_reference
+#include <stdio.h> // for fputc, FILE, putc, putc_unlocked
int putc(int c, FILE *stream)
{
diff --git a/lib/libc/stdio/putchar.c b/lib/libc/stdio/putchar.c
index f54bb9d7..c9e3ba08 100644
--- a/lib/libc/stdio/putchar.c
+++ b/lib/libc/stdio/putchar.c
@@ -1,5 +1,5 @@
-#include <libc.h>
-#include <stdio.h>
+#include <libc.h> // for weak_reference
+#include <stdio.h> // for putc, putchar, putchar_unlocked, stdout
int putchar(int c)
{
diff --git a/lib/libc/stdio/puts.c b/lib/libc/stdio/puts.c
index 41024337..1688a7cb 100644
--- a/lib/libc/stdio/puts.c
+++ b/lib/libc/stdio/puts.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <string.h>
+#include <stdio.h> // for fwrite, stdout, puts
+#include <string.h> // for strlen
int puts(const char *s)
{
diff --git a/lib/libc/stdio/remove.c b/lib/libc/stdio/remove.c
index 9aec52e9..25bbd3bd 100644
--- a/lib/libc/stdio/remove.c
+++ b/lib/libc/stdio/remove.c
@@ -1,6 +1,9 @@
-#include <stdio.h>
-#include <fcntl.h>
-#include <syscall.h>
+#include "asm/unistd_64.h" // for __NR_unlinkat
+#include "errno.h" // for EISDIR, errno
+
+#include <fcntl.h> // for AT_FDCWD, AT_REMOVEDIR
+#include <stdio.h> // for remove
+#include <syscall.h> // for __syscall_3, syscall
int remove(const char *path)
{
diff --git a/lib/libc/stdio/rename.c b/lib/libc/stdio/rename.c
index 612326e8..8857f852 100644
--- a/lib/libc/stdio/rename.c
+++ b/lib/libc/stdio/rename.c
@@ -1,4 +1,6 @@
-#include <syscall.h>
+#include "asm/unistd_64.h" // for __NR_rename
+
+#include <syscall.h> // 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 93d435fa..fde966c4 100644
--- a/lib/libc/stdio/renameat.c
+++ b/lib/libc/stdio/renameat.c
@@ -1,4 +1,6 @@
-#include <syscall.h>
+#include "asm/unistd_64.h" // for __NR_renameat
+
+#include <syscall.h> // for __syscall_4, syscall
int renameat(int oldfd, const char *old, int newfd, const char *new)
{
diff --git a/lib/libc/stdio/rewind.c b/lib/libc/stdio/rewind.c
index b047fa41..728e2d75 100644
--- a/lib/libc/stdio/rewind.c
+++ b/lib/libc/stdio/rewind.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#include <stdio.h> // for clearerr, fseek, FILE, SEEK_SET, rewind
void rewind(FILE *stream)
{
diff --git a/lib/libc/stdio/setbuf.c b/lib/libc/stdio/setbuf.c
index a1ef7b31..ddddf4ac 100644
--- a/lib/libc/stdio/setbuf.c
+++ b/lib/libc/stdio/setbuf.c
@@ -1,5 +1,4 @@
-#include <io.h>
-#include <stdio.h>
+#include <stdio.h> // for setvbuf, BUFSIZ, FILE, NULL, _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 56d7196c..02928a35 100644
--- a/lib/libc/stdio/setvbuf.c
+++ b/lib/libc/stdio/setvbuf.c
@@ -1,5 +1,7 @@
-#include <io.h>
-#include <stdio.h>
+#include "stddef.h" // for NULL
+
+#include <libc.h> // for __IMPL
+#include <stdio.h> // for _IONBF, FILE, _IOFBF, _IOLBF, setvbuf, size_t
int setvbuf(FILE *restrict stream, char *restrict buf, int type, size_t size)
{
@@ -8,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 (stream->fd < 0)
+ if (__IMPL(stream)->fd < 0)
return -1;
- stream->buf = buf;
- stream->buf_size = size;
- stream->buf_pos = 0;
- stream->type = type;
+ __IMPL(stream)->buf = buf;
+ __IMPL(stream)->buf_size = size;
+ __IMPL(stream)->buf_pos = 0;
+ __IMPL(stream)->type = type;
return 0;
}
diff --git a/lib/libc/stdio/snprintf.c b/lib/libc/stdio/snprintf.c
index 81f2c5b6..49204437 100644
--- a/lib/libc/stdio/snprintf.c
+++ b/lib/libc/stdio/snprintf.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <stdarg.h>
+#include <stdarg.h> // for va_end, va_start
+#include <stdio.h> // for snprintf, vsnprintf, size_t, va_list
int snprintf(char *restrict s, size_t n, const char *restrict format, ...)
{
diff --git a/lib/libc/stdio/sprintf.c b/lib/libc/stdio/sprintf.c
index 057c162d..a0ed7bf1 100644
--- a/lib/libc/stdio/sprintf.c
+++ b/lib/libc/stdio/sprintf.c
@@ -1,6 +1,5 @@
-#include <stdio.h>
-#include <limits.h>
-#include <stdarg.h>
+#include <stdarg.h> // for va_end, va_start
+#include <stdio.h> // for sprintf, vsprintf, va_list
int sprintf(char *restrict s, const char *restrict format, ...)
{
diff --git a/lib/libc/stdio/stderr.c b/lib/libc/stdio/stderr.c
index 42098960..155f0e3e 100644
--- a/lib/libc/stdio/stderr.c
+++ b/lib/libc/stdio/stderr.c
@@ -1,6 +1,8 @@
-#include <io.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include "__stdio.h" // for __FILE
+#include "stdatomic.h" // for ATOMIC_FLAG_INIT
+
+#include <fcntl.h> // for O_WRONLY
+#include <unistd.h> // for STDERR_FILENO
#define BUFSIZ 4096
diff --git a/lib/libc/stdio/stdin.c b/lib/libc/stdio/stdin.c
index 329c9528..b4194704 100644
--- a/lib/libc/stdio/stdin.c
+++ b/lib/libc/stdio/stdin.c
@@ -1,7 +1,8 @@
-#include <io.h>
-#include <libc.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include "__stdio.h" // for __FILE
+#include "features.h" // for __weak
+
+#include <fcntl.h> // for O_RDONLY
+#include <unistd.h> // for STDOUT_FILENO
#define BUFSIZ 4096
#define _IOLBF 0x1
diff --git a/lib/libc/stdio/stdout.c b/lib/libc/stdio/stdout.c
index 3645e07d..a21125c3 100644
--- a/lib/libc/stdio/stdout.c
+++ b/lib/libc/stdio/stdout.c
@@ -1,8 +1,11 @@
-#include <io.h>
-#include <libc.h>
-#include <fcntl.h>
-#include <atomic.h>
-#include <unistd.h>
+#include "__stdio.h" // for __FILE, __libc_fadd
+#include "features.h" // for __weak
+#include "stdatomic.h" // for ATOMIC_FLAG_INIT, atomic_flag
+
+#include <atomic.h> // for LIBC_LOCK, LIBC_UNLOCK
+#include <fcntl.h> // for O_WRONLY
+#include <stddef.h> // for NULL
+#include <unistd.h> // for STDOUT_FILENO
#define BUFSIZ 4096
diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c
index 128d4ee3..1f012caa 100644
--- a/lib/libc/stdio/vasprintf.c
+++ b/lib/libc/stdio/vasprintf.c
@@ -1,6 +1,6 @@
-#include <io.h>
-#include <stdarg.h>
-#include <stdio.h>
+#include <stdarg.h> // for va_copy, va_end
+#include <stdio.h> // for vsnprintf, va_list, vasprintf
+#include <stdlib.h> // for malloc
// TODO: maybe use memstream in future??
int vasprintf(char **restrict ptr, const char *restrict format, va_list ap)
diff --git a/lib/libc/stdio/vdprintf.c b/lib/libc/stdio/vdprintf.c
index 66a041cc..4647d641 100644
--- a/lib/libc/stdio/vdprintf.c
+++ b/lib/libc/stdio/vdprintf.c
@@ -1,12 +1,16 @@
-#include <io.h>
-#include <stdio.h>
+#include "__stdio.h" // for __FILE
+
+#include <stdio.h> // for vfprintf, _IONBF, va_list, vdprintf
int vdprintf(int fildes, const char *restrict format, va_list ap)
{
int r;
- FILE f;
- f.fd = fildes;
- f.type = _IONBF;
- r = vfprintf(&f, format, ap);
+ struct __FILE stream;
+
+ stream.fd = fildes;
+ stream.type = _IONBF;
+
+ r = vfprintf(&stream, format, ap);
+
return r;
}
diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c
index 90e60f7f..126c1193 100644
--- a/lib/libc/stdio/vfprintf.c
+++ b/lib/libc/stdio/vfprintf.c
@@ -1,14 +1,12 @@
-#include <ctype.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <stdint.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stddef.h>
-#include <math.h>
-#include <unistd.h>
+#include <ctype.h> // for isdigit
+#include <errno.h> // for EINVAL, errno
+#include <math.h> // for frexp, isinf, isnan
+#include <stdarg.h> // for va_arg
+#include <stddef.h> // for NULL, ptrdiff_t
+#include <stdint.h> // for uintptr_t, intmax_t, uintmax_t
+#include <stdio.h> // for fwrite, fputc, vfprintf, FILE, va_list
+#include <string.h> // for memmove, strlcpy, strlen
+#include <sys/types.h> // for size_t, ssize_t
extern char *dtoa(double, int mode, int ndigits, int *decpt, int *sign,
char **rve);
diff --git a/lib/libc/stdio/vprintf.c b/lib/libc/stdio/vprintf.c
index 10e938b7..c6cc8b4c 100644
--- a/lib/libc/stdio/vprintf.c
+++ b/lib/libc/stdio/vprintf.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#include <stdio.h> // for vfprintf, vprintf, stdout, va_list
int vprintf(const char *restrict format, va_list ap)
{
diff --git a/lib/libc/stdio/vsnprintf.c b/lib/libc/stdio/vsnprintf.c
index 7afae866..818cffb5 100644
--- a/lib/libc/stdio/vsnprintf.c
+++ b/lib/libc/stdio/vsnprintf.c
@@ -1,42 +1,43 @@
-#include <io.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdatomic.h>
-#include <fcntl.h>
+#include "__stdio.h" // for __FILE
+#include "stddef.h" // for NULL
+
+#include <fcntl.h> // for O_WRONLY
+#include <stdatomic.h> // for atomic_flag_clear
+#include <stdio.h> // for vfprintf, vsnprintf, _IONBF, size_t, va_list
+#include <string.h> // for memset
int vsnprintf(char *restrict s, size_t n, const char *restrict format,
va_list ap)
{
int r;
- FILE f;
+ struct __FILE stream;
if (n == 0) {
return 0;
}
- if (!s) {
+ if (s == NULL) {
return -1;
}
- memset(&f, 0, sizeof(f));
- f.fd = -1;
- f.flags = O_WRONLY;
- f.type = _IONBF;
- atomic_flag_clear(&f.lock);
- f.buf = s;
- f.buf_size = n;
- f.buf_len = 0;
- f.buf_pos = 0;
- f.eof = 0;
- f.unget_cnt = 0;
- f.offset = 0;
- f.next = NULL;
-
- r = vfprintf(&f, format, ap);
-
- if (f.buf_len < n) {
- s[f.buf_len] = '\0';
+ memset(&stream, 0, sizeof(stream));
+ stream.fd = -1;
+ stream.flags = O_WRONLY;
+ stream.type = _IONBF;
+ atomic_flag_clear(&stream.lock);
+ stream.buf = s;
+ stream.buf_size = n;
+ stream.buf_len = 0;
+ stream.buf_pos = 0;
+ stream.eof = 0;
+ stream.unget_cnt = 0;
+ stream.offset = 0;
+ stream.next = NULL;
+
+ r = vfprintf(&stream, format, ap);
+
+ if (stream.buf_len < n) {
+ s[stream.buf_len] = '\0';
} else if (n > 0) {
s[n - 1] = '\0';
r = n - 1;
diff --git a/lib/libc/stdio/vsprintf.c b/lib/libc/stdio/vsprintf.c
index eec52313..b8da63b4 100644
--- a/lib/libc/stdio/vsprintf.c
+++ b/lib/libc/stdio/vsprintf.c
@@ -1,5 +1,4 @@
-#include <stdarg.h>
-#include <stdio.h>
+#include <stdio.h> // for vsnprintf, vsprintf, size_t, va_list
int vsprintf(char *restrict s, const char *restrict format, va_list ap)
{