libc: io: implement (v)fprintf

This commit is contained in:
2026-05-31 17:20:57 +01:00
parent c9a87457b8
commit d47260dd13
5 changed files with 48 additions and 17 deletions
+1
View File
@@ -25,6 +25,7 @@ typedef struct __opaque_file FILE;
extern int printf(const char *format, ...); extern int printf(const char *format, ...);
extern int vprintf(const char *format, va_list arg); extern int vprintf(const char *format, va_list arg);
extern int fprintf(FILE *stream, const char *format, ...); 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 snprintf(char *buffer, size_t count, const char *format, ...);
extern int vsnprintf( extern int vsnprintf(
char *buffer, char *buffer,
+8 -1
View File
@@ -1,6 +1,13 @@
#include "file.h" #include "file.h"
#include <stdio.h>
int fprintf(struct __opaque_file *stream, const char *format, ...) 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;
} }
+1 -1
View File
@@ -5,7 +5,7 @@ int printf(const char *format, ...)
{ {
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
int ret = vprintf(format, arg); int ret = vfprintf(stdout, format, arg);
va_end(arg); va_end(arg);
return ret; return ret;
+35
View File
@@ -0,0 +1,35 @@
#include "file.h"
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
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;
}
+2 -14
View File
@@ -1,7 +1,7 @@
#include "file.h" #include "file.h"
#include <stddef.h>
#include <errno.h> #include <errno.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
extern int __libc_fctprintf( extern int __libc_fctprintf(
@@ -19,17 +19,5 @@ static inline int _out_file(char character, void *arg)
int vprintf(const char *format, va_list arg) int vprintf(const char *format, va_list arg)
{ {
__libc_file_lock(stdout); return vfprintf(stdout, format, arg);
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;
} }