]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: introduce strv_extendf_with_size()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 31 Oct 2025 12:57:37 +0000 (21:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 31 Oct 2025 15:58:39 +0000 (00:58 +0900)
src/basic/strv.c
src/basic/strv.h
src/test/test-strv.c

index 37f4bd44f3f4c70fe46086756807dcf1935edc69..4f61a42114fdad69d7fb3409c6d1e8346f68d39d 100644 (file)
@@ -906,7 +906,7 @@ void strv_print_full(char * const *l, const char *prefix) {
                 printf("%s%s\n", strempty(prefix), *s);
 }
 
-int strv_extendf(char ***l, const char *format, ...) {
+int strv_extendf_with_size(char ***l, size_t *n, const char *format, ...) {
         va_list ap;
         char *x;
         int r;
@@ -918,7 +918,7 @@ int strv_extendf(char ***l, const char *format, ...) {
         if (r < 0)
                 return -ENOMEM;
 
-        return strv_consume(l, x);
+        return strv_consume_with_size(l, n, x);
 }
 
 char* startswith_strv(const char *s, char * const *l) {
index b358ab4c2b8f6d07c660a769a36d8b530693bac1..1133895158448359801acd5bf5fe9a42395c6c8e 100644 (file)
@@ -56,7 +56,8 @@ static inline int strv_extend(char ***l, const char *value) {
 int strv_extend_many_internal(char ***l, const char *value, ...);
 #define strv_extend_many(l, ...) strv_extend_many_internal(l, __VA_ARGS__, POINTER_MAX)
 
-int strv_extendf(char ***l, const char *format, ...) _printf_(2,3);
+int strv_extendf_with_size(char ***l, size_t *n, const char *format, ...) _printf_(3,4);
+#define strv_extendf(l, ...) strv_extendf_with_size(l, NULL, __VA_ARGS__)
 
 int strv_push_with_size(char ***l, size_t *n, char *value);
 static inline int strv_push(char ***l, char *value) {
index 88505993cddcafad825443aadd3de62c3f6f33be..49558850d618646741af0680060eb50d7113edb7 100644 (file)
@@ -749,6 +749,19 @@ TEST(strv_extendf) {
         ASSERT_STREQ(b[0], "test3 bar foo 128");
 }
 
+TEST(strv_extendf_with_size) {
+        _cleanup_strv_free_ char **a = NULL;
+        size_t n = 0;
+
+        ASSERT_OK(strv_extendf_with_size(&a, &n, "test2 %s %d %s", "foo", 128, "bar"));
+        ASSERT_OK(strv_extendf_with_size(&a, &n, "test3 %s %s %d", "bar", "foo", 128));
+
+        ASSERT_EQ(n, 2u);
+        ASSERT_EQ(strv_length(a), n);
+        ASSERT_STREQ(a[0], "test2 foo 128 bar");
+        ASSERT_STREQ(a[1], "test3 bar foo 128");
+}
+
 TEST(strv_foreach) {
         _cleanup_strv_free_ char **a;
         unsigned i = 0;