From adc0d4ac927193ab00160fc1d083fd40ff982d9d Mon Sep 17 00:00:00 2001 From: Josh Elsasser Date: Wed, 18 Jul 2007 17:19:14 +0000 Subject: [PATCH] Add asprintf implementation for systems which lack it. --- configure | 41 ++++++++++++++++++++++++++++++++++++++ libtransmission/trcompat.h | 6 ++++++ libtransmission/utils.c | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/configure b/configure index e4395d2cc..46cb5108d 100755 --- a/configure +++ b/configure @@ -180,6 +180,45 @@ EOF rm -f testconf* } +asprintf_test() +{ + verbose asprintf_test + cat > testconf.c < +int main() +{ + asprintf( NULL, NULL ); + return 0; +} +EOF + if runcmd $CC -o testconf testconf.c + then + CFLAGS="$CFLAGS -DHAVE_ASPRINTF" + fi + rm -f testconf* +} + +snprintf_test() +{ + verbose snprintf_test + cat > testconf.c < +int main() +{ + char buf[] = "blueberry"; + return ( 6 != snprintf( buf, 4, "%s%s", "foo", "bar" ) || + 0 != memcmp( buf, "foo\0berry", 9 ) ); +} +EOF + if ! runcmd $CC -o testconf testconf.c || ! runcmd ./testconf + then + echo "error: broken snprintf() found" + exit 1 + fi + rm -f testconf* +} + libgen_test() { verbose libgen_test @@ -467,6 +506,8 @@ lrintf_test # strlcpy_test strlcat_test +asprintf_test +snprintf_test libgen_test # diff --git a/libtransmission/trcompat.h b/libtransmission/trcompat.h index 8ca1deecc..ddcd4c9f8 100644 --- a/libtransmission/trcompat.h +++ b/libtransmission/trcompat.h @@ -24,6 +24,7 @@ #ifndef TRCOMPAT_H #define TRCOMPAT_H +#include #include /* for size_t */ #ifndef HAVE_STRLCPY @@ -36,6 +37,11 @@ size_t strlcat(char *dst, const char *src, size_t siz); #endif +#ifndef HAVE_ASPRINTF +int asprintf( char **, const char *, ... ); +int vasprintf( char **, const char *, va_list ); +#endif + #ifdef HAVE_LIBGEN # include #else diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 4dd1d8eb0..62230c58d 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -28,7 +28,9 @@ #include #include #include /* usleep, stat */ + #include "transmission.h" +#include "trcompat.h" #define SPRINTF_BUFSIZE 100 @@ -319,6 +321,44 @@ int tr_vsprintf( char ** buf, int * used, int * max, const char * fmt, return 0; } +#ifndef HAVE_ASPRINTF + +int +asprintf( char ** buf, const char * format, ... ) +{ + va_list ap; + int ret; + + va_start( ap, format ); + ret = vasprintf( buf, format, ap ); + va_end( ap ); + + return ret; +} + +int +vasprintf( char ** buf, const char * format, va_list ap ) +{ + va_list ap2; + int ret, used, max; + + va_copy( ap2, ap ); + + *buf = NULL; + used = 0; + max = 0; + + if( tr_vsprintf( buf, &used, &max, format, ap, ap2 ) ) + { + free( *buf ); + return -1; + } + + return used; +} + +#endif /* HAVE_ASPRINTF */ + int tr_concat( char ** buf, int * used, int * max, const char * data, int len ) { int newmax;