chiark / gitweb /
main: minor optimization
[elogind.git] / src / strv.c
index d9aef982246c0dc158b4a190ce8832a340cfc088..c8ff5745e82024bead8e6f997f1d1f0f4e0815b6 100644 (file)
@@ -355,7 +355,7 @@ char **strv_remove(char **l, const char *s) {
         if (!l)
                 return NULL;
 
-        /* Drops every occurence of s in the string list */
+        /* Drops every occurrence of s in the string list */
 
         for (f = t = l; *f; f++) {
 
@@ -379,8 +379,8 @@ static int env_append(char **r, char ***k, char **a) {
                 return 0;
 
         /* Add the entries of a to *k unless they already exist in *r
-         * in which case they are overriden instead. This assumes
-         * there is enough space in the r */
+         * in which case they are overridden instead. This assumes
+         * there is enough space in the r array. */
 
         for (; *a; a++) {
                 char **j;
@@ -474,7 +474,7 @@ char **strv_env_delete(char **x, unsigned n_lists, ...) {
         char **l, **k, **r, **j;
         va_list ap;
 
-        /* Deletes every entry fromx that is mentioned in the other
+        /* Deletes every entry from x that is mentioned in the other
          * string lists */
 
         n = strv_length(x);
@@ -556,3 +556,66 @@ char *strv_env_get_with_length(char **l, const char *name, size_t k) {
 char *strv_env_get(char **l, const char *name) {
         return strv_env_get_with_length(l, name, strlen(name));
 }
+
+char **strv_env_clean(char **l) {
+        char **r, **ret;
+
+        for (r = ret = l; *l; l++) {
+                const char *equal;
+
+                equal = strchr(*l, '=');
+
+                if (equal && equal[1] == 0) {
+                        free(*l);
+                        continue;
+                }
+
+                *(r++) = *l;
+        }
+
+        *r = NULL;
+
+        return ret;
+}
+
+char **strv_parse_nulstr(const char *s, size_t l) {
+        const char *p;
+        unsigned c = 0, i = 0;
+        char **v;
+
+        assert(s || l <= 0);
+
+        if (l <= 0)
+                return strv_new(NULL, NULL);
+
+        for (p = s; p < s + l; p++)
+                if (*p == 0)
+                        c++;
+
+        if (s[l-1] != 0)
+                c++;
+
+        if (!(v = new0(char*, c+1)))
+                return NULL;
+
+        p = s;
+        while (p < s + l) {
+                const char *e;
+
+                e = memchr(p, 0, s + l - p);
+
+                if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
+                        strv_free(v);
+                        return NULL;
+                }
+
+                if (!e)
+                        break;
+
+                p = e + 1;
+        }
+
+        assert(i == c);
+
+        return v;
+}