about summary refs log tree commit diff
path: root/misc
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2009-01-12 22:09:26 +0100
committerRichard Nyberg <rnyberg@murmeldjur.se>2009-01-12 23:43:35 +0100
commit7b8644dcc1a0db95c3ea321ebb8bc059ab931b42 (patch)
treefb4b0ad2985e42a66c982b15d79eaf0019f098e3 /misc
parentb50a306ca3966dc056974c39e72e19da41ad873b (diff)
downloadbtpd-7b8644dcc1a0db95c3ea321ebb8bc059ab931b42.tar.gz
btpd-7b8644dcc1a0db95c3ea321ebb8bc059ab931b42.zip
Provide own implementation of asprintf if it's missing.
Diffstat (limited to 'misc')
-rw-r--r--misc/subr.c20
-rw-r--r--misc/subr.h8
2 files changed, 27 insertions, 1 deletions
diff --git a/misc/subr.c b/misc/subr.c
index 5b5261e..b90af81 100644
--- a/misc/subr.c
+++ b/misc/subr.c
@@ -14,6 +14,8 @@
 #include <strings.h>
 #include <unistd.h>
 
+#include "subr.h"
+
 void *
 memfind(const void *sub, size_t sublen, const void *mem, size_t memlen)
 {
@@ -443,3 +445,21 @@ end:
     out[oi] = '\0';
     return 0;
 }
+
+#ifndef HAVE_ASPRINTF
+int
+asprintf(char **strp, const char *fmt, ...)
+{
+    int np;
+    va_list ap;
+    va_start(ap, fmt);
+    np = vsnprintf(NULL, 0, fmt, ap);
+    va_end(ap);
+    if ((*strp = malloc(np + 1)) == NULL)
+        return -1;
+    va_start(ap, fmt);
+    vsnprintf(*strp, np + 1, fmt, ap);
+    va_end(ap);
+    return np;
+}
+#endif
diff --git a/misc/subr.h b/misc/subr.h
index 73465b0..cec7586 100644
--- a/misc/subr.h
+++ b/misc/subr.h
@@ -1,8 +1,9 @@
 #ifndef BTPD_SUBR_H
 #define BTPD_SUBR_H
 
-#include <stdio.h>
 #include <stdarg.h>
+#include <stdio.h>
+#include <stdint.h>
 
 #define max(x, y) ((x) >= (y) ? (x) : (y))
 #define min(x, y) ((x) <= (y) ? (x) : (y))
@@ -47,4 +48,9 @@ void *read_file(const char *path, void *buf, size_t *size);
 char *find_btpd_dir(void);
 int make_abs_path(const char *in, char *out);
 
+#ifndef HAVE_ASPRINTF
+__attribute__((format (printf, 2, 3)))
+int asprintf(char **strp, const char *fmt, ...);
+#endif
+
 #endif