]> git.lizzy.rs Git - bspwm.git/blobdiff - src/helpers.c
Escape shell characters in the erc arguments
[bspwm.git] / src / helpers.c
index 57a82bdd8939da1ff9d1a7f760ac6f119cd28b5e..84199b68a7f28733f7009030a48716402b11b16c 100644 (file)
@@ -119,6 +119,35 @@ char *copy_string(char *str, size_t len)
        return cpy;
 }
 
+char *shell_escape(char *s)
+{
+       char *e = malloc(2 * strlen(s) + 1);
+
+       if (e == NULL) {
+               return NULL;
+       }
+
+       int c = 0;
+       int j = 0;
+
+       for (size_t i = 0; i < strlen(s); i++) {
+               if (s[i] == '\\') {
+                       c += 1;
+               } else {
+                       if (s[i] == '$' || s[i] == '(' || s[i] == ')') {
+                               if (c % 2 == 0) {
+                                       e[j++] = '\\';
+                               }
+                       }
+                       c = 0;
+               }
+               e[j++] = s[i];
+       }
+
+       e[j] = '\0';
+       return e;
+}
+
 char *mktempfifo(const char *template)
 {
        int tempfd;