From ab54c861dc857522747264f7494793e1d1589ff2 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 11 Jan 2016 19:49:17 +0100 Subject: Reimplement asprintf() using dynamic strings Use the new string implementation to simplify asprintf(). Signed-off-by: Lukas Fleischer --- src/utils.c | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/utils.c b/src/utils.c index 415924a..14446fc 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1564,39 +1564,17 @@ void print_todo(const char *format, struct todo *todo) } } -#define VASPRINTF_INITIAL_BUFSIZE 128 - -int -vasprintf(char **str, const char *format, va_list ap) -{ - va_list ap2; - int n; - - va_copy(ap2, ap); - - *str = mem_malloc(VASPRINTF_INITIAL_BUFSIZE); - n = vsnprintf(*str, VASPRINTF_INITIAL_BUFSIZE, format, ap); - if (n >= VASPRINTF_INITIAL_BUFSIZE) { - *str = mem_realloc(*str, 1, n + 1); - n = vsnprintf(*str, n + 1, format, ap2); - } - - if (n < 0) { - mem_free(*str); - *str = NULL; - } - - return n; -} - int asprintf(char **str, const char *format, ...) { + struct string s; va_list ap; int n; va_start(ap, format); - n = vasprintf(str, format, ap); + string_init(&s); + n = string_vcatf(&s, format, ap); + *str = string_buf(&s); va_end(ap); return n; -- cgit v1.2.3-70-g09d2