strv: add strv_copy_n() helper for copying part of a n strv

This commit is contained in:
Lennart Poettering
2023-02-17 09:55:35 +01:00
parent f4ff3e7149
commit 4ea517a6e0
3 changed files with 52 additions and 3 deletions
+8 -2
View File
@@ -77,20 +77,26 @@ char** strv_free_erase(char **l) {
return mfree(l);
}
char** strv_copy(char * const *l) {
char** strv_copy_n(char * const *l, size_t m) {
_cleanup_strv_free_ char **result = NULL;
char **k;
result = new(char*, strv_length(l) + 1);
result = new(char*, MIN(strv_length(l), m) + 1);
if (!result)
return NULL;
k = result;
STRV_FOREACH(i, l) {
if (m == 0)
break;
*k = strdup(*i);
if (!*k)
return NULL;
k++;
if (m != SIZE_MAX)
m--;
}
*k = NULL;