From 99009ed0f490b89fa14ca323a9ffb9b963980822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 11 Mar 2021 00:10:02 +0100 Subject: basic/process-util: add mode where posix shell escape is used for quoting The new flag is not used, except in tests, so no functional change yet. This way, the command as shown can be copied-and-pasted into the shell in more cases. For simple cases, shell quoting with "" is enough. But $'' is needed when there are control characters in the command. --- src/test/test-process-util.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/test/test-process-util.c') diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 957d23ffc5..f79df46d29 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -431,6 +431,7 @@ static void test_get_process_cmdline_harder(void) { #define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0" #define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\"" +#define EXPECT1p "foo $'\\'bar\\'' $'\"bar$\"' $'x y z' $'!``' \"\"" assert_se(lseek(fd, SEEK_SET, 0) == 0); assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1); assert_se(ftruncate(fd, sizeof CMDLINE1) == 0); @@ -441,8 +442,15 @@ static void test_get_process_cmdline_harder(void) { assert_se(streq(line, EXPECT1)); line = mfree(line); + assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0); + log_debug("got: ==%s==", line); + log_debug("exp: ==%s==", EXPECT1p); + assert_se(streq(line, EXPECT1p)); + line = mfree(line); + #define CMDLINE2 "foo\0\1\2\3\0\0" #define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\"" +#define EXPECT2p "foo $'\\001\\002\\003' \"\" \"\"" assert_se(lseek(fd, SEEK_SET, 0) == 0); assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2); assert_se(ftruncate(fd, sizeof CMDLINE2) == 0); @@ -453,6 +461,12 @@ static void test_get_process_cmdline_harder(void) { assert_se(streq(line, EXPECT2)); line = mfree(line); + assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE_POSIX, &line) >= 0); + log_debug("got: ==%s==", line); + log_debug("exp: ==%s==", EXPECT2p); + assert_se(streq(line, EXPECT2p)); + line = mfree(line); + safe_close(fd); _exit(EXIT_SUCCESS); } -- cgit v1.2.1