util: add shell_maybe_quote() call for preparing a string for shell cmdline inclusion

If necessary the passed string is enclosed in "", and all special
characters escapes.

This also ports over usage in bus-util.c and job.c to use this, instead
of a incorrect local implementation that forgets to properly escape.
This commit is contained in:
Lennart Poettering
2015-04-09 18:32:21 +02:00
parent ab51b943d4
commit 019c7fba75
5 changed files with 74 additions and 15 deletions
+19
View File
@@ -1542,6 +1542,24 @@ static void test_sparse_write(void) {
test_sparse_write_one(fd, test_e, sizeof(test_e));
}
static void test_shell_maybe_quote_one(const char *s, const char *expected) {
_cleanup_free_ char *r;
assert_se(r = shell_maybe_quote(s));
assert_se(streq(r, expected));
}
static void test_shell_maybe_quote(void) {
test_shell_maybe_quote_one("", "");
test_shell_maybe_quote_one("\\", "\"\\\\\"");
test_shell_maybe_quote_one("\"", "\"\\\"\"");
test_shell_maybe_quote_one("foobar", "foobar");
test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
}
int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
@@ -1619,6 +1637,7 @@ int main(int argc, char *argv[]) {
test_same_fd();
test_uid_ptr();
test_sparse_write();
test_shell_maybe_quote();
return 0;
}