Add an asprintf() implementation
authorBart Van Assche <bart.vanassche@wdc.com>
Mon, 12 Mar 2018 22:38:28 +0000 (15:38 -0700)
committerBart Van Assche <bart.vanassche@wdc.com>
Mon, 19 Mar 2018 17:39:28 +0000 (10:39 -0700)
Since I would like to use the asprintf() function in the ZBC code and
since that function is not available on every platform supported by
fio, add an asprintf() implementation.

Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com>
Makefile
configure
oslib/asprintf.c [new file with mode: 0644]
oslib/asprintf.h [new file with mode: 0644]

index eb3bddd6be095fc7a46bf8764e36eddb06d1a7bb..d45ba6b5585fe43ce793937f0fa5c87828c434b4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -104,6 +104,7 @@ endif
 ifdef CONFIG_RBD
   SOURCE += engines/rbd.c
 endif
+SOURCE += oslib/asprintf.c
 ifndef CONFIG_STRSEP
   SOURCE += oslib/strsep.c
 endif
index ddf03a6b8e1b12faddb1f4a9310d30d45214dac7..f6358630562229029bb409ee98fbe94df939f7d1 100755 (executable)
--- a/configure
+++ b/configure
@@ -783,6 +783,40 @@ if test "$disable_rdma" != "yes" && compile_prog "" "-lrdmacm" "rdma"; then
 fi
 print_config "rdmacm" "$rdmacm"
 
+##########################################
+# asprintf() and vasprintf() probes
+if test "$have_asprintf" != "yes" ; then
+  have_asprintf="no"
+fi
+cat > $TMPC << EOF
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+  return asprintf(NULL, "%s", "str") == 0;
+}
+EOF
+if compile_prog "" "" "have_asprintf"; then
+    have_asprintf="yes"
+fi
+print_config "asprintf()" "$have_asprintf"
+
+if test "$have_vasprintf" != "yes" ; then
+  have_vasprintf="no"
+fi
+cat > $TMPC << EOF
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+  return vasprintf(NULL, "%s", NULL) == 0;
+}
+EOF
+if compile_prog "" "" "have_vasprintf"; then
+    have_vasprintf="yes"
+fi
+print_config "vasprintf()" "$have_vasprintf"
+
 ##########################################
 # Linux fallocate probe
 if test "$linux_fallocate" != "yes" ; then
@@ -2169,6 +2203,12 @@ fi
 if test "$posix_pshared" = "yes" ; then
   output_sym "CONFIG_PSHARED"
 fi
+if test "$have_asprintf" = "yes" ; then
+    output_sym "HAVE_ASPRINTF"
+fi
+if test "$have_vasprintf" = "yes" ; then
+    output_sym "HAVE_VASPRINTF"
+fi
 if test "$linux_fallocate" = "yes" ; then
   output_sym "CONFIG_LINUX_FALLOCATE"
 fi
diff --git a/oslib/asprintf.c b/oslib/asprintf.c
new file mode 100644 (file)
index 0000000..f1e7fd2
--- /dev/null
@@ -0,0 +1,43 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "oslib/asprintf.h"
+
+#ifndef HAVE_VASPRINTF
+int vasprintf(char **strp, const char *fmt, va_list ap)
+{
+    va_list ap_copy;
+    char *str;
+    int len;
+
+#ifdef va_copy
+    va_copy(ap_copy, ap);
+#else
+    __va_copy(ap_copy, ap);
+#endif
+    len = vsnprintf(NULL, 0, fmt, ap_copy);
+    va_end(ap_copy);
+
+    if (len < 0)
+        return len;
+
+    len++;
+    str = malloc(len);
+    *strp = str;
+    return str ? vsnprintf(str, len, fmt, ap) : -1;
+}
+#endif
+
+#ifndef HAVE_ASPRINTF
+int asprintf(char **strp, const char *fmt, ...)
+{
+    va_list arg;
+    int done;
+
+    va_start(arg, fmt);
+    done = vasprintf(strp, fmt, arg);
+    va_end(arg);
+
+    return done;
+}
+#endif
diff --git a/oslib/asprintf.h b/oslib/asprintf.h
new file mode 100644 (file)
index 0000000..1aa076b
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef FIO_ASPRINTF_H
+#define FIO_ASPRINTF_H
+
+#ifndef HAVE_VASPRINTF
+int vasprintf(char **strp, const char *fmt, va_list ap);
+#endif
+#ifndef HAVE_ASPRINTF
+int asprintf(char **strp, const char *fmt, ...);
+#endif
+
+#endif /* FIO_ASPRINTF_H */