From 7b8644dcc1a0db95c3ea321ebb8bc059ab931b42 Mon Sep 17 00:00:00 2001
From: Richard Nyberg <rnyberg@murmeldjur.se>
Date: Mon, 12 Jan 2009 22:09:26 +0100
Subject: [PATCH] Provide own implementation of asprintf if it's missing.

---
 misc/subr.c | 20 ++++++++++++++++++++
 misc/subr.h |  8 +++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

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