diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2011-10-14 03:00:39 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2011-10-14 03:00:39 +0000 |
commit | ffe612e49de38913799133a7af5d9baa1c7424e1 (patch) | |
tree | 0f5aceb2a8fcf4a82e6e9fa12c8a892f5d422b2c /lib/util/nstring.c | |
parent | 9aa506b90dd79fd1b00b97cd32bf73829565d4aa (diff) | |
download | netpbm-mirror-ffe612e49de38913799133a7af5d9baa1c7424e1.tar.gz netpbm-mirror-ffe612e49de38913799133a7af5d9baa1c7424e1.tar.xz netpbm-mirror-ffe612e49de38913799133a7af5d9baa1c7424e1.zip |
Use libc asprintf where available
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1590 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util/nstring.c')
-rw-r--r-- | lib/util/nstring.c | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/util/nstring.c b/lib/util/nstring.c index 03439cad..e3aa5565 100644 --- a/lib/util/nstring.c +++ b/lib/util/nstring.c @@ -113,6 +113,10 @@ */ +#define _GNU_SOURCE + /* Due to conditional compilation, this is GNU source only if the C library + is GNU. + */ #define PORTABLE_SNPRINTF_VERSION_MAJOR 2 #define PORTABLE_SNPRINTF_VERSION_MINOR 2 @@ -130,6 +134,12 @@ #include "nstring.h" +#if (defined(__GLIBC__) || defined(__GNU_LIBRARY__)) + #define HAVE_VASPRINTF 1 +#else + #define HAVE_VASPRINTF 0 +#endif + #ifdef isdigit #undef isdigit #endif @@ -745,26 +755,30 @@ pm_asprintf(const char ** const resultP, const char * const fmt, ...) { + const char * result; va_list varargs; +#if HAVE_VASPRINTF + va_start(varargs, fmt); + vasprintf((char **)&result, fmt, varargs); + va_end(varargs); +#else size_t dryRunLen; va_start(varargs, fmt); - + pm_vsnprintf(NULL, 0, fmt, varargs, &dryRunLen); va_end(varargs); if (dryRunLen + 1 < dryRunLen) /* arithmetic overflow */ - *resultP = pm_strsol; + result = NULL; else { size_t const allocSize = dryRunLen + 1; char * result; result = malloc(allocSize); - if (result == NULL) - *resultP = pm_strsol; - else { + if (result != NULL) { va_list varargs; size_t realLen; @@ -774,10 +788,14 @@ pm_asprintf(const char ** const resultP, assert(realLen == dryRunLen); va_end(varargs); - - *resultP = result; } } +#endif + + if (result == NULL) + *resultP = pm_strsol; + else + *resultP = result; } |