From 2391ec9cff7b64df58f17151bfd8c40e85d56c92 Mon Sep 17 00:00:00 2001
From: Dirk Zimoch <dirk.zimoch@psi.ch>
Date: Wed, 12 Aug 2015 12:00:41 +0200
Subject: [PATCH] added helper functions unavailable in vxWorks

---
 asprintf.c | 34 ++++++++++++++++++++++++++++++++++
 asprintf.h | 12 ++++++++++++
 strdup.c   | 22 ++++++++++++++++++++++
 strdup.h   | 11 +++++++++++
 4 files changed, 79 insertions(+)
 create mode 100644 asprintf.c
 create mode 100644 asprintf.h
 create mode 100644 strdup.c
 create mode 100644 strdup.h

diff --git a/asprintf.c b/asprintf.c
new file mode 100644
index 00000000..a4f200cf
--- /dev/null
+++ b/asprintf.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "asprintf.h"
+
+int vasprintf(char** pbuffer, const char* format, va_list ap)
+{
+    va_list ap2;
+    int len;
+    FILE* f;
+    
+    /* print to null device to get required buffer length */
+    f = fopen("/null","w");
+    if (f == NULL) return -1;
+    __va_copy(ap2, ap);
+    len = vfprintf(f, format, ap2);
+    va_end(ap2);
+    fclose(f);
+    if (len < 0) return len;
+
+    *pbuffer = malloc(len+1);
+    if (*pbuffer == NULL) return -1;
+    return vsprintf(*pbuffer, format, ap);
+}
+
+int asprintf(char** pbuffer, const char* format, ...)
+{
+    va_list ap;
+    int len;
+
+    va_start(ap, format);
+    len = vasprintf(pbuffer, format, ap);
+    va_end(ap);
+    return len;
+}
diff --git a/asprintf.h b/asprintf.h
new file mode 100644
index 00000000..7625d11f
--- /dev/null
+++ b/asprintf.h
@@ -0,0 +1,12 @@
+#ifndef asprintf_h
+#define asprintf_h
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <stdarg.h>
+int asprintf(char** pbuffer, const char* format, ...) __attribute__((format(printf,2,3)));
+int vasprintf(char** pbuffer, const char* format, va_list ap) __attribute__((format(printf,2,0)));
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/strdup.c b/strdup.c
new file mode 100644
index 00000000..bc708537
--- /dev/null
+++ b/strdup.c
@@ -0,0 +1,22 @@
+#include <string.h>
+#include <stdlib.h>
+
+char *strdup(const char *s)
+{
+    char *d = malloc(strlen(s)+1);
+    if (d) strcpy(d,s);
+    return d;
+}
+
+char *strndup(const char *s, size_t n)
+{
+    size_t l;
+    char *d;
+    
+    l = strlen(s);
+    if (n > l) n = l;
+    d = malloc(n+1);
+    strncpy(d,s,l);
+    d[n] = 0;
+    return d;
+}
diff --git a/strdup.h b/strdup.h
new file mode 100644
index 00000000..f20c9f9b
--- /dev/null
+++ b/strdup.h
@@ -0,0 +1,11 @@
+#ifndef strdup_h
+#define strdup_h
+#ifdef __cplusplus
+extern "C" {
+#endif
+char *strdup(const char *s);
+char *strndup(const char *s, size_t n);
+#ifdef __cplusplus
+}
+#endif
+#endif
-- 
GitLab