Index: /trunk/src/kObjCache/kObjCache.c
===================================================================
--- /trunk/src/kObjCache/kObjCache.c	(revision 1043)
+++ /trunk/src/kObjCache/kObjCache.c	(revision 1044)
@@ -88,4 +88,14 @@
 #endif
 
+#ifndef STDIN_FILENO
+# define STDIN_FILENO 0
+#endif
+#ifndef STDOUT_FILENO
+# define STDOUT_FILENO 1
+#endif
+#ifndef STDERR_FILENO
+# define STDERR_FILENO 2
+#endif
+
 
 /*******************************************************************************
@@ -200,5 +210,5 @@
     void *pv = malloc(cb);
     if (!pv)
-        FatalDie(NULL, "out of memory (%d)\n", (int)cb);
+        FatalDie("out of memory (%d)\n", (int)cb);
     return pv;
 }
@@ -209,5 +219,5 @@
     void *pv = realloc(pvOld, cb);
     if (!pv)
-        FatalDie(NULL, "out of memory (%d)\n", (int)cb);
+        FatalDie("out of memory (%d)\n", (int)cb);
     return pv;
 }
@@ -218,5 +228,5 @@
     char *psz = strdup(pszIn);
     if (!psz)
-        FatalDie(NULL, "out of memory (%d)\n", (int)strlen(pszIn));
+        FatalDie("out of memory (%d)\n", (int)strlen(pszIn));
     return psz;
 }
@@ -532,6 +542,48 @@
 static int MakePath(const char *pszPath)
 {
-    /** @todo implement me */
-    return 0;
+    int iErr = 0;
+    char *pszAbsPath = AbsPath(pszPath);
+    char *psz = pszAbsPath;
+
+    /* Skip to the root slash (PC). */
+    while (!IS_SLASH(*psz) && *psz)
+        psz++;
+/** @todo UNC */
+    for (;;)
+    {
+        char chSaved;
+
+        /* skip slashes */
+        while (IS_SLASH(*psz))
+            psz++;
+        if (!*psz)
+            break;
+
+        /* find the next slash or end and terminate the string. */
+        while (!IS_SLASH(*psz) && *psz)
+            psz++;
+        chSaved = *psz;
+        *psz = '\0';
+
+        /* try create the directory, ignore failure because the directory already exists. */
+        errno = 0;
+#ifdef _MSC_VER
+        if (    _mkdir(pszAbsPath)
+            &&  errno != EEXIST)
+#else
+        if (    mkdir(pszAbsPath, 0777)
+            &&  errno != EEXIST)
+#endif
+        {
+            iErr = errno;
+            break;
+        }
+
+        /* restore the slash/terminator */
+        *psz = chSaved;
+    }
+
+    free(pszAbsPath);
+    return iErr ? -1 : 0;
 }
 
@@ -579,6 +631,6 @@
      */
     i = *pcArgs;
-    *pcArgs = i + cExtraArgs + 1 + !!pszWedgeArg;
-    papszArgs = xmalloc(*pcArgs * sizeof(char *));
+    *pcArgs = i + cExtraArgs + !!pszWedgeArg;
+    papszArgs = xmalloc((*pcArgs + 1) * sizeof(char *));
     *ppapszArgs = memcpy(papszArgs, *ppapszArgs, i * sizeof(char *));
 
@@ -589,4 +641,5 @@
     while (*psz)
     {
+        size_t cch;
         const char *pszEnd;
         while (isspace(*psz))
@@ -598,9 +651,14 @@
             pszEnd++;
 
-        papszArgs[i] = xmalloc(psz - pszEnd + 1);
-        memcpy(papszArgs[i], psz, psz - pszEnd);
-        papszArgs[i][psz - pszEnd] = '\0';
+        cch = pszEnd - psz;
+        papszArgs[i] = xmalloc(cch + 1);
+        memcpy(papszArgs[i], psz, cch);
+        papszArgs[i][cch] = '\0';
+
         i++;
-    }
+        psz = pszEnd;
+    }
+
+    papszArgs[i] = NULL;
 }
 
@@ -1462,6 +1520,6 @@
     {
         int fdReDir;
-        fdStdOut = dup(1); /* dup2(1,-1) doesn't work right on windows */
-        close(1);
+        fdStdOut = dup(STDOUT_FILENO);
+        close(STDOUT_FILENO);
         fdReDir = open(pszStdOut, O_CREAT | O_TRUNC | O_WRONLY, 0666);
         if (fdReDir < 0)
@@ -1469,7 +1527,7 @@
                      pszMsg, pszStdOut, strerror(errno));
 
