From: Masatake YAMATO Date: Fri, 29 Sep 2023 20:21:40 +0000 (+0900) Subject: lib/strutils: add strfappend and strvfappend X-Git-Tag: v2.40-rc1~224^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=25a5cff86b73c54d8d0b6128240ecace87d365a2;p=thirdparty%2Futil-linux.git lib/strutils: add strfappend and strvfappend Signed-off-by: Masatake YAMATO --- diff --git a/include/strutils.h b/include/strutils.h index 07aa656811..7ae8511ab8 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -394,6 +394,10 @@ extern char *strfconcat(const char *s, const char *format, ...) __attribute__ ((__format__ (__printf__, 2, 3))); extern int strappend(char **a, const char *b); +extern int strfappend(char **a, const char *format, ...) + __attribute__ ((__format__ (__printf__, 2, 3))); +extern int strvfappend(char **a, const char *format, va_list ap) + __attribute__ ((__format__ (__printf__, 2, 0))); extern const char *split(const char **state, size_t *l, const char *separator, int quoted); diff --git a/lib/strutils.c b/lib/strutils.c index 21751fd89e..2bfc8ac6cf 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -1010,6 +1010,34 @@ int strappend(char **a, const char *b) return 0; } +/* the hybrid version of strfconcat and strappend. */ +int strfappend(char **a, const char *format, ...) +{ + va_list ap; + int res; + + va_start(ap, format); + res = strvfappend(a, format, ap); + va_end(ap); + + return res; +} + +extern int strvfappend(char **a, const char *format, va_list ap) +{ + char *val; + int sz; + int res; + + sz = vasprintf(&val, format, ap); + if (sz < 0) + return -errno; + + res = strappend(a, val); + free(val); + return res; +} + static size_t strcspn_escaped(const char *s, const char *reject) { int escaped = 0;