diff options
author | Lukas Fleischer <lfleischer@calcurse.org> | 2016-01-11 19:49:17 +0100 |
---|---|---|
committer | Lukas Fleischer <lfleischer@calcurse.org> | 2016-01-13 17:39:44 +0100 |
commit | ab54c861dc857522747264f7494793e1d1589ff2 (patch) | |
tree | 2cd1d22b4674567a0eeee23b72f0ff099695f36e | |
parent | 03d35bacf44d7f3e63805411370b7ada8aae93e8 (diff) | |
download | calcurse-ab54c861dc857522747264f7494793e1d1589ff2.tar.gz calcurse-ab54c861dc857522747264f7494793e1d1589ff2.zip |
Reimplement asprintf() using dynamic strings
Use the new string implementation to simplify asprintf().
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
-rw-r--r-- | src/utils.c | 30 |
1 files changed, 4 insertions, 26 deletions
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; |