summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--asprintf.h40
1 files changed, 40 insertions, 0 deletions
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 <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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