aboutsummaryrefslogtreecommitdiffstats
path: root/common/stringop.c
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-25 00:02:28 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-25 00:14:13 +0100
commit32ae26e519eeb6a5108f1d82fe36ce5b6ef65af3 (patch)
treeb4f10872f5182c8db7fa9629d1567e34e8373ef1 /common/stringop.c
parentswaybar: move headers to include/bar (diff)
downloadsway-32ae26e519eeb6a5108f1d82fe36ce5b6ef65af3.tar.gz
sway-32ae26e519eeb6a5108f1d82fe36ce5b6ef65af3.tar.zst
sway-32ae26e519eeb6a5108f1d82fe36ce5b6ef65af3.zip
Add quotes to multiword arguments.
This adds quotes around multiword arguments before they are passed to `/bin/sh -c` in an exec command. Example: I connect to irc like this: exec termite -e "mosh server tmux a" Without this patch the arguments are passed to sh as: termite -e mosh server tmux a When it should be: termite -e "mosh server tmux a" For the command to work.
Diffstat (limited to 'common/stringop.c')
-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) {
273 return res; 273 return res;
274} 274}
275 275
276static bool has_whitespace(const char *str) {
277 while (*str) {
278 if (isspace(*str)) {
279 return true;
280 }
281 ++str;
282 }
283 return false;
284}
285
286/**
287 * Add quotes around any argv with whitespaces.
288 */
289void add_quotes(char **argv, int argc) {
290 int i;
291 for (i = 0; i < argc; ++i) {
292 if (has_whitespace(argv[i])) {
293 int len = strlen(argv[i]) + 3;
294 char *tmp = argv[i];
295 argv[i] = malloc(len * sizeof(char));
296 snprintf(argv[i], len, "\"%s\"", tmp);
297 free(tmp);
298 }
299 }
300}
301
276/* 302/*
277 * Join a list of strings, adding separator in between. Separator can be NULL. 303 * Join a list of strings, adding separator in between. Separator can be NULL.
278 */ 304 */