From 78f1d33382e3777b11510d0a0b6d00a78261de68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bor=20Gro=C5=A1elj=20Simi=C4=87?= Date: Sun, 21 Apr 2019 10:41:38 +0200 Subject: [PATCH] swaymsg: quote arguments containing spaces --- swaymsg/main.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/swaymsg/main.c b/swaymsg/main.c index 518993af7..18198e695 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -443,7 +443,21 @@ int main(int argc, char **argv) { char *command = NULL; if (optind < argc) { - command = join_args(argv + optind, argc - optind); + char **commands = malloc(sizeof(char *) * (argc - optind)); + for (int i = 0; i < argc - optind; ++i) { + // quote arguments containing spaces + if (strchr(argv[optind + i], ' ')) { + size_t len = strlen(argv[optind + i]); + commands[i] = malloc(sizeof(char) * (len + 3)); + strcpy(commands[i], "\""); + strcat(commands[i], argv[optind + i]); + strcat(commands[i], "\""); + } else { + commands[i] = strdup(argv[optind + i]); + } + } + command = join_args(commands, argc - optind); + free_argv(argc - optind, commands); } else { command = strdup(""); }