Escape arguments for Windows in shell-tools.c
authorIan Lynagh <igloo@earth.li>
Wed, 23 Jan 2008 15:17:24 +0000 (15:17 +0000)
committerIan Lynagh <igloo@earth.li>
Wed, 23 Jan 2008 15:17:24 +0000 (15:17 +0000)
includes/shell-tools.c

index 29b2e2d..4aebaf9 100644 (file)
@@ -59,7 +59,8 @@ int run(char *this, char *program, int argc, char** argv) {
     cmdline_len = 0;
     for(i = 1; i < argc; i++) {
         /* Note: play it safe and quote all argv strings */
-        cmdline_len += 1 + strlen(argv[i]) + 2;
+        /* In the worst case we have to escape every character with a \ */
+        cmdline_len += 1 + 2 * strlen(argv[i]) + 2;
     }
     new_cmdline = (char*)malloc(sizeof(char) * (cmdline_len + 1));
     if (!new_cmdline) {
@@ -73,6 +74,10 @@ int run(char *this, char *program, int argc, char** argv) {
         *ptr++ = '"';
         src = argv[i];
         while(*src) {
+            /* Escape any \ and " characters */
+            if ((*src == '\\') || (*src == '"')) {
+                *ptr++ = '\\';
+            }
             *ptr++ = *src++;
         }
         *ptr++ = '"';