X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=string.c;h=4775175dcca010ea723fcf2660a6be71fed7a3fe;hp=b089b785f96a8463bf850b408791987378e69e93;hb=3d29aa18911043879fcd4412fa25b0ab5f5e95bb;hpb=ba2d3fccbd565c9f5f8f652fcc300672e7bf09be;ds=sidebyside 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; } /**