MINOR: standard: Add memvprintf function

Now memprintf relies on memvprintf. This new function does exactly what
memprintf did before, but it must be called with a va_list instead of a variable
number of arguments. So there is no change for every functions using
memprintf. But it is now also possible to have same functionnality from any
function with variadic arguments.
This commit is contained in:
Christopher Faulet 2017-10-24 11:25:33 +02:00 committed by Willy Tarreau
parent 31dff9b1bd
commit 93a518f02a
2 changed files with 23 additions and 3 deletions

View File

@ -1072,7 +1072,13 @@ int parse_asctime_date(const char *date, int len, struct tm *tm);
* if (!fct2(err)) report(*err);
* if (!fct3(err)) report(*err);
* free(*err);
*
* memprintf relies on memvprintf. This last version can be called from any
* function with variadic arguments.
*/
char *memvprintf(char **out, const char *format, va_list args)
__attribute__ ((format(printf, 2, 0)));
char *memprintf(char **out, const char *format, ...)
__attribute__ ((format(printf, 2, 3)));

View File

@ -3309,8 +3309,11 @@ int parse_http_date(const char *date, int len, struct tm *tm)
* if (!fct2(err)) report(*err);
* if (!fct3(err)) report(*err);
* free(*err);
*
* memprintf relies on memvprintf. This last version can be called from any
* function with variadic arguments.
*/
char *memprintf(char **out, const char *format, ...)
char *memvprintf(char **out, const char *format, va_list orig_args)
{
va_list args;
char *ret = NULL;
@ -3325,10 +3328,9 @@ char *memprintf(char **out, const char *format, ...)
* target buffer is NULL. We do this in a loop just in case
* intermediate evaluations get wrong.
*/
va_start(args, format);
va_copy(args, orig_args);
needed = vsnprintf(ret, allocated, format, args);
va_end(args);
if (needed < allocated) {
/* Note: on Solaris 8, the first iteration always
* returns -1 if allocated is zero, so we force a
@ -3358,6 +3360,18 @@ char *memprintf(char **out, const char *format, ...)
return ret;
}
char *memprintf(char **out, const char *format, ...)
{
va_list args;
char *ret = NULL;
va_start(args, format);
ret = memvprintf(out, format, args);
va_end(args);
return ret;
}
/* Used to add <level> spaces before each line of <out>, unless there is only one line.
* The input argument is automatically freed and reassigned. The result will have to be
* freed by the caller. It also supports being passed a NULL which results in the same