From d1c4e0b7ceee424db0d8cc3ccb39fb33a4fbb8c5 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 11 Mar 2020 09:31:17 -0400 Subject: add header --- asprintf.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 asprintf.h (limited to 'asprintf.h') diff --git a/asprintf.h b/asprintf.h new file mode 100644 index 0000000..acec2b1 --- /dev/null +++ b/asprintf.h @@ -0,0 +1,40 @@ +#ifndef ASPRINTF_H +#define ASPRINTF_H + +#include +#include +#include + +static inline int vasprintf(char **strp, const char *fmt, va_list ap) +{ + va_list cp; + va_copy(cp, ap); + + int len = vsnprintf(NULL, 0, fmt, ap) + 1; + if (len < 0) { + return -1; + } + + *strp = malloc(len); + if (*strp == NULL) { + return -1; + } + + int ret = vsnprintf(*strp, len, fmt, cp); + + va_end(cp); + return ret; +} + +static inline int asprintf(char **strp, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + int ret = vasprintf(strp, fmt, ap); + + va_end(ap); + return ret; +} + +#endif -- cgit v1.2.1