diff options
| author | Furkan Sahin <furkan-dev@proton.me> | 2019-01-22 11:43:37 +0000 |
|---|---|---|
| committer | Furkan Sahin <furkan-dev@proton.me> | 2019-01-22 11:43:37 +0000 |
| commit | 9942790554d4a1510f53bc6e2d21b6087a395318 (patch) | |
| tree | 89a75ecad4a25b16a6734593b094429f139cbc0e | |
| parent | fdccf2831746ba99f907847f9cbe249f2f2cc3db (diff) | |
swaybar: fix workspace command
Escape quotes and backslashes, allowing switching to workspace names
like "1" (including quotes) and \
| -rw-r--r-- | swaybar/ipc.c | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/swaybar/ipc.c b/swaybar/ipc.c index dbb593fb..46565202 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -13,10 +13,27 @@ #include "util.h" void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { - const char *fmt = "workspace \"%s\""; - uint32_t size = snprintf(NULL, 0, fmt, ws); - char *command = malloc(sizeof(char) * (size + 1)); - snprintf(command, size, fmt, ws); + uint32_t size = strlen("workspace \"\"") + strlen(ws); + for (size_t i = 0; i < strlen(ws); ++i) { + if (ws[i] == '"' || ws[i] == '\\') { + ++size; + } + } + + char *command = malloc(size) + 1; + if (!command) { + return; + } + + strcpy(command, "workspace \""); + strcpy(&command[size - 1], "\""); + for (size_t i = 0, d = strlen("workspace \""); i < strlen(ws); ++i) { + if (ws[i] == '"' || ws[i] == '\\') { + command[d++] = '\\'; + } + command[d++] = ws[i]; + } + ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size); free(command); } |
