]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: Add strv_shell_escape
authorRichard Maw <richard.maw@codethink.co.uk>
Tue, 23 Jun 2015 10:57:41 +0000 (10:57 +0000)
committerRichard Maw <richard.maw@codethink.co.uk>
Fri, 7 Aug 2015 15:50:43 +0000 (15:50 +0000)
This modifies the strv in-place, replacing strings with their escaped
version. It's mostly just a convenience function for when you need to
join a strv together because it's passed as a string to something, and
the separator needs escaping.

src/basic/strv.c
src/basic/strv.h
src/test/test-strv.c

index 79a9d8d42142541dfeec576a2df51d80c5a00197..eaf440a4b2bc7c168775bd48b3dfeb1c48613e01 100644 (file)
@@ -694,6 +694,26 @@ char **strv_reverse(char **l) {
         return l;
 }
 
+char **strv_shell_escape(char **l, const char *bad) {
+        char **s;
+
+        /* Escapes every character in every string in l that is in bad,
+         * edits in-place, does not roll-back on error. */
+
+        STRV_FOREACH(s, l) {
+                char *v;
+
+                v = shell_escape(*s, bad);
+                if (!v)
+                        return NULL;
+
+                free(*s);
+                *s = v;
+        }
+
+        return l;
+}
+
 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
         char* const* p;
 
index 954da06fcb54ef0f6ce2d2b2284ce2e292ed47fd..f07da8cdf3b7965251ef851278cd4e68ba1c5058 100644 (file)
@@ -145,6 +145,7 @@ void strv_print(char **l);
         }))
 
 char **strv_reverse(char **l);
+char **strv_shell_escape(char **l, const char *bad);
 
 bool strv_fnmatch(char* const* patterns, const char *s, int flags);
 
index 53074b4b653d37abaea893321e58462e0ce95ba0..bff43950a9077cd602eb6b29f48f3c05d60343ea 100644 (file)
@@ -557,6 +557,18 @@ static void test_strv_reverse(void) {
         assert_se(streq_ptr(d[3], NULL));
 }
 
+static void test_strv_shell_escape(void) {
+        _cleanup_strv_free_ char **v = NULL;
+
+        v = strv_new("foo:bar", "bar,baz", "wal\\do", NULL);
+        assert_se(v);
+        assert_se(strv_shell_escape(v, ",:"));
+        assert_se(streq_ptr(v[0], "foo\\:bar"));
+        assert_se(streq_ptr(v[1], "bar\\,baz"));
+        assert_se(streq_ptr(v[2], "wal\\\\do"));
+        assert_se(streq_ptr(v[3], NULL));
+}
+
 int main(int argc, char *argv[]) {
         test_specifier_printf();
         test_strv_foreach();
@@ -614,6 +626,7 @@ int main(int argc, char *argv[]) {
         test_strv_equal();
         test_strv_is_uniq();
         test_strv_reverse();
+        test_strv_shell_escape();
 
         return 0;
 }