Index: /trunk/src/kmk/Makefile.am
===================================================================
--- /trunk/src/kmk/Makefile.am	(revision 3028)
+++ /trunk/src/kmk/Makefile.am	(revision 3029)
@@ -57,4 +57,6 @@
 		../lib/md5.c \
 		../lib/kDep.c \
+		../lib/kbuild_version.c \
+		../lib/maybe_con_fwrite.c \
 		\
 		kmkbuiltin.c \
@@ -76,4 +78,5 @@
 		kmkbuiltin/mv.c \
 		kmkbuiltin/printf.c \
+		kmkbuiltin/redirect.c \
 		kmkbuiltin/rm.c \
 		kmkbuiltin/rmdir.c \
@@ -87,7 +90,12 @@
 		kmkbuiltin/osdep.c \
 		kmkbuiltin/kbuild_protection.c \
-		kmkbuiltin/kbuild_version.c
-
-kmk_redirect_SOURCES = kmkbuiltin/redirect.c
+		kmkbuiltin/common-env-and-cwd-opt.c
+
+kmk_redirect_SOURCES = kmkbuiltin/redirect.c \
+		kmkbuiltin/common-env-and-cwd-opt.c \
+		kmkbuiltin/err.c \
+		../lib/kbuild_version.c
+kmk_redirect_CFLAGS = -UKMK
+
 
 EXTRA_kmk_SOURCES = vmsjobs.c remote-stub.c remote-cstms.c
@@ -172,5 +180,5 @@
     @DEFS@
 
-AM_CPPFLAGS =	$(GLOBINC) -I$(srcdir)/../lib
+AM_CPPFLAGS =	$(GLOBINC) -I$(srcdir)/../lib -I$(srcdir)/../lib/kStuff/include
 # Only process if target is MS-Windows
 if WINDOWSENV
Index: /trunk/src/kmk/kmkbuiltin/redirect.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/redirect.c	(revision 3028)
+++ /trunk/src/kmk/kmkbuiltin/redirect.c	(revision 3029)
@@ -27,6 +27,6 @@
 *   Header Files                                                               *
 *******************************************************************************/
-#ifdef __APPLE__
-# define _POSIX_C_SOURCE 1 /* 10.4 sdk and unsetenv */
+#if defined(__APPLE__)
+/*# define _POSIX_C_SOURCE 1  / *  10.4 sdk and unsetenv  * / - breaks O_CLOEXEC on 10.8 */
 #endif
 #include "make.h"
@@ -90,5 +90,5 @@
 #endif /* _MSC_VER */
 
-
+#if 0 /* unused */
 /**
  * Safely works around MS CRT's pedantic close() function.
@@ -107,4 +107,5 @@
 #endif
 }
+#endif /* unused */
 
 
@@ -176,5 +177,5 @@
         kRedirectOrder_Close,
         kRedirectOrder_Open,
-        kRedirectOrder_Dup,
+        kRedirectOrder_Dup
     }           enmOrder;
     /** The target file handle. */
@@ -386,5 +387,5 @@
 #endif
     int         aFdTries[32];
-    int         cTries;
+    unsigned    cTries;
     int         fdOpened;
 
@@ -456,4 +457,33 @@
     return -1;
 }
+
+
+/**
+ * Cleans up the file operation orders.
+ *
+ * This does not restore stuff, just closes handles we've opened for the child.
+ *
+ * @param   cOrders         Number of file operation orders.
+ * @param   paOrders        The file operation orders.
+ * @param   fFailed         Set if it's a failure.
+ */
+static void kRedirectCleanupFdOrders(unsigned cOrders, REDIRECTORDERS *paOrders, KBOOL fFailure)
+{
+    unsigned i = cOrders;
+    while (i-- > 0)
+    {
+        if (   paOrders[i].enmOrder == kRedirectOrder_Open
+            && paOrders[i].fdSource != -1)
+        {
+            close(paOrders[i].fdSource);
+            paOrders[i].fdSource = -1;
+            if (   fFailure
+                && paOrders[i].fRemoveOnFailure
+                && paOrders[i].pszFilename)
+                remove(paOrders[i].pszFilename);
+        }
+    }
+}
+
 
 #ifndef USE_POSIX_SPAWN
