From 81fa8da4ce006da745dd98d47ce782d01cd9035d Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 25 Jan 2011 12:39:05 +0100 Subject: [PATCH] Check for vasprintf() and add a replacement if it wasn't found. --- configure | 26 ++++++++++++++++++++++++++ lib/util.c | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/configure b/configure index 45eeaaa0..6c90e78f 100755 --- a/configure +++ b/configure @@ -4,6 +4,7 @@ # Ensure that we do not inherit these from env STRLCPY= STRLCAT= +VASPRINTF= OS= BUILD= HOST= @@ -275,6 +276,29 @@ if [ -n "$BUILD_PIE" ]; then check_compiler_flag pie "" PROG_LDFLAGS fi +# +# Check for vasprintf(). +# +printf "Checking for vasprintf() ... " +cat <_vasprintf.c +#define _GNU_SOURCE +#include +int main(void) { + vasprintf(NULL, NULL, NULL); + return 0; +} +EOF +if $XCC _vasprintf.c -o _vasprintf 2>/dev/null; then + VASPRINTF=yes +else + VASPRINTF=no +fi +echo "$VASPRINTF." +rm -f _vasprint.c _vasprintf +if [ "$VASPRINTF" = "yes" ]; then + echo "CPPFLAGS += -DHAVE_VASPRINTF" >> $CONFIG_MK +fi + # # Check for strlcpy(). # @@ -298,6 +322,7 @@ rm -f _strlcpy.c _strlcpy if [ "$STRLCPY" = no ]; then echo "COMPAT_SRCS += compat/strlcpy.o" >>$CONFIG_MK echo "#include \"strlcpy.h\"" >>$CONFIG_H +else echo "CPPFLAGS += -DHAVE_STRLCPY" >> $CONFIG_MK fi @@ -323,6 +348,7 @@ rm -f _strlcat.c _strlcat if [ "$STRLCAT" = no ]; then echo "COMPAT_SRCS += compat/strlcat.o" >>$CONFIG_MK echo "#include \"strlcat.h\"" >>$CONFIG_H +else echo "CPPFLAGS += -DHAVE_STRLCAT" >>$CONFIG_MK fi diff --git a/lib/util.c b/lib/util.c index 99d0b182..4c89befe 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2008-2010 Juan Romero Pardines. + * Copyright (c) 2008-2011 Juan Romero Pardines. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -45,7 +45,6 @@ * @brief Utility routines * @defgroup util Utility functions */ - static const char *rootdir; static const char *cachedir; static int flags; @@ -493,6 +492,7 @@ xbps_get_flags(void) return flags; } +#ifdef HAVE_VASPRINTF char * xbps_xasprintf(const char *fmt, ...) { @@ -500,9 +500,38 @@ xbps_xasprintf(const char *fmt, ...) char *buf; va_start(ap, fmt); - if (vasprintf(&buf, fmt, ap) == -1) + if (vasprintf(&buf, fmt, ap) == -1) { + va_end(ap); return NULL; + } va_end(ap); return buf; } +#else +char * +xbps_xasprintf(const char *fmt, ...) +{ + va_list ap, aq; + size_t len; + int res; + char *buf; + + va_start(aq, fmt); + va_copy(ap, aq); + len = vsnprintf(NULL, 0, fmt, aq) + 1; + if ((buf = malloc(len)) == NULL) { + va_end(ap); + return NULL; + } + + va_start(ap, fmt); + res = vsnprintf(buf, len, fmt, ap); + if (res < 0 || res >= (int)len) { + free(buf); + return NULL; + } + + return buf; +} +#endif /* !HAVE_VASPRINTF */