diff --git a/libs/libc/include/stdio.h b/libs/libc/include/stdio.h index 2197ae13..f2528129 100644 --- a/libs/libc/include/stdio.h +++ b/libs/libc/include/stdio.h @@ -29,6 +29,11 @@ extern "C" void setbuf(FILE*, char*); int vfprintf(FILE*, const char*, va_list); int printf(const char*, ...); + int vprintf(const char*, va_list); + int sprintf(char*, const char*, ...); + int snprintf(char*, size_t, const char*, ...); + int vsprintf(char*, const char*, va_list); + int vsnprintf(char*, size_t, const char*, va_list); int puts(const char*); #ifdef __cplusplus diff --git a/libs/libc/src/printf.cpp b/libs/libc/src/printf.cpp index e65f3ac7..16fef188 100644 --- a/libs/libc/src/printf.cpp +++ b/libs/libc/src/printf.cpp @@ -247,13 +247,29 @@ static int internal_printf(const char* format, PutString put_string_callback, ss extern "C" { - int printf(const char* format, ...) + int vprintf(const char* format, va_list ap) { - va_list ap; - va_start(ap, format); - int written = internal_printf( + return internal_printf( format, [](const char* s) { syscall(SYS_write, s, strlen(s)); }, -1, ap); - va_end(ap); - return written; + } + + int vsprintf(char* str, const char* format, va_list ap) + { + return internal_printf( + format, + [&](const char* s) { + if (str) strcat(str, s); + }, + -1, ap); + } + + int vsnprintf(char* str, size_t max, const char* format, va_list ap) + { + return internal_printf( + format, + [&](const char* s) { + if (str) strcat(str, s); + }, + max == 0 ? 0 : max - 1, ap); } } \ No newline at end of file diff --git a/libs/libc/src/stdio.cpp b/libs/libc/src/stdio.cpp index 48e08768..cbab3974 100644 --- a/libs/libc/src/stdio.cpp +++ b/libs/libc/src/stdio.cpp @@ -53,4 +53,28 @@ extern "C" nwritten += syscall(SYS_write, "\n", 1); return nwritten; } + int snprintf(char* str, size_t max, const char* format, ...) + { + va_list ap; + va_start(ap, format); + int written = vsnprintf(str, max, format, ap); + va_end(ap); + return written; + } + int sprintf(char* str, const char* format, ...) + { + va_list ap; + va_start(ap, format); + int written = vsprintf(str, format, ap); + va_end(ap); + return written; + } + int printf(const char* format, ...) + { + va_list ap; + va_start(ap, format); + int written = vprintf(format, ap); + va_end(ap); + return written; + } } \ No newline at end of file