From 25a5cff86b73c54d8d0b6128240ecace87d365a2 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 30 Sep 2023 05:21:40 +0900 Subject: [PATCH] lib/strutils: add strfappend and strvfappend Signed-off-by: Masatake YAMATO --- include/strutils.h | 4 ++++ lib/strutils.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) 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; -- 2.47.2