From b74ab9de899edd35738cd6b08693f3f2a9a17dae Mon Sep 17 00:00:00 2001 From: Oliver Schmidt Date: Fri, 8 Jun 2018 19:16:18 +0200 Subject: [PATCH] Allow spaces in path names. The Microsoft C Library needs to have spawnvp() parameters with spaces quoted manually. We do this only if actually necessary to limit issues with parameters already containing double quotes. --- src/cl65/main.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index f8acb57a0..118acfb03 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -192,6 +192,38 @@ static void DisableAssemblingAndLinking (void) +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 */ { @@ -215,7 +247,7 @@ static void CmdAddArg (CmdDesc* Cmd, const char* Arg) /* 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; } @@ -250,9 +282,7 @@ static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList) } /* 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. @@ -1187,6 +1217,7 @@ static void OptPrintTargetPath (const char* Opt attribute ((unused)), } + static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg) /* Handle the --register-space option */ { -- 2.39.2