return NULL;
}
+char *strv_find_startswith(char **l, const char *name) {
+ char **i, *e;
+
+ assert(name);
+
+ /* Like strv_find_prefix, but actually returns only the
+ * suffix, not the whole item */
+
+ STRV_FOREACH(i, l) {
+ e = startswith(*i, name);
+ if (e)
+ return e;
+ }
+
+ return NULL;
+}
+
void strv_free(char **l) {
char **k;
return r;
}
-char **strv_split_quoted(const char *s) {
+int strv_split_quoted(char ***t, const char *s) {
const char *word, *state;
size_t l;
unsigned n, i;
n = 0;
FOREACH_WORD_QUOTED(word, l, s, state)
n++;
- if (*state)
+ if (!isempty(state))
/* bad syntax */
- return NULL;
+ return -EINVAL;
r = new(char*, n+1);
if (!r)
- return NULL;
+ return -ENOMEM;
i = 0;
FOREACH_WORD_QUOTED(word, l, s, state) {
r[i] = cunescape_length(word, l);
if (!r[i]) {
strv_free(r);
- return NULL;
+ return -ENOMEM;
}
i++;
}
r[i] = NULL;
- return r;
+ *t = r;
+ return 0;
}
char **strv_split_newlines(const char *s) {
int strv_push(char ***l, char *value) {
char **c;
- unsigned n;
+ unsigned n, m;
if (!value)
return 0;
n = strv_length(*l);
- c = realloc(*l, sizeof(char*) * (n + 2));
+
+ /* increase and check for overflow */
+ m = n + 2;
+ if (m < n)
+ return -ENOMEM;
+
+ c = realloc_multiply(*l, sizeof(char*), m);
if (!c)
return -ENOMEM;
int strv_push_prepend(char ***l, char *value) {
char **c;
- unsigned n, i;
+ unsigned n, m, i;
if (!value)
return 0;
n = strv_length(*l);
- c = new(char*, n + 2);
+
+ /* increase and check for overflow */
+ m = n + 2;
+ if (m < n)
+ return -ENOMEM;
+
+ c = new(char*, m);
if (!c)
return -ENOMEM;