X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fstrv.c;h=ee45ad1d0f3bb434044462696f675432e28c3ed4;hb=113b3fc1a8061f4a24dd0db74e9a3cd0083b2251;hp=a5f8a2aff60e3ea36c82998c0f43e003717efbdb;hpb=0f84a72e3c0f58d71cff2121e6df1611eaf9c9ea;p=elogind.git
diff --git a/src/shared/strv.c b/src/shared/strv.c
index a5f8a2aff..ee45ad1d0 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -19,7 +19,6 @@
along with systemd; If not, see .
***/
-#include
#include
#include
#include
@@ -69,7 +68,7 @@ char *strv_find_startswith(char **l, const char *name) {
return NULL;
}
-void strv_free(char **l) {
+void strv_clear(char **l) {
char **k;
if (!l)
@@ -78,6 +77,11 @@ void strv_free(char **l) {
for (k = l; *k; k++)
free(*k);
+ *l = NULL;
+}
+
+void strv_free(char **l) {
+ strv_clear(l);
free(l);
}
@@ -388,7 +392,7 @@ int strv_push(char ***l, char *value) {
n = strv_length(*l);
- /* increase and check for overflow */
+ /* Increase and check for overflow */
m = n + 2;
if (m < n)
return -ENOMEM;
@@ -404,6 +408,34 @@ int strv_push(char ***l, char *value) {
return 0;
}
+int strv_push_pair(char ***l, char *a, char *b) {
+ char **c;
+ unsigned n, m;
+
+ if (!a && !b)
+ return 0;
+
+ n = strv_length(*l);
+
+ /* increase and check for overflow */
+ m = n + !!a + !!b + 1;
+ if (m < n)
+ return -ENOMEM;
+
+ c = realloc_multiply(*l, sizeof(char*), m);
+ if (!c)
+ return -ENOMEM;
+
+ if (a)
+ c[n++] = a;
+ if (b)
+ c[n++] = b;
+ c[n] = NULL;
+
+ *l = c;
+ return 0;
+}
+
int strv_push_prepend(char ***l, char *value) {
char **c;
unsigned n, m, i;
@@ -444,6 +476,18 @@ int strv_consume(char ***l, char *value) {
return r;
}
+int strv_consume_pair(char ***l, char *a, char *b) {
+ int r;
+
+ r = strv_push_pair(l, a, b);
+ if (r < 0) {
+ free(a);
+ free(b);
+ }
+
+ return r;
+}
+
int strv_consume_prepend(char ***l, char *value) {
int r;
@@ -479,6 +523,16 @@ char **strv_uniq(char **l) {
return l;
}
+bool strv_is_uniq(char **l) {
+ char **i;
+
+ STRV_FOREACH(i, l)
+ if (strv_find(i+1, *i))
+ return false;
+
+ return true;
+}
+
char **strv_remove(char **l, const char *s) {
char **f, **t;
@@ -619,3 +673,31 @@ int strv_extendf(char ***l, const char *format, ...) {
return strv_consume(l, x);
}
+
+char **strv_reverse(char **l) {
+ unsigned n, i;
+
+ n = strv_length(l);
+ if (n <= 1)
+ return l;
+
+ for (i = 0; i < n / 2; i++) {
+ char *t;
+
+ t = l[i];
+ l[i] = l[n-1-i];
+ l[n-1-i] = t;
+ }
+
+ return l;
+}
+
+bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
+ char* const* p;
+
+ STRV_FOREACH(p, patterns)
+ if (fnmatch(*p, s, 0) == 0)
+ return true;
+
+ return false;
+}