diff --git a/modules/src/system/syssystem.c b/modules/src/system/syssystem.c index c86b0be65..6013ade8d 100644 --- a/modules/src/system/syssystem.c +++ b/modules/src/system/syssystem.c @@ -2,34 +2,79 @@ #include #include "system.h" +#if defined WIN32 + #define ESCAPECHAR '^' + + static int needs_escaping(char c) + { + switch (c) + { + case ' ': + case '"': + case '\'': + case '(': + case ')': + case '^': + return 1; + + default: + return 0; + } + } +#else + #define ESCAPECHAR '\\' + + static int needs_escaping(char c) + { + switch (c) + { + case ' ': + case '"': + case '\'': + case '\\': + case '(': + case ')': + return 1; + + default: + return 0; + } + } +#endif + +static char* append_escaped(char* buffer, const char* word) +{ + for (;;) + { + char c = *word++; + if (!c) + break; + if (needs_escaping(c)) + *buffer++ = ESCAPECHAR; + *buffer++ = c; + } + return buffer; +} + int sys_system(const char* prog, const char* const* arglist) { /* Calculate the maximum length of the command line. */ - int len = strlen(prog); + int len = strlen(prog) * 2 + 1; for (const char* const* arg = arglist+1; *arg; arg++) len += strlen(*arg) * 2 + 1; /* Now actually build the command line. */ char* cmdline = malloc(len + 1); - strcpy(cmdline, prog); - char* p = cmdline + strlen(prog); + char* p = append_escaped(cmdline, prog); for (const char* const* arg = arglist+1; *arg; arg++) { const char* word = *arg; *p++ = ' '; - for (;;) - { - char c = *word++; - if (!c) - break; - if ((c == ' ') || (c == '\"') || (c == '\'')) - *p++ = '\\'; - *p++ = c; - } + p = append_escaped(p, word); } *p = 0;