Index: /trunk/src/kWorker/Makefile.kmk
===================================================================
--- /trunk/src/kWorker/Makefile.kmk	(revision 2859)
+++ /trunk/src/kWorker/Makefile.kmk	(revision 2860)
@@ -64,5 +64,6 @@
        nt/ntstat.c \
        nt/ntunlink.c \
-       nt/kFsCache.c
+       nt/kFsCache.c \
+       quote_argv.c
 kbuild_version.c_DEFS = KBUILD_SVN_REV=$(KBUILD_SVN_REV)
 
Index: /trunk/src/kWorker/kWorker.c
===================================================================
--- /trunk/src/kWorker/kWorker.c	(revision 2859)
+++ /trunk/src/kWorker/kWorker.c	(revision 2860)
@@ -47,4 +47,5 @@
 
 #include "nt/kFsCache.h"
+#include "quote_argv.h"
 
 
@@ -4308,42 +4309,81 @@
 
 
+/**
+ * Creates a correctly quoted ANSI command line string from the given argv.
+ *
+ * @returns Pointer to the command line.
+ * @param   cArgs               Number of arguments.
+ * @param   papszArgs           The argument vector.
+ * @param   fWatcomBrainDamange Whether to apply watcom rules while quoting.
+ * @param   pcbCmdLine          Where to return the command line length,
+ *                              including one terminator.
+ */
+static char *kwSandboxInitCmdLineFromArgv(KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange, KSIZE *pcbCmdLine)
+{
+    KU32    i;
+    KSIZE   cbCmdLine;
+    char   *pszCmdLine;
+
+    /* Make a copy of the argument vector that we'll be quoting. */
+    char **papszQuotedArgs = alloca(sizeof(papszArgs[0]) * (cArgs + 1));
+    kHlpMemCopy(papszQuotedArgs, papszArgs, sizeof(papszArgs[0]) * (cArgs + 1));
+
+    /* Quote the arguments that need it. */
+    quote_argv(cArgs, papszQuotedArgs, fWatcomBrainDamange, 0 /*leak*/);
+
+    /* figure out cmd line length. */
+    cbCmdLine = 0;
+    for (i = 0; i < cArgs; i++)
+        cbCmdLine += strlen(papszQuotedArgs[i]) + 1;
+    *pcbCmdLine = cbCmdLine;
+
+    pszCmdLine = (char *)kHlpAlloc(cbCmdLine + 1);
+    if (pszCmdLine)
+    {
+        char *psz = kHlpStrPCopy(pszCmdLine, papszQuotedArgs[0]);
+        if (papszQuotedArgs[0] != papszArgs[0])
+            free(papszQuotedArgs[0]);
+
+        for (i = 1; i < cArgs; i++)
+        {
+            *psz++ = ' ';
+            psz = kHlpStrPCopy(psz, papszQuotedArgs[i]);
+            if (papszQuotedArgs[i] != papszArgs[i])
+                free(papszQuotedArgs[i]);
+        }
+        kHlpAssert((KSIZE)(&psz[1] - pszCmdLine) == cbCmdLine);
+
+        *psz++ = '\0';
+        *psz++ = '\0';
+    }
+
+    return pszCmdLine;
+}
+
+
 
 static int kwSandboxInit(PKWSANDBOX pSandbox, PKWTOOL pTool,
-                         KU32 cArgs, const char **papszArgs, KU32 cbArgs,
+                         KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
                          KU32 cEnvVars, const char **papszEnvVars)
 {
     PPEB pPeb = kwSandboxGetProcessEnvironmentBlock();
-    char *psz;
     wchar_t *pwcPool;
     KSIZE cbStrings;
     KSIZE cwc;
+    KSIZE cbCmdLine;
     KU32 i;
 
     /* Simple stuff. */
-    g_Sandbox.rcExitCode    = 256;
-    g_Sandbox.pTool         = pTool;
-    g_Sandbox.idMainThread  = GetCurrentThreadId();
-    g_Sandbox.TibMainThread = *(PNT_TIB)NtCurrentTeb();
-    g_Sandbox.pgmptr        = (char *)pTool->pszPath;
-    g_Sandbox.wpgmptr       = (wchar_t *)pTool->pwszPath;
-    g_Sandbox.cArgs         = cArgs;
-    g_Sandbox.papszArgs     = (char **)papszArgs;
-
-    /*
-     * Create a command line from the given argv.
-     * ASSUME that it's correctly quoted by quote_argv.c already.
-     */
-    pSandbox->pszCmdLine = psz = (char *)kHlpAlloc(cbArgs + 1);
-    if (!psz)
+    pSandbox->rcExitCode    = 256;
+    pSandbox->pTool         = pTool;
+    pSandbox->idMainThread  = GetCurrentThreadId();
+    pSandbox->TibMainThread = *(PNT_TIB)NtCurrentTeb();
+    pSandbox->pgmptr        = (char *)pTool->pszPath;
+    pSandbox->wpgmptr       = (wchar_t *)pTool->pwszPath;
+    pSandbox->cArgs         = cArgs;
+    pSandbox->papszArgs     = (char **)papszArgs;
+    pSandbox->pszCmdLine    = kwSandboxInitCmdLineFromArgv(cArgs, papszArgs, fWatcomBrainDamange, &cbCmdLine);
+    if (!pSandbox->pszCmdLine)
         return KERR_NO_MEMORY;
-    psz = kHlpStrPCopy(psz, papszArgs[0]);
-    for (i = 1; i < cArgs; i++)
-    {
-        *psz++ = ' ';
-        psz = kHlpStrPCopy(psz, papszArgs[i]);
-    }
-    kHlpAssert((KSIZE)(&psz[1] - pSandbox->pszCmdLine) == cbArgs);
-    psz[0] = '\0';
-    psz[1] = '\0';
 
     /*
@@ -4351,5 +4391,5 @@
      * We assume each ANSI char requires a surrogate pair in the UTF-16 variant.
      */
-    pSandbox->papwszArgs = (wchar_t **)kHlpAlloc(sizeof(wchar_t *) * (pSandbox->cArgs + 2) + cbArgs * 2 * sizeof(wchar_t));
+    pSandbox->papwszArgs = (wchar_t **)kHlpAlloc(sizeof(wchar_t *) * (pSandbox->cArgs + 2) + cbCmdLine * 2 * sizeof(wchar_t));
     if (!pSandbox->papwszArgs)
         return KERR_NO_MEMORY;
@@ -4368,5 +4408,5 @@
      * Convert the commandline string to UTF-16, same pessimistic approach as above.
      */
-    cbStrings = (cbArgs + 1) * 2 * sizeof(wchar_t);
+    cbStrings = (cbCmdLine + 1) * 2 * sizeof(wchar_t);
     pSandbox->pwszCmdLine = kHlpAlloc(cbStrings);
     if (!pSandbox->pwszCmdLine)
@@ -4415,5 +4455,5 @@
 
 
-static int kwSandboxExec(PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KU32 cbArgs,
+static int kwSandboxExec(PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
                          KU32 cEnvVars, const char **papszEnvVars)
 {
@@ -4424,5 +4464,5 @@
      * Initialize the sandbox environment.
      */
-    rc = kwSandboxInit(&g_Sandbox, pTool, cArgs, papszArgs, cbArgs, cEnvVars, papszEnvVars);
+    rc = kwSandboxInit(&g_Sandbox, pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);
     if (rc == 0)
     {
@@ -4531,10 +4571,10 @@
  * @param   cArgs           The number of arguments.
  * @param   papszArgs       The argument vector.
- * @param   cbArgs          The size of the argument strings and terminators.
+ * @param   fWatcomBrainDamange Whether to apply watcom rules while quoting.
  * @param   cEnvVars        The number of environment variables.
  * @param   papszEnvVars    The enviornment vector.
  */
 static int kSubmitHandleJobUnpacked(const char *pszExecutable, const char *pszCwd,
-                                    KU32 cArgs, const char **papszArgs, KU32 cbArgs,
+                                    KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
                                     KU32 cEnvVars, const char **papszEnvVars)
 {
@@ -4580,5 +4620,5 @@
                 {
                     KW_LOG(("Sandboxing tool %s\n", pTool->pszPath));
-                    rcExit = kwSandboxExec(pTool, cArgs, papszArgs, cbArgs, cEnvVars, papszEnvVars);
+                    rcExit = kwSandboxExec(pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);
                 }
                 else
@@ -4662,5 +4702,4 @@
                 if (papszArgs)
                 {
-                    KU32 cbArgs;
                     KU32 i;
                     for (i = 0; i < cArgs; i++)
@@ -4679,5 +4718,4 @@
                     }
                     papszArgs[cArgs] = 0;
-                    cbArgs = (KU32)(pszMsg - papszArgs[0]) - cArgs + 1;
 
                     /* Environment variable count. */
@@ -4718,9 +4756,10 @@
                                     if (cbMsg == 0)
                                     {
+                                        KBOOL fWatcomBrainDamange = K_FALSE; /** @todo fix fWatcomBrainDamange */
                                         /*
                                          * The next step.
                                          */
                                         rcExit = kSubmitHandleJobUnpacked(pszExecutable, pszCwd,
-                                                                          cArgs, papszArgs, cbArgs,
+                                                                          cArgs, papszArgs, fWatcomBrainDamange,
                                                                           cEnvVars, papszEnvVars);
                                     }
@@ -4853,5 +4892,4 @@
     char        szCwd[MAX_PATH];
     const char *pszCwd = getcwd(szCwd, sizeof(szCwd));
-    KU32        cbArgs;
     KU32        cEnvVars;
 
@@ -4898,8 +4936,4 @@
      * Do the job.
      */
-    cbArgs = 0;
-    for (j = i; j < argc; j++)
-        cbArgs += (KU32)strlen(argv[j]) + 1;
-
     cEnvVars = 0;
     while (environ[cEnvVars] != NULL)
@@ -4908,5 +4942,7 @@
     for (j = 0; j < cRepeats; j++)
     {
-        rcExit = kSubmitHandleJobUnpacked(argv[i], pszCwd, argc - i, &argv[i], cbArgs, cEnvVars, environ);
+        rcExit = kSubmitHandleJobUnpacked(argv[i], pszCwd,
+                                          argc - i, &argv[i], K_FALSE /* fWatcomBrainDamange*/,
+                                          cEnvVars, environ);
     }
 
