From 0621fa81b6f6ca0163fec31f2e8cbdc2375c118d Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 21 Apr 2023 15:55:17 +0900 Subject: [PATCH] libc: add asprintf, fix vasprintf return value --- base/usr/include/stdio.h | 1 + libc/stdio/printf.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base/usr/include/stdio.h b/base/usr/include/stdio.h index f8ff0c9a..2317b993 100644 --- a/base/usr/include/stdio.h +++ b/base/usr/include/stdio.h @@ -34,6 +34,7 @@ extern size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE * stream); extern int fileno(FILE * stream); extern int fflush(FILE * stream); +extern int asprintf(char ** ret, const char * fmt, ...); extern int vasprintf(char ** buf, const char *fmt, va_list args); extern int sprintf(char *buf, const char *fmt, ...); extern int fprintf(FILE *stream, const char *fmt, ...); diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c index 1547520a..1547c9b3 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.c @@ -438,12 +438,19 @@ static int cb_asprintf(void * user, char c) { int vasprintf(char ** buf, const char * fmt, va_list args) { struct CBData data = {NULL,0,0}; - int out = xvasprintf(cb_asprintf, &data, fmt, args); + xvasprintf(cb_asprintf, &data, fmt, args); cb_asprintf(&data, '\0'); *buf = data.str; - return out; + return 0; } +int asprintf(char ** ret, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + int out = vasprintf(ret,fmt,args); + va_end(args); + return out; +} /* Streams */