Index: /trunk/src/kmk/main.c
===================================================================
--- /trunk/src/kmk/main.c	(revision 1125)
+++ /trunk/src/kmk/main.c	(revision 1126)
@@ -1576,61 +1576,4 @@
     }
 
-#ifdef KMK
-  /* If there wasn't any -C or -f flags, check for Makefile.kup and
-     insert a fake -C argument.
-     Makefile.kmk overrides Makefile.kup but not plain Makefile. */
-  if (makefiles == 0 && directories == 0)
-    {
-      struct stat st;
-      if ((   stat ("Makefile.kup", &st) == 0
-           && S_ISREG (st.st_mode) )
-       || (   stat ("makefile.kup", &st) == 0
-           && S_ISREG (st.st_mode) )
-       && stat ("Makefile.kmk", &st) < 0
-       && stat ("makefile.kmk", &st) < 0)
-        {
-          static char fake_path[3*16 + 32] = "..";
-          static const char *fake_list[2] = { &fake_path[0], NULL };
-          struct stringlist fake_directories = { &fake_list[0], 1, 0 };
-
-          char *cur = &fake_path[2];
-          int   up_levels = 1;
-
-          while (up_levels < 16)
-            {
-              /* File with higher precedence.s */
-              strcpy (cur, "/Makefile.kmk");
-              if (stat (fake_path, &st) == 0)
-                break;
-              strcpy (cur, "/makefile.kmk");
-              if (stat (fake_path, &st) == 0)
-                break;
-
-              /* the .kup files */
-              strcpy (cur, "/Makefile.kup");
-              if (   stat (fake_path, &st) != 0
-                  || !S_ISREG (st.st_mode))
-                {
-                  strcpy (cur, "/makefile.kup");
-                  if (   stat (fake_path, &st) != 0
-                      || !S_ISREG (st.st_mode))
-                    break;
-                }
-
-              /* ok */
-              strcpy (cur, "/..");
-              cur += 3;
-              up_levels++;
-            }
-
-          if (up_levels >= 16)
-            fatal (NILF, _("Makefile.kup recursion is too deep."));
-
-          *cur = '\0';
-          directories = &fake_directories;
-      }
-    }
-#endif /* KMK */
-
   /* If there were -C flags, move ourselves about.  */
   if (directories != 0)
@@ -1654,4 +1597,75 @@
         }
     }
+
+#ifdef KMK
+  /* Check for [Mm]akefile.kup and change directory when found.
+     Makefile.kmk overrides Makefile.kup but not plain Makefile. 
+     If no -C arguments were given, fake one to indicate chdir. */
+  if (makefiles == 0)
+    {
+      struct stat st;
+      if ((   stat ("Makefile.kup", &st) == 0
+           && S_ISREG (st.st_mode) )
+       || (   stat ("makefile.kup", &st) == 0
+           && S_ISREG (st.st_mode) )
+       && stat ("Makefile.kmk", &st) < 0
+       && stat ("makefile.kmk", &st) < 0)
+        {
+          static char fake_path[3*16 + 32] = "..";
+          char *cur = &fake_path[2];
+          int   up_levels = 1;
+          while (up_levels < 16)
+            {
+              /* File with higher precedence.s */
+              strcpy (cur, "/Makefile.kmk");
+              if (stat (fake_path, &st) == 0)
+                break;
+              strcpy (cur, "/makefile.kmk");
+              if (stat (fake_path, &st) == 0)
+                break;
+
+              /* the .kup files */
+              strcpy (cur, "/Makefile.kup");
+              if (   stat (fake_path, &st) != 0
+                  || !S_ISREG (st.st_mode))
+                {
+                  strcpy (cur, "/makefile.kup");
+                  if (   stat (fake_path, &st) != 0
+                      || !S_ISREG (st.st_mode))
+                    break;
+                }
+
+              /* ok */
+              strcpy (cur, "/..");
+              cur += 3;
+              up_levels++;
+            }
+
+          if (up_levels >= 16)
+            fatal (NILF, _("Makefile.kup recursion is too deep."));
+
+          /* attempt to change to the directory. */
+          *cur = '\0';
+          if (chdir (fake_path) < 0)
+            pfatal_with_name (fake_path);
+
+          /* add the string to the directories. */
+          if (!directories)
+            {
+              directories = xmalloc (sizeof(*directories));
+              directories->list = xmalloc (5 * sizeof (char *));
+              directories->max = 5;
+              directories->idx = 0;
+            }
+          else if (directories->idx == directories->max - 1)
+            {
+              directories->max += 5;
+              directories->list = xrealloc ((void *)directories->list,
+                                   directories->max * sizeof (char *));
+            }
+          directories->list[directories->idx++] = fake_path;
+        }
+    }
+#endif /* KMK */
 
 #ifdef WINDOWS32
