]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: introduce strv_copy_unless_empty()
authorLudwig Nussel <ludwig.nussel@suse.de>
Tue, 9 Jan 2024 16:31:01 +0000 (17:31 +0100)
committerLudwig Nussel <ludwig.nussel@suse.de>
Mon, 15 Jan 2024 16:16:37 +0000 (17:16 +0100)
src/basic/strv.c
src/basic/strv.h

index 908e9e251398a15e5e95b01622dd003c7d4b0f6c..fa17631aa0e575ec2562b464ec53d5e12766973a 100644 (file)
@@ -123,6 +123,22 @@ char** strv_copy_n(char * const *l, size_t m) {
         return TAKE_PTR(result);
 }
 
+int strv_copy_unless_empty(char * const *l, char ***ret) {
+        assert(ret);
+
+        if (strv_isempty(l)) {
+                *ret = NULL;
+                return 0;
+        }
+
+        char **copy = strv_copy(l);
+        if (!copy)
+                return -ENOMEM;
+
+        *ret = TAKE_PTR(copy);
+        return 1;
+}
+
 size_t strv_length(char * const *l) {
         size_t n = 0;
 
index f1a8bc49109540bfbbecf94d4ce9f52e997937b4..7361faed84c85d4b15f4d8205a0cd21e49b0bef3 100644 (file)
@@ -38,6 +38,8 @@ char** strv_copy_n(char * const *l, size_t n);
 static inline char** strv_copy(char * const *l) {
         return strv_copy_n(l, SIZE_MAX);
 }
+int strv_copy_unless_empty(char * const *l, char ***ret);
+
 size_t strv_length(char * const *l) _pure_;
 
 int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);