]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: add strfappend and strvfappend
authorMasatake YAMATO <yamato@redhat.com>
Fri, 29 Sep 2023 20:21:40 +0000 (05:21 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Fri, 29 Sep 2023 20:29:15 +0000 (05:29 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
include/strutils.h
lib/strutils.c

index 07aa65681140629463bb6656a884b19b0a32d028..7ae8511ab838991b61509c52087868ddb43babe8 100644 (file)
@@ -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);
 
index 21751fd89e9179df65d2f301c11b047890c6967e..2bfc8ac6cfcb6667ed60748a153bd1bf30b9aa80 100644 (file)
@@ -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;