Index: /trunk/src/kash/shfile.c
===================================================================
--- /trunk/src/kash/shfile.c	(revision 2649)
+++ /trunk/src/kash/shfile.c	(revision 2650)
@@ -570,7 +570,17 @@
     {
 #ifdef SHFILE_IN_USE
+        /* Get CWD with unix slashes. */
         char buf[SHFILE_MAX_PATH];
         if (getcwd(buf, sizeof(buf)))
         {
+# if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
+            char *pszSlash = strchr(buf, '\\');
+            while (pszSlash)
+            {
+                *pszSlash = '/';
+                pszSlash = strchr(pszSlash + 1, '\\');
+            }
+# endif
+
             pfdtab->cwd = sh_strdup(NULL, buf);
             if (!inherit)
@@ -764,4 +774,48 @@
 
 /**
+ * Changes the inheritability of a file descriptro, taking console handles into
+ * account.
+ *
+ * @note    This MAY change the native handle for the entry.
+ *
+ * @returns The native handle.
+ * @param   pfd     The file descriptor to change.
+ * @param   set     If set, make child processes inherit the handle, if clear
+ *                  make them not inherit it.
+ */
+static HANDLE shfile_set_inherit_win(shfile *pfd, int set)
+{
+    HANDLE hFile = (HANDLE)pfd->native;
+    if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, set ? HANDLE_FLAG_INHERIT : 0))
+    {
+        /* SetHandleInformation doesn't work for console handles,
+           so we have to duplicate the handle to change the
+           inheritability. */
+        DWORD err = GetLastError();
+        if (   err == ERROR_INVALID_PARAMETER
+            && DuplicateHandle(GetCurrentProcess(),
+                               hFile,
+                               GetCurrentProcess(),
+                               &hFile,
+                               0,
+                               set ? TRUE : FALSE /* bInheritHandle */,
+                               DUPLICATE_SAME_ACCESS))
+        {
+            TRACE2((NULL, "shfile_set_inherit_win: %p -> %p (set=%d)\n", pfd->native, hFile, set));
+            if (!CloseHandle((HANDLE)pfd->native))
+                assert(0);
+            pfd->native = (intptr_t)hFile;
+        }
+        else
+        {
+            err = GetLastError();
+            assert(0);
+            hFile = (HANDLE)pfd->native;
+        }
+    }
+    return hFile;
+}
+
+/**
  * Helper for shfork.
  *
@@ -775,5 +829,4 @@
     shmtxtmp tmp;
     unsigned i;
-    DWORD fFlag = set ? HANDLE_FLAG_INHERIT : 0;
 
     shmtx_enter(&pfdtab->mtx, &tmp);
@@ -785,15 +838,8 @@
         if (pfdtab->tab[i].fd == i)
         {
-            HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
+            shfile_set_inherit_win(&pfdtab->tab[i], set);
             if (set)
                 TRACE2((NULL, "  #%d: native=%#x oflags=%#x shflags=%#x\n",
-                        i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
-            if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
-            {
-#if 0  /* Seems to happen for console handles, ignore it. */
-                DWORD err = GetLastError();
-                assert(0);
-#endif
-            }
+                        i, pfdtab->tab[i].native, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
         }
     }
@@ -861,15 +907,7 @@
                 &&  !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
             {
-                HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
+                HANDLE hFile = shfile_set_inherit_win(&pfdtab->tab[i], 1);
                 TRACE2((NULL, "  #%d: native=%#x oflags=%#x shflags=%#x\n",
                         i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
-
-                if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
-                {
-#if 0 /* Seems to fail for console handles, ignore. */
-                    DWORD err = GetLastError();
-                    assert(0);
-#endif
-                }
                 paf[i] = FOPEN;
                 if (pfdtab->tab[i].oflags & _O_APPEND)
@@ -914,12 +952,5 @@
             if (    pfdtab->tab[i].fd == i
                 &&  !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
-            {
-                HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
-                if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, 0))
-                {
-                    DWORD err = GetLastError();
-                    assert(0);
-                }
-            }
+                shfile_set_inherit_win(&pfdtab->tab[i], 0);
         }
         pvRet = NULL;
