aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2016-01-24 18:33:06 -0500
committerDrew DeVault <sir@cmpwn.com>2016-01-24 18:33:06 -0500
commit55ac868898df8acc790dcd2ec56e294e89df5ea1 (patch)
treeb4f10872f5182c8db7fa9629d1567e34e8373ef1 /common
parented227f5664196d85194d63d01a5382499867a386 (diff)
parent32ae26e519eeb6a5108f1d82fe36ce5b6ef65af3 (diff)
Merge pull request #471 from mikkeloscar/fix-exec-quotes
Add quotes to multiword arguments.
Diffstat (limited to 'common')
-rw-r--r--common/stringop.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/common/stringop.c b/common/stringop.c
index 81d9b963..186fe121 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -273,6 +273,32 @@ char *join_args(char **argv, int argc) {
return res;
}
+static bool has_whitespace(const char *str) {
+ while (*str) {
+ if (isspace(*str)) {
+ return true;
+ }
+ ++str;
+ }
+ return false;
+}
+
+/**
+ * Add quotes around any argv with whitespaces.
+ */
+void add_quotes(char **argv, int argc) {
+ int i;
+ for (i = 0; i < argc; ++i) {
+ if (has_whitespace(argv[i])) {
+ int len = strlen(argv[i]) + 3;
+ char *tmp = argv[i];
+ argv[i] = malloc(len * sizeof(char));
+ snprintf(argv[i], len, "\"%s\"", tmp);
+ free(tmp);
+ }
+ }
+}
+
/*
* Join a list of strings, adding separator in between. Separator can be NULL.
*/