Add vprintf, (v)sprintf and (v)snprintf to libc + move the non v-printfs to stdio.cpp, since they can now call their v-variants and thus don't depend on internal_printf

This commit is contained in:
apio 2022-10-03 19:59:33 +02:00
parent 9420484c9b
commit cb60e418b2
3 changed files with 51 additions and 6 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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;
}
}