From 44e8e956254815d90472076951b81bc933f83fd5 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Mon, 12 Mar 2018 15:38:28 -0700 Subject: [PATCH] Add an asprintf() implementation 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 --- Makefile | 1 + configure | 40 ++++++++++++++++++++++++++++++++++++++++ oslib/asprintf.c | 43 +++++++++++++++++++++++++++++++++++++++++++ oslib/asprintf.h | 11 +++++++++++ 4 files changed, 95 insertions(+) create mode 100644 oslib/asprintf.c create mode 100644 oslib/asprintf.h diff --git a/Makefile b/Makefile index eb3bddd6..d45ba6b5 100644 --- 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 diff --git a/configure b/configure index ddf03a6b..f6358630 100755 --- 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 + +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 + +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 index 00000000..f1e7fd23 --- /dev/null +++ b/oslib/asprintf.c @@ -0,0 +1,43 @@ +#include +#include +#include +#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 index 00000000..1aa076b8 --- /dev/null +++ b/oslib/asprintf.h @@ -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 */ -- 2.25.1