summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/basic/strv.c20
-rw-r--r--src/basic/strv.h1
-rw-r--r--src/test/test-strv.c13
3 files changed, 34 insertions, 0 deletions
diff --git a/src/basic/strv.c b/src/basic/strv.c
index 79a9d8d421..eaf440a4b2 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -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;
diff --git a/src/basic/strv.h b/src/basic/strv.h
index 954da06fcb..f07da8cdf3 100644
--- a/src/basic/strv.h
+++ b/src/basic/strv.h
@@ -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);
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index 53074b4b65..bff43950a9 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -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;
}