@@ -574,32 +604,4 @@
 
 /**
- * Cleans up the file operation orders.
- *
- * This does not restore stuff, just closes handles we've opened for the guest.
- *
- * @param   cOrders         Number of file operation orders.
- * @param   paOrders        The file operation orders.
- * @param   fFailed         Set if it's a failure.
- */
-static void kRedirectCleanupFdOrders(unsigned cOrders, REDIRECTORDERS *paOrders, KBOOL fFailure)
-{
-    unsigned i = cOrders;
-    while (i-- > 0)
-    {
-        if (   paOrders[i].enmOrder == kRedirectOrder_Open
-            && paOrders[i].fdSource != -1)
-        {
-            close(paOrders[i].fdSource);
-            paOrders[i].fdSource = -1;
-            if (   fFailure
-                && paOrders[i].fRemoveOnFailure
-                && paOrders[i].pszFilename)
-                remove(paOrders[i].pszFilename);
-        }
-    }
-}
-
-
-/**
  * Restores the target file descriptors affected by the file operation orders.
  *
@@ -763,5 +765,5 @@
  * @param   fWatcomBrainDamage  Whether MSC need to do quoting according to
  *                              weird Watcom WCC rules.
- * @param   papszEnv            The child environment vector.
+ * @param   papszEnvVars        The child environment vector.
  * @param   pszCwd              The current working directory of the child.
  * @param   pszSavedCwd         The saved current working directory.  This is
@@ -777,5 +779,5 @@
  *                              is from the child or from our setup efforts.
  */
