From c4a46cd8baae484d54ceb866c076203ca804e66e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= <clement.chigot@atos.net>
Date: Thu, 16 Sep 2021 09:35:19 +0200
Subject: [PATCH] include: add support for asprintf and vasprintf

---
 include/asprintf.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 include/c.h        |  4 ++++
 2 files changed, 56 insertions(+)
 create mode 100644 include/asprintf.h

diff --git a/include/asprintf.h b/include/asprintf.h
new file mode 100644
index 0000000..a9b19e8
--- /dev/null
+++ b/include/asprintf.h
@@ -0,0 +1,52 @@
+#ifndef UTIL_LINUX_ASPRINTF_H
+#define UTIL_LINUX_ASPRINTF_H
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+
+#ifndef HAVE_VASPRINTF
+static inline
+int vasprintf(char **strp, const char *fmt, va_list ap)
+{
+  int str_size = -1, len;
+  va_list cap;
+
+  va_copy(cap, ap);
+
+  len = vsnprintf(0, 0, fmt, cap);
+  va_end(cap);
+
+  if (len < 0) return -1;
+
+  *strp = (char *)malloc (len + 1);
+
+  if (!*strp)
+      return -1;
+
+  str_size = vsnprintf(*strp, len + 1, fmt, ap);
+  if (str_size < 0 || str_size >= len + 1) {
+    free(*strp);
+    str_size = -1;
+  }
+  return str_size;
+}
+#endif
+
+#ifndef HAVE_ASPRINTF
+static inline
+int asprintf(char **strp, const char *fmt, ...)
+{
+  int res;
+  va_list ap;
+
+  va_start (ap, fmt);
+  res = vasprintf (strp, fmt, ap);
+  va_end (ap);
+
+  return res;
+}
+#endif
+
+#endif
diff --git a/include/c.h b/include/c.h
index ae08131..6f2ee1d 100644
--- a/include/c.h
+++ b/include/c.h
@@ -27,6 +27,10 @@
 # include <sys/sysmacros.h>     /* for major, minor */
 #endif
 
+#ifdef _AIX
+#include "asprintf.h"
+#endif
+
 #ifndef LOGIN_NAME_MAX
 # define LOGIN_NAME_MAX 256
 #endif
-- 
2.33.0

