From d47260dd13b2aca303934f1c3f1e61ed6c314738 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 31 May 2026 17:20:57 +0100 Subject: [PATCH] libc: io: implement (v)fprintf --- lib/libc/include/stdio.h | 1 + lib/libc/io/stdio/fprintf.c | 9 ++++++++- lib/libc/io/stdio/printf.c | 2 +- lib/libc/io/stdio/vfprintf.c | 35 +++++++++++++++++++++++++++++++++++ lib/libc/io/stdio/vprintf.c | 18 +++--------------- 5 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 lib/libc/io/stdio/vfprintf.c diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h index d8327e3..c8ce5f7 100644 --- a/lib/libc/include/stdio.h +++ b/lib/libc/include/stdio.h @@ -25,6 +25,7 @@ typedef struct __opaque_file FILE; extern int printf(const char *format, ...); extern int vprintf(const char *format, va_list arg); extern int fprintf(FILE *stream, const char *format, ...); +extern int vfprintf(FILE *stream, const char *format, va_list arg); extern int snprintf(char *buffer, size_t count, const char *format, ...); extern int vsnprintf( char *buffer, diff --git a/lib/libc/io/stdio/fprintf.c b/lib/libc/io/stdio/fprintf.c index 35e7d36..f4b2f47 100644 --- a/lib/libc/io/stdio/fprintf.c +++ b/lib/libc/io/stdio/fprintf.c @@ -1,6 +1,13 @@ #include "file.h" +#include + int fprintf(struct __opaque_file *stream, const char *format, ...) { - return 0; + va_list arg; + va_start(arg, format); + int ret = vfprintf(stream, format, arg); + va_end(arg); + + return ret; } diff --git a/lib/libc/io/stdio/printf.c b/lib/libc/io/stdio/printf.c index 67e4a1c..c6ffa34 100644 --- a/lib/libc/io/stdio/printf.c +++ b/lib/libc/io/stdio/printf.c @@ -5,7 +5,7 @@ int printf(const char *format, ...) { va_list arg; va_start(arg, format); - int ret = vprintf(format, arg); + int ret = vfprintf(stdout, format, arg); va_end(arg); return ret; diff --git a/lib/libc/io/stdio/vfprintf.c b/lib/libc/io/stdio/vfprintf.c new file mode 100644 index 0000000..442b6ce --- /dev/null +++ b/lib/libc/io/stdio/vfprintf.c @@ -0,0 +1,35 @@ +#include "file.h" + +#include +#include +#include + +extern int __libc_fctprintf( + int (*out)(char character, void *arg), + void *arg, + const char *format, + va_list va); +extern int __fputc(int c, struct __opaque_file *stream); + +static inline int _out_file(char character, void *arg) +{ + FILE *fp = arg; + return __fputc(character, fp); +} + +int vfprintf(FILE *fp, const char *format, va_list arg) +{ + __libc_file_lock(fp); + + int ret = __libc_fctprintf(_out_file, fp, format, arg); + if (errno != SUCCESS) { + ret = -1; + } + + if (ferror(fp)) { + ret = -1; + } + + __libc_file_unlock(fp); + return ret; +} diff --git a/lib/libc/io/stdio/vprintf.c b/lib/libc/io/stdio/vprintf.c index b6a3e07..f3125fe 100644 --- a/lib/libc/io/stdio/vprintf.c +++ b/lib/libc/io/stdio/vprintf.c @@ -1,7 +1,7 @@ #include "file.h" -#include #include +#include #include extern int __libc_fctprintf( @@ -11,7 +11,7 @@ extern int __libc_fctprintf( va_list va); extern int __fputc(int c, struct __opaque_file *stream); -static inline int _out_file(char character, void *arg) +static inline int _out_file(char character, void *arg) { FILE *fp = arg; return __fputc(character, fp); @@ -19,17 +19,5 @@ static inline int _out_file(char character, void *arg) int vprintf(const char *format, va_list arg) { - __libc_file_lock(stdout); - - int ret = __libc_fctprintf(_out_file, stdout, format, arg); - if (errno != SUCCESS) { - ret = -1; - } - - if (ferror(stdout)) { - ret = -1; - } - - __libc_file_unlock(stdout); - return ret; + return vfprintf(stdout, format, arg); }