asprintf.c

asprintf polyfill, regardless of _GNU_SOURCE
git clone git://git.finwo.net/lib/asprintf.c
Log | Files | Refs

commit 4e144f4f788c28908776eed85a1752225163f051
parent e5fc661626336e227e0cedde8abb154549370b6b
Author: finwo <finwo@pm.me>
Date:   Wed, 25 Oct 2023 22:59:12 +0200

Actually make it properly alloc

Diffstat:
Msrc/asprintf.c | 8++++----
Msrc/asprintf.h | 2+-
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/asprintf.c b/src/asprintf.c @@ -4,13 +4,13 @@ #include <stdlib.h> // Polyfill asprintf, comes from stdio when _GNU_SOURCE is defined -char * asprintf(char **restrict strp, const char *restrict fmt, ...) { +int asprintf(char **restrict strp, const char *restrict fmt, ...) { va_list argptr; va_start(argptr, fmt); int size = vsnprintf(NULL, 0, fmt, argptr); - char *output = calloc(1, size + 1); - vsnprintf(output, size + 1, fmt, argptr); + *strp = realloc(*strp, size + 1); + vsnprintf(*strp, size + 1, fmt, argptr); va_end(argptr); - return output; + return size; } #endif diff --git a/src/asprintf.h b/src/asprintf.h @@ -1,5 +1,5 @@ #ifdef _GNU_SOURCE #include <stdio.h> #else -char * asprintf(char **restrict strp, const char *restrict fmt, ...); +int asprintf(char **restrict strp, const char *restrict fmt, ...); #endif