-static int kRedirectDoSpawn(const char *pszExecutable, int cArgs, char **papszArgs, int fWatcomBrainDamage, char **papszEnv,
+static int kRedirectDoSpawn(const char *pszExecutable, int cArgs, char **papszArgs, int fWatcomBrainDamage, char **papszEnvVars,
                             const char *pszCwd, const char *pszSavedCwd, unsigned cOrders, REDIRECTORDERS *paOrders,
 #ifdef USE_POSIX_SPAWN
@@ -863,5 +865,5 @@
 #if defined(KBUILD_OS_WINDOWS)
                 /* Windows is slightly complicated due to handles and sub_proc.c. */
-                HANDLE  hProcess = (HANDLE)_spawnvpe(_P_NOWAIT, pszExecutable, papszArgs, papszEnv);
+                HANDLE  hProcess = (HANDLE)_spawnvpe(_P_NOWAIT, pszExecutable, papszArgs, papszEnvVars);
                 kRedirectRestoreFdOrders(cOrders, paOrders, &pWorkingStdErr);
                 if ((intptr_t)hProcess != -1)
@@ -898,5 +900,5 @@
 
 # elif defined(KBUILD_OS_OS2)
-                *pPidSpawned = _spawnve(_P_NOWAIT, pszExecutable, papszArgs, papszEnv);
+                *pPidSpawned = _spawnve(_P_NOWAIT, pszExecutable, papszArgs, papszEnvVars);
                 kRedirectRestoreFdOrders(cOrders, paOrders, &pWorkingStdErr);
                 if (*pPidSpawned != -1)
@@ -930,5 +932,5 @@
 # if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
                 errno  = 0;
-                rcExit = (int)_spawnvpe(_P_WAIT, pszExecutable, papszArgs, papszEnv);
+                rcExit = (int)_spawnvpe(_P_WAIT, pszExecutable, papszArgs, papszEnvVars);
                 kRedirectRestoreFdOrders(cOrders, paOrders, &pWorkingStdErr);
                 if (rcExit != -1 || errno == 0)
@@ -1023,5 +1025,5 @@
     int             iArg;
     const char     *pszExecutable      = NULL;
-    char          **papszEnv           = NULL;
+    char          **papszEnvVars       = NULL;
     unsigned        cAllocatedEnvVars;
     unsigned        iEnvVar;
@@ -1057,9 +1059,9 @@
 #if defined(KMK)
     /* We get it from kmk and just count it:  */
-    papszEnv = pChild->environment;
-    if (!papszEnv)
-        pChild->environment = papszEnv = target_environment(pChild->file);
+    papszEnvVars = pChild->environment;
+    if (!papszEnvVars)
+        pChild->environment = papszEnvVars = target_environment(pChild->file);
     cEnvVars = 0;
-    while (papszEnv[cEnvVars] != NULL)
+    while (papszEnvVars[cEnvVars] != NULL)
         cEnvVars++;
     cAllocatedEnvVars = cEnvVars;
@@ -1071,18 +1073,18 @@
 
     cAllocatedEnvVars = cEnvVars + 4;
-    papszEnv = malloc((cAllocatedEnvVars + 1) * sizeof(papszEnv));
-    if (!papszEnv)
+    papszEnvVars = malloc((cAllocatedEnvVars + 1) * sizeof(papszEnvVars));
+    if (!papszEnvVars)
         return errx(9, "out of memory!");
 
     iEnvVar = cEnvVars;
-    papszEnv[iEnvVar] = NULL;
+    papszEnvVars[iEnvVar] = NULL;
     while (iEnvVar-- > 0)
     {
-        papszEnv[iEnvVar] = strdup(envp[iEnvVar]);
-        if (!papszEnv[iEnvVar])
+        papszEnvVars[iEnvVar] = strdup(envp[iEnvVar]);
+        if (!papszEnvVars[iEnvVar])
         {
             while (iEnvVar-- > 0)
-                free(papszEnv[iEnvVar]);
-            free(papszEnv);
+                free(papszEnvVars[iEnvVar]);
+            free(papszEnvVars);
             return errx(9, "out of memory!");
         }
@@ -1245,7 +1247,7 @@
                     if (pchEqual[1] != '\0')
                     {
-                        rcExit = kBuiltinOptEnvSet(&papszEnv, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
+                        rcExit = kBuiltinOptEnvSet(&papszEnvVars, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
 #ifdef KMK
-                        pChild->environment = papszEnv;
+                        pChild->environment = papszEnvVars;
 #endif
                     }
@@ -1256,5 +1258,5 @@
                         {
                             pszCopy[pchEqual - pszValue] = '\0';
-                            rcExit = kBuiltinOptEnvUnset(papszEnv, &cEnvVars, cVerbosity, pszCopy);
+                            rcExit = kBuiltinOptEnvUnset(papszEnvVars, &cEnvVars, cVerbosity, pszCopy);
                             free(pszCopy);
                         }
@@ -1280,5 +1282,5 @@
                 else
 #endif
-                    rcExit = kBuiltinOptEnvUnset(papszEnv, &cEnvVars, cVerbosity, pszValue);
+                    rcExit = kBuiltinOptEnvUnset(papszEnvVars, &cEnvVars, cVerbosity, pszValue);
                 continue;
             }
@@ -1291,6 +1293,6 @@
             {
                 for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
-                    free(papszEnv[iEnvVar]);
-                papszEnv[0] = NULL;
+                    free(papszEnvVars[iEnvVar]);
+                papszEnvVars[0] = NULL;
                 cEnvVars = 0;
                 continue;
@@ -1577,5 +1579,5 @@
 
 #ifdef USE_POSIX_SPAWN
-                    if (fdOpened != fdSource)
+                    if (fdOpened != fd)
                     {
                         rcExit = posix_spawn_file_actions_adddup2(&FileActions, fdOpened, fd);
@@ -1607,5 +1609,5 @@
          * Do the spawning in a separate function (main is far to large as it is by now).
          */
-        rcExit = kRedirectDoSpawn(pszExecutable, argc - iArg, &argv[iArg], fWatcomBrainDamage, papszEnv, szCwd, pszSavedCwd,
+        rcExit = kRedirectDoSpawn(pszExecutable, argc - iArg, &argv[iArg], fWatcomBrainDamage, papszEnvVars, szCwd, pszSavedCwd,
 #ifdef USE_POSIX_SPAWN
                                   cOrders, aOrders, &FileActions, cVerbosity,
@@ -1639,6 +1641,6 @@
     iEnvVar = cEnvVars;
     while (iEnvVar-- > 0)
-        free(papszEnv[iEnvVar]);
-    free(papszEnv);
+        free(papszEnvVars[iEnvVar]);
+    free(papszEnvVars);
 #endif
 #ifdef KBUILD_OS_OS2
