From: Ludwig Nussel Date: Tue, 9 Jan 2024 16:31:01 +0000 (+0100) Subject: strv: introduce strv_copy_unless_empty() X-Git-Tag: v256-rc1~1149^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5058bd7e1f0ae226d835253fcf333da3ab8d2806;p=thirdparty%2Fsystemd.git strv: introduce strv_copy_unless_empty() --- diff --git a/src/basic/strv.c b/src/basic/strv.c index 908e9e25139..fa17631aa0e 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -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; diff --git a/src/basic/strv.h b/src/basic/strv.h index f1a8bc49109..7361faed84c 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -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);