summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-03-11 09:31:17 -0400
committerJakob Kaivo <jkk@ung.org>2020-03-11 09:31:17 -0400
commitd1c4e0b7ceee424db0d8cc3ccb39fb33a4fbb8c5 (patch)
tree1404d9bfa9255920cf0a4c39fb324c1843672fb6
add header
-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