Index: trunk/src/kmk/kmkbuiltin/kSubmit.c
===================================================================
--- trunk/src/kmk/kmkbuiltin/kSubmit.c	(revision 3156)
+++ trunk/src/kmk/kmkbuiltin/kSubmit.c	(revision 3173)
@@ -1250,7 +1250,6 @@
     int             iArg;
     unsigned        cAllocatedEnvVars;
-    unsigned        iEnvVar;
     unsigned        cEnvVars;
-    char          **papszEnv            = NULL;
+    char          **papszEnvVars;
     const char     *pszExecutable       = NULL;
     int             iPostCmd            = argc;
@@ -1267,4 +1266,7 @@
     /*
      * Create default program environment.
+     *
+     * Note! We only clean up the environment on successful return, assuming
+     *       make will stop after that.
      */
     if (getcwd_fs(szCwd, cbCwdBuf) != NULL)
@@ -1273,11 +1275,10 @@
         return err(1, "getcwd_fs failed\n");
 
-    papszEnv = pChild->environment;
-    if (!papszEnv)
-        pChild->environment = papszEnv = target_environment(pChild->file);
+    /* The environment starts out in read-only mode and will be duplicated if modified. */
+    cAllocatedEnvVars = 0;
+    papszEnvVars = envp;
     cEnvVars = 0;
-    while (papszEnv[cEnvVars] != NULL)
+    while (papszEnvVars[cEnvVars] != NULL)
         cEnvVars++;
-    cAllocatedEnvVars = cEnvVars;
 
     /*
@@ -1387,33 +1388,29 @@
                     case 'Z':
                     case 'i': /* GNU env compatibility. */
-                        for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
-                            free(papszEnv[iEnvVar]);
-                        papszEnv[0] = NULL;
-                        cEnvVars = 0;
-                        break;
-
-                    case 'E':
-                        rcExit = kBuiltinOptEnvSet(&papszEnv, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
-                        pChild->environment = papszEnv;
+                        rcExit = kBuiltinOptEnvZap(&papszEnvVars, &cEnvVars, &cAllocatedEnvVars, cVerbosity);
                         if (rcExit == 0)
                             break;
                         return rcExit;
 
-                    case 'A':
-                        rcExit = kBuiltinOptEnvAppend(&papszEnv, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
-                        pChild->environment = papszEnv;
+                    case 'E':
+                        rcExit = kBuiltinOptEnvSet(&papszEnvVars, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
                         if (rcExit == 0)
                             break;
                         return rcExit;
 
-                    case 'D':
-                        rcExit = kBuiltinOptEnvPrepend(&papszEnv, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
-                        pChild->environment = papszEnv;
+                    case 'A':
+                        rcExit = kBuiltinOptEnvAppend(&papszEnvVars, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
                         if (rcExit == 0)
                             break;
                         return rcExit;
 
+                    case 'D':
+                        rcExit = kBuiltinOptEnvPrepend(&papszEnvVars, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
+                        if (rcExit == 0)
+                            break;
+                        return rcExit;
+
                     case 'U':
-                        rcExit = kBuiltinOptEnvUnset(papszEnv, &cEnvVars, cVerbosity, pszValue);
+                        rcExit = kBuiltinOptEnvUnset(&papszEnvVars, &cEnvVars, &cAllocatedEnvVars, cVerbosity, pszValue);
                         if (rcExit == 0)
                             break;
@@ -1458,7 +1455,9 @@
                     case 'h':
                         usage(stdout, argv[0]);
+                        kBuiltinOptEnvCleanup(&papszEnvVars, cEnvVars, &cAllocatedEnvVars);
                         return 0;
 
                     case 'V':
+                        kBuiltinOptEnvCleanup(&papszEnvVars, cEnvVars, &cAllocatedEnvVars);
                         return kbuild_version(argv[0]);
                 }
@@ -1478,5 +1477,5 @@
     {
         uint32_t        cbMsg;
-        void           *pvMsg   = kSubmitComposeJobMessage(pszExecutable, &argv[iArg], papszEnv, szCwd,
+        void           *pvMsg   = kSubmitComposeJobMessage(pszExecutable, &argv[iArg], papszEnvVars, szCwd,
                                                            fWatcomBrainDamage, fNoPchCaching,
                                                            &argv[iPostCmd], cPostCmdArgs, &cbMsg);
@@ -1505,4 +1504,5 @@
     }
 
+    kBuiltinOptEnvCleanup(&papszEnvVars, cEnvVars, &cAllocatedEnvVars);
     return rcExit;
 }
