+static char* CmdAllocArg (const char* Arg, unsigned Len)
+/* Alloc (potentially quoted) argument */
+{
+ char* Alloc;
+
+/* The Microsoft docs say on spawnvp():
+** Spaces embedded in strings may cause unexpected behavior; for example,
+** passing _spawn the string "hi there" will result in the new process getting
+** two arguments, "hi" and "there". If the intent was to have the new process
+** open a file named "hi there", the process would fail. You can avoid this by
+** quoting the string: "\"hi there\"".
+*/
+#if defined(_WIN32)
+ /* Quote argument if it contains space(s) */
+ if (memchr (Arg, ' ', Len)) {
+ Alloc = xmalloc (Len + 3);
+ Alloc[0] = '"';
+ memcpy (Alloc + 1, Arg, Len);
+ Alloc[Len + 1] = '"';
+ Alloc[Len + 2] = '\0';
+ } else
+#endif
+ {
+ Alloc = xmalloc (Len + 1);
+ memcpy (Alloc, Arg, Len);
+ Alloc[Len] = '\0';
+ }
+ return Alloc;
+}
+
+
+
static void CmdExpand (CmdDesc* Cmd)
/* Expand the argument vector */
{
/* Add a copy of the new argument, allow a NULL pointer */
if (Arg) {
- Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
+ Cmd->Args[Cmd->ArgCount++] = CmdAllocArg (Arg, strlen (Arg));
} else {
Cmd->Args[Cmd->ArgCount++] = 0;
}
}
/* Add the new argument */
- Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
- Cmd->Args[Cmd->ArgCount][Len] = '\0';
- ++Cmd->ArgCount;
+ Cmd->Args[Cmd->ArgCount++] = CmdAllocArg (Arg, Len);
/* If the argument was terminated by a comma, skip it, otherwise
** we're done.
}
+
static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
/* Handle the --register-space option */
{