From 3d29aa18911043879fcd4412fa25b0ab5f5e95bb Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 18 Aug 2012 18:33:19 +0200 Subject: [PATCH] Remove VSPRINTF. This macro has only a single user in string.c, and it's kind of pointless for this code to be a macro. --- adu.h | 30 ------------------------------ string.c | 24 +++++++++++++++++++++--- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/adu.h b/adu.h index 42b9471..45dae6a 100644 --- a/adu.h +++ b/adu.h @@ -85,36 +85,6 @@ #endif /** \endcond */ -/** - * Write a log message to a dynamically allocated string. - * - * \param fmt Usual format string. - * \param p Result pointer. - * - * \sa printf(3). */ -#define VSPRINTF(fmt, p) \ -{ \ - int n; \ - size_t size = 100; \ - p = adu_malloc(size); \ - while (1) { \ - va_list ap; \ - /* Try to print in the allocated space. */ \ - va_start(ap, fmt); \ - n = vsnprintf(p, size, fmt, ap); \ - va_end(ap); \ - /* If that worked, return the string. */ \ - if (n > -1 && n < size) \ - break; \ - /* Else try again with more space. */ \ - if (n > -1) /* glibc 2.1 */ \ - size = n + 1; /* precisely what is needed */ \ - else /* glibc 2.0 */ \ - size *= 2; /* twice the old size */ \ - p = adu_realloc(p, size); \ - } \ -} - /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */ #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y))) diff --git a/string.c b/string.c index b089b78..4775175 100644 --- a/string.c +++ b/string.c @@ -123,10 +123,28 @@ __must_check __malloc char *adu_strdup(const char *s) */ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...) { - char *msg; + char *p; + int n; + size_t size = 100; - VSPRINTF(fmt, msg); - return msg; + p = adu_malloc(size); + while (1) { + va_list ap; + /* Try to print in the allocated space. */ + va_start(ap, fmt); + n = vsnprintf(p, size, fmt, ap); + va_end(ap); + /* If that worked, return the string. */ + if (n > -1 && n < size) + break; + /* Else try again with more space. */ + if (n > -1) /* glibc 2.1 */ + size = n + 1; /* precisely what is needed */ + else /* glibc 2.0 */ + size *= 2; /* twice the old size */ + p = adu_realloc(p, size); + } + return p; } /** -- 2.39.5