X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=str.c;h=623def1fd9b7aa7ee72f221aa6759fcf74b70948;hp=ff8f02a4846af402c71b8a2818f2db7fceedfde8;hb=be3b0326d868aed97fc4224dcbf0266c303d2871;hpb=16f02580474d5f4a284863f48416a2a51fa78e85 diff --git a/str.c b/str.c index ff8f02a..623def1 100644 --- a/str.c +++ b/str.c @@ -1,8 +1,4 @@ -/* - * Copyright (C) 2004-2010 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ +/* SPDX-License-Identifier: GPL-2.0 */ #include #include #include @@ -20,36 +16,6 @@ #include "err.h" #include "str.h" -/** - * Write a 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 = dss_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 = dss_realloc(p, size); \ - } \ -} - /** * dss' version of realloc(). * @@ -165,9 +131,24 @@ __must_check __malloc char *dss_strdup(const char *s) __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...) { char *msg; + size_t size = 100; - VSPRINTF(fmt, msg); - return msg; + msg = dss_malloc(size); + while (1) { + int n; + va_list ap; + + /* Try to print in the allocated space. */ + va_start(ap, fmt); + n = vsnprintf(msg, size, fmt, ap); + va_end(ap); + /* If that worked, return the string. */ + if (n < size) + return msg; + /* Else try again with more space. */ + size = n + 1; /* precisely what is needed */ + msg = dss_realloc(msg, size); + } } /**