-        if (fdReDir != 1)
-        {
-            if (dup2(fdReDir, 1) < 0)
+        if (fdReDir != STDOUT_FILENO)
+        {
+            if (dup2(fdReDir, STDOUT_FILENO) < 0)
                 FatalDie("%s - dup2 failed: %s\n", pszMsg, strerror(errno));
             close(fdReDir);
@@ -1485,6 +1543,6 @@
     if (fdStdOut)
     {
-        close(1);
-        fdStdOut = dup2(fdStdOut, 1);
+        close(STDOUT_FILENO);
+        fdStdOut = dup2(fdStdOut, STDOUT_FILENO);
         close(fdStdOut);
     }
@@ -1500,12 +1558,12 @@
             int fdReDir;
 
-            close(1);
+            close(STDOUT_FILENO);
             fdReDir = open(pszStdOut, O_CREAT | O_TRUNC | O_WRONLY, 0666);
             if (fdReDir < 0)
                 FatalDie("%s - failed to create stdout redirection file '%s': %s\n",
                          pszMsg, pszStdOut, strerror(errno));
-            if (fdReDir != 1)
+            if (fdReDir != STDOUT_FILENO)
             {
-                if (dup2(fdReDir, 1) < 0)
+                if (dup2(fdReDir, STDOUT_FILENO) < 0)
                     FatalDie("%s - dup2 failed: %s\n", pszMsg, strerror(errno));
                 close(fdReDir);
@@ -1554,8 +1612,8 @@
      * Setup redirection.
      */
-    if (fdStdOut != -1)
-    {
-        fdSavedStdOut = dup(1 /* stdout */);
-        if (dup2(fdStdOut, 1 /* stdout */) < 0)
+    if (fdStdOut != -1 && fdStdOut != STDOUT_FILENO)
+    {
+        fdSavedStdOut = dup(STDOUT_FILENO);
+        if (dup2(fdStdOut, STDOUT_FILENO) < 0)
             FatalDie("%s - dup2(,1) failed: %s\n", pszMsg, strerror(errno));
         close(fdStdOut);
@@ -1564,8 +1622,8 @@
 #endif
     }
-    if (fdStdIn != -1)
-    {
-        fdSavedStdIn = dup(0 /* stdin */);
-        if (dup2(fdStdOut, 0 /* stdin */) < 0)
+    if (fdStdIn != -1 && fdStdIn != STDIN_FILENO)
+    {
+        fdSavedStdIn = dup(STDIN_FILENO);
+        if (dup2(fdStdIn, STDIN_FILENO) < 0)
             FatalDie("%s - dup2(,0) failed: %s\n", pszMsg, strerror(errno));
         close(fdStdIn);
@@ -1598,17 +1656,18 @@
      * Restore stdout & stdin.
      */
-    if (fdSavedStdIn)
-    {
-        close(0 /* stdin */);
-        dup2(fdStdOut, 0 /* stdin */);
+    if (fdSavedStdIn != -1)
+    {
+        close(STDIN_FILENO);
+        dup2(fdStdOut, STDIN_FILENO);
         close(fdSavedStdIn);
     }
-    if (fdSavedStdOut)
-    {
-        close(1 /* stdout */);
-        dup2(fdSavedStdOut, 1 /* stdout */);
+    if (fdSavedStdOut != -1)
+    {
+        close(STDOUT_FILENO);
+        dup2(fdSavedStdOut, STDOUT_FILENO);
         close(fdSavedStdOut);
     }
 
+    InfoMsg(3, "%s - spawned %ld\n", pszMsg, (long)pid);
     (void)cArgv;
     (void)pEntry;
@@ -1628,4 +1687,6 @@
     int iStatus = -1;
     pid_t pidWait;
+    InfoMsg(3, "%s - wait-child(%ld)\n", pszMsg, (long)pid);
+
 #ifdef __WIN__
     pidWait = _cwait(&iStatus, pid, _WAIT_CHILD);
@@ -1658,4 +1719,5 @@
 static void kOCEntryCreatePipe(PKOCENTRY pEntry, int *pFDs, const char *pszMsg)
 {
+    pFDs[0] = pFDs[1] = -1;
 #if defined(__WIN__)
     if (_pipe(pFDs, 0, _O_NOINHERIT | _O_BINARY) < 0)
@@ -1748,5 +1810,5 @@
      */
     kOCEntryCreatePipe(pEntry, fds, pszMsg);
-    pidProducer = kOCEntrySpawnChild(pEntry, papszProdArgv, cProdArgv, fds[0 /* read */], -1, pszMsg);
+    pidProducer = kOCEntrySpawnChild(pEntry, papszConsArgv, cConsArgv, fds[0 /* read */], -1, pszMsg);
     fdOut = fds[1 /* write */];
 
@@ -2004,4 +2066,5 @@
         cbLeft -= cbWritten;
     }
+    close(fdOut);
 
     if (pEntry->fPipedPreComp)
@@ -2075,4 +2138,5 @@
     cbLeft = cbAlloc;
     pEntry->New.pszCppMapping = psz = xmalloc(cbAlloc);
+    InfoMsg(3, "precompiler|compile - starting passhtru...\n");
     for (;;)
     {
@@ -2090,4 +2154,5 @@
                      fdIn, (long)cbLeft, strerror(errno));
         }
+        InfoMsg(2, "precompiler|compile - read %d\n", cbRead);
 
         /*
@@ -2123,4 +2188,5 @@
         }
     }
+    InfoMsg(2, "precompiler|compile - done passhtru\n");
 
     close(fdIn);
@@ -3681,5 +3747,5 @@
         if (!pszCacheName)
         {
-            pszCacheName = FindFilenameInPath(pszCacheFile);
+            pszCacheName = FindFilenameInPath(pszEntryFile);
             if (!*pszCacheName)
                 return SyntaxError("The cache file (-f) specifies a directory / nothing!\n");
@@ -3689,5 +3755,5 @@
                 *psz = '\0';
         }
-        pszCacheFile = MakePathFromDirAndFile(pszCacheDir, pszCacheName); /* harmless leak. */
+        pszCacheFile = MakePathFromDirAndFile(pszCacheName, pszCacheDir);
     }
 
@@ -3762,4 +3828,5 @@
      * Update the cache files.
      */
+    kObjCacheRemoveEntry(pCache, pEntry);
     kObjCacheInsertEntry(pCache, pEntry);
     kOCEntryWrite(pEntry);
