summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@calcurse.org>2016-01-11 19:49:17 +0100
committerLukas Fleischer <lfleischer@calcurse.org>2016-01-13 17:39:44 +0100
commitab54c861dc857522747264f7494793e1d1589ff2 (patch)
tree2cd1d22b4674567a0eeee23b72f0ff099695f36e /src
parent03d35bacf44d7f3e63805411370b7ada8aae93e8 (diff)
downloadcalcurse-ab54c861dc857522747264f7494793e1d1589ff2.zip
Reimplement asprintf() using dynamic strings
Use the new string implementation to simplify asprintf(). Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
Diffstat (limited to 'src')
-rw-r--r--src/utils.c30
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;