diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/calcurse.h | 2 | ||||
-rw-r--r-- | src/utils.c | 38 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/calcurse.h b/src/calcurse.h index e593ff0..887e224 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -1051,6 +1051,8 @@ void print_recur_apoint(const char *, long, unsigned, struct recur_apoint *); void print_recur_event(const char *, long, struct recur_event *); void print_todo(const char *, struct todo *); +int vasprintf(char **, const char *, va_list); +int asprintf(char **, const char *, ...); /* vars.c */ extern int col, row; diff --git a/src/utils.c b/src/utils.c index 02839c3..c675518 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1473,3 +1473,41 @@ 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, ...) +{ + va_list ap; + int n; + + va_start(ap, format); + n = vasprintf(str, format, ap); + va_end(ap); + + return n; +} |