Index: /trunk/src/kash/Makefile.kmk
===================================================================
--- /trunk/src/kash/Makefile.kmk	(revision 2375)
+++ /trunk/src/kash/Makefile.kmk	(revision 2376)
@@ -191,4 +191,13 @@
 endif
 
+#
+# For debugging file handle inheritance on Windows.
+#
+if "$(KBUILD_TARGET)" == win && 0
+PROGRAMS += tstDump
+tstDump_TEMPLATE = BIN
+tstDump_SOURCES = tstDump.c
+endif
+
 # Include the sub-makefile.
 include $(PATH_SUB_CURRENT)/tests/Makefile.kmk
Index: /trunk/src/kash/shfile.c
===================================================================
--- /trunk/src/kash/shfile.c	(revision 2375)
+++ /trunk/src/kash/shfile.c	(revision 2376)
@@ -548,5 +548,15 @@
                         if (hFile != INVALID_HANDLE_VALUE)
                         {
-                            int fd2 = shfile_insert(pfdtab, (intptr_t)hFile, aStdHandles[i].fFlags, 0, i, "shtab_init");
+                            DWORD       dwType  = GetFileType(hFile);
+                            unsigned    fFlags  = aStdHandles[i].fFlags;
+                            unsigned    fFlags2;
+                            int         fd2;
+                            if (dwType == FILE_TYPE_CHAR)
+                                fFlags2 = SHFILE_FLAGS_TTY;
+                            else if (dwType == FILE_TYPE_PIPE)
+                                fFlags2 = SHFILE_FLAGS_PIPE;
+                            else
+                                fFlags2 = SHFILE_FLAGS_FILE;
+                            fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init");
                             assert(fd2 == i); (void)fd2;
                             if (fd2 != i)
@@ -588,5 +598,5 @@
 
     shmtx_enter(&pfdtab->mtx, &tmp);
-    TRACE2((NULL, "shfile_fork_win:\n"));
+    TRACE2((NULL, "shfile_fork_win: set=%d\n", set));
 
     i = pfdtab->size;
@@ -598,5 +608,5 @@
             if (set)
                 TRACE2((NULL, "  #%d: native=%#x oflags=%#x shflags=%#x\n",
-                        i, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags, hFile));
+                        i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
             if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
             {
@@ -612,8 +622,10 @@
         {
             if (    pfdtab->size > i
-                &&  pfdtab->tab[i].fd == 0)
+                &&  pfdtab->tab[i].fd == i)
                 hndls[i] = pfdtab->tab[i].native;
             else
                 hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
+            TRACE2((NULL, "shfile_fork_win: i=%d size=%d fd=%d native=%d hndls[%d]=%p\n",
+                    i, pfdtab->size, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
         }
     }
@@ -644,10 +656,10 @@
 
     shmtx_enter(&pfdtab->mtx, &tmp);
-    TRACE2((NULL, "shfile_fork_win:\n"));
+    TRACE2((NULL, "shfile_exec_win: prepare=%p\n", prepare));
 
     count  = pfdtab->size < (0x10000-4) / (1 + sizeof(HANDLE))
            ? pfdtab->size
            : (0x10000-4) / (1 + sizeof(HANDLE));
-    while (count > 3 && pfdtab->tab[count].fd == -1)
+    while (count > 3 && pfdtab->tab[count - 1].fd == -1)
         count--;
 
@@ -669,5 +681,5 @@
                 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
                 TRACE2((NULL, "  #%d: native=%#x oflags=%#x shflags=%#x\n",
-                        i, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags, hFile));
+                        i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
 
                 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
@@ -697,9 +709,11 @@
         for (i = 0; i < 3; i++)
         {
-            if (    count > i
-                &&  pfdtab->tab[i].fd == 0)
+            if (    i < count
+                &&  pfdtab->tab[i].fd == i)
                 hndls[i] = pfdtab->tab[i].native;
             else
                 hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
+            TRACE2((NULL, "shfile_exec_win: i=%d count=%d fd=%d native=%d hndls[%d]=\n",
+                    i, count, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
         }
 
Index: /trunk/src/kash/shinstance.c
===================================================================
--- /trunk/src/kash/shinstance.c	(revision 2375)
+++ /trunk/src/kash/shinstance.c	(revision 2376)
@@ -899,4 +899,6 @@
 #if K_OS == K_OS_WINDOWS //&& defined(SH_FORKED_MODE)
     pid = shfork_do_it(psh);
+    if (pid == 0)
+        shthread_set_shell(psh);
 
 #elif defined(SH_FORKED_MODE)
@@ -1141,4 +1143,5 @@
 
         /* File handles. */
+        StrtInfo.dwFlags   |= STARTF_USESTDHANDLES;
         StrtInfo.lpReserved2 = shfile_exec_win(&psh->fdtab, 1 /* prepare */, &StrtInfo.cbReserved2, hndls);
         StrtInfo.hStdInput  = (HANDLE)hndls[0];
Index: /trunk/src/kash/show.c
===================================================================
--- /trunk/src/kash/show.c	(revision 2375)
+++ /trunk/src/kash/show.c	(revision 2376)
@@ -273,6 +273,6 @@
 # define TRY_GET_PSH_OR_RETURN(psh)  \
 	if (!(psh)) { \
-		psh = shthread_get_shell(); \
-		if (!psh) \
+		(psh) = shthread_get_shell(); \
+		if (!(psh)) \
 			return; \
 	} else do { } while (0)
Index: /trunk/src/kash/tstDump.c
===================================================================
--- /trunk/src/kash/tstDump.c	(revision 2376)
+++ /trunk/src/kash/tstDump.c	(revision 2376)
@@ -0,0 +1,75 @@
+/* $Id$ */
+/** @file
+ * tstDump - dump inherited file handle information on Windows.
+ */
+
+/*
+ * Copyright (c) 2010  knut st. osmundsen <bird-kBuild-spamix@anduin.net>
+ *
+ *
+ * This file is part of kBuild.
+ *
+ * kBuild is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * kBuild is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with kBuild; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include <Windows.h>
+#include <stdio.h>
+
+
+int main()
+{
+    STARTUPINFO Info;
+    GetStartupInfo(&Info);
+
+    printf("tst: hStdInput =%p / %p\n", Info.hStdInput,  GetStdHandle(STD_INPUT_HANDLE));
+    printf("tst: hStdOutput=%p / %p\n", Info.hStdOutput, GetStdHandle(STD_OUTPUT_HANDLE));
+    printf("tst: hStdError =%p / %p\n", Info.hStdError,  GetStdHandle(STD_ERROR_HANDLE));
+    printf("tst: cbReserved2=%d (%#x) lpReserved2=%p\n",
+           Info.cbReserved2, Info.cbReserved2, Info.lpReserved2);
+
+    if (Info.cbReserved2 > sizeof(int) && Info.lpReserved2)
+    {
+        int             count = *(int *)Info.lpReserved2;
+        unsigned char  *paf   = (unsigned char *)Info.lpReserved2 + sizeof(int);
+        HANDLE         *pah   = (HANDLE *)(paf + count);
+        int             i;
+
+        printf("tst: count=%d\n", count);
+        for (i = 0; i < count; i++)
+        {
+            if (paf[i] == 0 && pah[i] == INVALID_HANDLE_VALUE)
+                continue;
+
+            printf("tst: #%02d: native=%#p flags=%#x", i, pah[i], paf[i]);
+            if (paf[i] & 0x01)  printf(" FOPEN");
+            if (paf[i] & 0x02)  printf(" FEOFLAG");
+            if (paf[i] & 0x02)  printf(" FEOFLAG");
+            if (paf[i] & 0x04)  printf(" FCRLF");
+            if (paf[i] & 0x08)  printf(" FPIPE");
+            if (paf[i] & 0x10)  printf(" FNOINHERIT");
+            if (paf[i] & 0x20)  printf(" FAPPEND");
+            if (paf[i] & 0x40)  printf(" FDEV");
+            if (paf[i] & 0x80)  printf(" FTEXT");
+            printf("\n");
+        }
+    }
+
+    return 0;
+}
+
