]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: Add memvprintf function
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 24 Oct 2017 09:25:33 +0000 (11:25 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 31 Oct 2017 10:36:12 +0000 (11:36 +0100)
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.

include/common/standard.h
src/standard.c

index 18d90566d14ba2ccec2442f95902c98751972c3c..59809b4c77698be142b647820877b4c04d2a9b47 100644 (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)));
 
index db312dd1892f48838da1215371b1233a44d2f920..77641b4271492620d02fad725eb46b187593ed62 100644 (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