From cb518ecd55cd7a45d0368fb9d68a1981c6c91adf Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 5 May 2024 14:58:44 +0200 Subject: libc: implement asprintf --- src/libc/stdio/asprintf.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/libc/stdio/asprintf.c (limited to 'src/libc/stdio') diff --git a/src/libc/stdio/asprintf.c b/src/libc/stdio/asprintf.c new file mode 100644 index 0000000..ee24e38 --- /dev/null +++ b/src/libc/stdio/asprintf.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int asprintf(char **restrict sp, const char *restrict fmt, ...) { + int ret; + va_list argp; + va_start(argp, fmt); + ret = vasprintf(sp, fmt, argp); + va_end(argp); + return ret; +} +int vasprintf(char **restrict sp, const char *restrict fmt, va_list ap) { + va_list ap2; + va_copy(ap2, ap); + int ret = vsnprintf(NULL, 0, fmt, ap2); + va_end(ap2); + + if (ret < 0) return ret; + *sp = malloc(ret+1); + if (*sp == NULL) return -1; + return vsnprintf(*sp, ret+1, fmt, ap); +} -- cgit v1.2.3