diff options
author | dzwdz | 2024-05-05 14:58:44 +0200 |
---|---|---|
committer | dzwdz | 2024-05-05 14:58:58 +0200 |
commit | cb518ecd55cd7a45d0368fb9d68a1981c6c91adf (patch) | |
tree | c25ecffeeaf4f1f7d33b4ced91515d43a79c2dd5 /src/libc | |
parent | 80839982e9983c6ccadbf57a44e60eeb0e535421 (diff) |
libc: implement asprintf
Diffstat (limited to 'src/libc')
-rw-r--r-- | src/libc/include/stdio.h | 2 | ||||
-rw-r--r-- | src/libc/stdio/asprintf.c | 24 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/libc/include/stdio.h b/src/libc/include/stdio.h index b582e8f..159bdca 100644 --- a/src/libc/include/stdio.h +++ b/src/libc/include/stdio.h @@ -28,10 +28,12 @@ int printf(const char *restrict fmt, ...); int fprintf(FILE *restrict f, const char *restrict fmt, ...); int sprintf(char *restrict s, const char *restrict fmt, ...); +int asprintf(char **restrict sp, const char *restrict fmt, ...); int snprintf(char *restrict str, size_t len, const char *restrict fmt, ...); int vprintf(const char *restrict fmt, va_list ap); int vsprintf(char *restrict s, const char *restrict fmt, va_list ap); +int vasprintf(char **restrict sp, const char *restrict fmt, va_list ap); int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap); int _klogf(const char *fmt, ...); // for kernel debugging only 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 <shared/mem.h> +#include <shared/printf.h> +#include <stdio.h> +#include <stdlib.h> + +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); +} |