enable better Win32::DomainName() by demand loading netapi32.dll
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
index cbe50c2..3f56f60 100644 (file)
 #endif
 #include <windows.h>
 
-#ifndef __MINGW32__
-#include <lmcons.h>
-#include <lmerr.h>
-/* ugliness to work around a buggy struct definition in lmwksta.h */
-#undef LPTSTR
-#define LPTSTR LPWSTR
-#include <lmwksta.h>
-#undef LPTSTR
-#define LPTSTR LPSTR
-#include <lmapibuf.h>
-#endif /* __MINGW32__ */
-
 /* #include "config.h" */
 
 #define PERLIO_NOT_STDIO 0 
@@ -40,6 +28,7 @@
 #include "perl.h"
 
 #define NO_XSLOCKS
+#define PERL_NO_GET_CONTEXT
 #include "XSUB.h"
 
 #include "Win32iop.h"
 int _CRT_glob = 0;
 #endif
 
+#if defined(__MINGW32__)
+#  define _stat stat
+#endif
+
+#if defined(__BORLANDC__)
+#  define _stat stat
+#  define _utimbuf utimbuf
+#endif
+
 #define EXECF_EXEC 1
 #define EXECF_SPAWN 2
 #define EXECF_SPAWN_NOWAIT 3
 
+#if defined(PERL_IMPLICIT_SYS)
+#  undef win32_get_privlib
+#  define win32_get_privlib g_win32_get_privlib
+#  undef win32_get_sitelib
+#  define win32_get_sitelib g_win32_get_sitelib
+#  undef do_spawn
+#  define do_spawn g_do_spawn
+#  undef getlogin
+#  define getlogin g_getlogin
+#endif
+
 #if defined(PERL_OBJECT)
-#undef win32_get_privlib
-#define win32_get_privlib g_win32_get_privlib
-#undef win32_get_sitelib
-#define win32_get_sitelib g_win32_get_sitelib
-#undef do_aspawn
-#define do_aspawn g_do_aspawn
-#undef do_spawn
-#define do_spawn g_do_spawn
-#undef Perl_do_exec
-#define Perl_do_exec g_do_exec
-#undef getlogin
-#define getlogin g_getlogin
+#  undef do_aspawn
+#  define do_aspawn g_do_aspawn
+#  undef Perl_do_exec
+#  define Perl_do_exec g_do_exec
 #endif
 
 static void            get_shell(void);
-static long            tokenize(char *str, char **dest, char ***destv);
-       int             do_spawn2(pTHX_ char *cmd, int exectype);
+static long            tokenize(const char *str, char **dest, char ***destv);
+       int             do_spawn2(char *cmd, int exectype);
 static BOOL            has_shell_metachars(char *ptr);
 static long            filetime_to_clock(PFILETIME ft);
 static BOOL            filetime_from_time(PFILETIME ft, time_t t);
-static char *          get_emd_part(SV *leading, char *trailing, ...);
+static char *          get_emd_part(SV **leading, char *trailing, ...);
 static void            remove_dead_process(long deceased);
 static long            find_pid(int pid);
 static char *          qualified_path(const char *cmd);
@@ -134,9 +135,9 @@ IsWinNT(void)
     return (win32_os_id() == VER_PLATFORM_WIN32_NT);
 }
 
-/* sv (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
+/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
 static char*
-get_regstr_from(HKEY hkey, const char *valuename, SV *sv)
+get_regstr_from(HKEY hkey, const char *valuename, SV **svp)
 {
     /* Retrieve a REG_SZ or REG_EXPAND_SZ from the registry */
     HKEY handle;
@@ -150,15 +151,15 @@ get_regstr_from(HKEY hkey, const char *valuename, SV *sv)
        DWORD datalen;
        retval = RegQueryValueEx(handle, valuename, 0, &type, NULL, &datalen);
        if (retval == ERROR_SUCCESS && type == REG_SZ) {
-           dPERLOBJ;
-           if (!sv)
-               sv = sv_2mortal(newSVpvn("",0));
-           SvGROW(sv, datalen);
+           dTHXo;
+           if (!*svp)
+               *svp = sv_2mortal(newSVpvn("",0));
+           SvGROW(*svp, datalen);
            retval = RegQueryValueEx(handle, valuename, 0, NULL,
-                                    (PBYTE)SvPVX(sv), &datalen);
+                                    (PBYTE)SvPVX(*svp), &datalen);
            if (retval == ERROR_SUCCESS) {
-               str = SvPVX(sv);
-               SvCUR_set(sv,datalen-1);
+               str = SvPVX(*svp);
+               SvCUR_set(*svp,datalen-1);
            }
        }
        RegCloseKey(handle);
@@ -166,19 +167,19 @@ get_regstr_from(HKEY hkey, const char *valuename, SV *sv)
     return str;
 }
 
-/* sv (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
+/* *svp (if non-NULL) is expected to be POK (valid allocated SvPVX(*svp)) */
 static char*
-get_regstr(const char *valuename, SV *sv)
+get_regstr(const char *valuename, SV **svp)
 {
-    char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, sv);
+    char *str = get_regstr_from(HKEY_CURRENT_USER, valuename, svp);
     if (!str)
-       str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, sv);
+       str = get_regstr_from(HKEY_LOCAL_MACHINE, valuename, svp);
     return str;
 }
 
-/* prev_path (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
+/* *prev_pathp (if non-NULL) is expected to be POK (valid allocated SvPVX(sv)) */
 static char *
-get_emd_part(SV *prev_path, char *trailing_path, ...)
+get_emd_part(SV **prev_pathp, char *trailing_path, ...)
 {
     char base[10];
     va_list ap;
@@ -202,9 +203,9 @@ get_emd_part(SV *prev_path, char *trailing_path, ...)
 
        /* try to get full path to binary (which may be mangled when perl is
         * run from a 16-bit app) */
-       /*PerlIO_printf(PerlIO_stderr(), "Before %s\n", w32_module_name);*/
+       /*PerlIO_printf(Perl_debug_log, "Before %s\n", w32_module_name);*/
        (void)win32_longpath(w32_module_name);
-       /*PerlIO_printf(PerlIO_stderr(), "After  %s\n", w32_module_name);*/
+       /*PerlIO_printf(Perl_debug_log, "After  %s\n", w32_module_name);*/
 
        /* normalize to forward slashes */
        ptr = w32_module_name;
@@ -246,38 +247,38 @@ get_emd_part(SV *prev_path, char *trailing_path, ...)
     /* only add directory if it exists */
     if (GetFileAttributes(mod_name) != (DWORD) -1) {
        /* directory exists */
-       dPERLOBJ;
-       if (!prev_path)
-           prev_path = sv_2mortal(newSVpvn("",0));
-       sv_catpvn(prev_path, ";", 1);
-       sv_catpv(prev_path, mod_name);
-       return SvPVX(prev_path);
+       dTHXo;
+       if (!*prev_pathp)
+           *prev_pathp = sv_2mortal(newSVpvn("",0));
+       sv_catpvn(*prev_pathp, ";", 1);
+       sv_catpv(*prev_pathp, mod_name);
+       return SvPVX(*prev_pathp);
     }
 
     return Nullch;
 }
 
 char *
-win32_get_privlib(pTHX_ char *pl)
+win32_get_privlib(char *pl)
 {
-    dPERLOBJ;
+    dTHXo;
     char *stdlib = "lib";
     char buffer[MAX_PATH+1];
     SV *sv = Nullsv;
 
     /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || "";  */
     sprintf(buffer, "%s-%s", stdlib, pl);
-    if (!get_regstr(buffer, sv))
-       (void)get_regstr(stdlib, sv);
+    if (!get_regstr(buffer, &sv))
+       (void)get_regstr(stdlib, &sv);
 
     /* $stdlib .= ";$EMD/../../lib" */
-    return get_emd_part(sv, stdlib, ARCHNAME, "bin", Nullch);
+    return get_emd_part(&sv, stdlib, ARCHNAME, "bin", Nullch);
 }
 
 char *
-win32_get_sitelib(pTHX_ char *pl)
+win32_get_sitelib(char *pl)
 {
-    dPERLOBJ;
+    dTHXo;
     char *sitelib = "sitelib";
     char regstr[40];
     char pathstr[MAX_PATH+1];
@@ -288,25 +289,25 @@ win32_get_sitelib(pTHX_ char *pl)
 
     /* $HKCU{"sitelib-$]"} || $HKLM{"sitelib-$]"} . ---; */
     sprintf(regstr, "%s-%s", sitelib, pl);
-    (void)get_regstr(regstr, sv1);
+    (void)get_regstr(regstr, &sv1);
 
     /* $sitelib .=
      * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/$]/lib";  */
     sprintf(pathstr, "site/%s/lib", pl);
-    (void)get_emd_part(sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
+    (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
     if (!sv1 && strlen(pl) == 7) {
        /* pl may have been SUBVERSION-specific; try again without
         * SUBVERSION */
        sprintf(pathstr, "site/%.5s/lib", pl);
-       (void)get_emd_part(sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
+       (void)get_emd_part(&sv1, pathstr, ARCHNAME, "bin", pl, Nullch);
     }
 
     /* $HKCU{'sitelib'} || $HKLM{'sitelib'} . ---; */
-    (void)get_regstr(sitelib, sv2);
+    (void)get_regstr(sitelib, &sv2);
 
     /* $sitelib .=
      * ";$EMD/" . ((-d $EMD/../../../$]) ? "../../.." : "../.."). "/site/lib";  */
-    (void)get_emd_part(sv2, "site/lib", ARCHNAME, "bin", pl, Nullch);
+    (void)get_emd_part(&sv2, "site/lib", ARCHNAME, "bin", pl, Nullch);
 
     if (!sv1 && !sv2)
        return Nullch;
@@ -363,7 +364,7 @@ has_shell_metachars(char *ptr)
     return FALSE;
 }
 
-#if !defined(PERL_OBJECT)
+#if !defined(PERL_IMPLICIT_SYS)
 /* since the current process environment is being updated in util.c
  * the library functions will get the correct environment
  */
@@ -418,13 +419,13 @@ win32_os_id(void)
  * Returns number of words in result buffer.
  */
 static long
-tokenize(char *str, char **dest, char ***destv)
+tokenize(const char *str, char **dest, char ***destv)
 {
     char *retstart = Nullch;
     char **retvstart = 0;
     int items = -1;
     if (str) {
-       dPERLOBJ;
+       dTHXo;
        int slen = strlen(str);
        register char *ret;
        register char **retv;
@@ -467,7 +468,7 @@ tokenize(char *str, char **dest, char ***destv)
 static void
 get_shell(void)
 {
-    dPERLOBJ;
+    dTHXo;
     if (!w32_perlshell_tokens) {
        /* we don't use COMSPEC here for two reasons:
         *  1. the same reason perl on UNIX doesn't use SHELL--rampant and
@@ -476,8 +477,9 @@ get_shell(void)
         *     interactive use (which is what most programs look in COMSPEC
         *     for).
         */
-       char* defaultshell = (IsWinNT() ? "cmd.exe /x/c" : "command.com /c");
-       char *usershell = getenv("PERL5SHELL");
+       const char* defaultshell = (IsWinNT()
+                                   ? "cmd.exe /x/c" : "command.com /c");
+       const char *usershell = getenv("PERL5SHELL");
        w32_perlshell_items = tokenize(usershell ? usershell : defaultshell,
                                       &w32_perlshell_tokens,
                                       &w32_perlshell_vec);
@@ -485,9 +487,9 @@ get_shell(void)
 }
 
 int
-do_aspawn(pTHX_ void *vreally, void **vmark, void **vsp)
+do_aspawn(void *vreally, void **vmark, void **vsp)
 {
-    dPERLOBJ;
+    dTHXo;
     SV *really = (SV*)vreally;
     SV **mark = (SV**)vmark;
     SV **sp = (SV**)vsp;
@@ -550,9 +552,9 @@ do_aspawn(pTHX_ void *vreally, void **vmark, void **vsp)
 }
 
 int
-do_spawn2(pTHX_ char *cmd, int exectype)
+do_spawn2(char *cmd, int exectype)
 {
-    dPERLOBJ;
+    dTHXo;
     char **a;
     char *s;
     char **argv;
@@ -640,21 +642,21 @@ do_spawn2(pTHX_ char *cmd, int exectype)
 }
 
 int
-do_spawn(pTHX_ char *cmd)
+do_spawn(char *cmd)
 {
-    return do_spawn2(aTHX_ cmd, EXECF_SPAWN);
+    return do_spawn2(cmd, EXECF_SPAWN);
 }
 
 int
-do_spawn_nowait(pTHX_ char *cmd)
+do_spawn_nowait(char *cmd)
 {
-    return do_spawn2(aTHX_ cmd, EXECF_SPAWN_NOWAIT);
+    return do_spawn2(cmd, EXECF_SPAWN_NOWAIT);
 }
 
 bool
 Perl_do_exec(pTHX_ char *cmd)
 {
-    do_spawn2(aTHX_ cmd, EXECF_EXEC);
+    do_spawn2(cmd, EXECF_EXEC);
     return FALSE;
 }
 
@@ -662,12 +664,11 @@ Perl_do_exec(pTHX_ char *cmd)
  * (separated by nulls) and when one of the other dir functions is called
  * return the pointer to the current file name.
  */
-DIR *
+DllExport DIR *
 win32_opendir(char *filename)
 {
-    dTHX;
-    dPERLOBJ;
-    DIR                        *p;
+    dTHXo;
+    DIR                        *dirp;
     long               len;
     long               idx;
     char               scanname[MAX_PATH+3];
@@ -677,7 +678,7 @@ win32_opendir(char *filename)
     HANDLE             fh;
     char               buffer[MAX_PATH*2];
     WCHAR              wbuffer[MAX_PATH];
-    char*              ptr;            
+    char*              ptr;
 
     len = strlen(filename);
     if (len > MAX_PATH)
@@ -688,9 +689,7 @@ win32_opendir(char *filename)
        return NULL;
 
     /* Get us a DIR structure */
-    Newz(1303, p, 1, DIR);
-    if (p == NULL)
-       return NULL;
+    Newz(1303, dirp, 1, DIR);
 
     /* Create the search pattern */
     strcpy(scanname, filename);
@@ -714,11 +713,25 @@ win32_opendir(char *filename)
     else {
        fh = FindFirstFileA(scanname, &aFindData);
     }
+    dirp->handle = fh;
     if (fh == INVALID_HANDLE_VALUE) {
+       DWORD err = GetLastError();
        /* FindFirstFile() fails on empty drives! */
-       if (GetLastError() == ERROR_FILE_NOT_FOUND)
-           return p;
-       Safefree( p);
+       switch (err) {
+       case ERROR_FILE_NOT_FOUND:
+           return dirp;
+       case ERROR_NO_MORE_FILES:
+       case ERROR_PATH_NOT_FOUND:
+           errno = ENOENT;
+           break;
+       case ERROR_NOT_ENOUGH_MEMORY:
+           errno = ENOMEM;
+           break;
+       default:
+           errno = EINVAL;
+           break;
+       }
+       Safefree(dirp);
        return NULL;
     }
 
@@ -733,50 +746,26 @@ win32_opendir(char *filename)
        ptr = aFindData.cFileName;
     }
     idx = strlen(ptr)+1;
-    New(1304, p->start, idx, char);
-    if (p->start == NULL)
-       Perl_croak_nocontext("opendir: malloc failed!\n");
-    strcpy(p->start, ptr);
-    p->nfiles++;
-
-    /* loop finding all the files that match the wildcard
-     * (which should be all of them in this directory!).
-     * the variable idx should point one past the null terminator
-     * of the previous string found.
-     */
-    while (USING_WIDE()
-           ? FindNextFileW(fh, &wFindData)
-           : FindNextFileA(fh, &aFindData)) {
-       if (USING_WIDE()) {
-       W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
-       }
-       /* ptr is set above to the correct area */
-       len = strlen(ptr);
-       /* bump the string table size by enough for the
-        * new name and it's null terminator
-        */
-       Renew(p->start, idx+len+1, char);
-       if (p->start == NULL)
-           Perl_croak_nocontext("opendir: malloc failed!\n");
-       strcpy(&p->start[idx], ptr);
-       p->nfiles++;
-       idx += len+1;
-    }
-    FindClose(fh);
-    p->size = idx;
-    p->curr = p->start;
-    return p;
+    if (idx < 256)
+       dirp->size = 128;
+    else
+       dirp->size = idx;
+    New(1304, dirp->start, dirp->size, char);
+    strcpy(dirp->start, ptr);
+    dirp->nfiles++;
+    dirp->end = dirp->curr = dirp->start;
+    dirp->end += idx;
+    return dirp;
 }
 
 
 /* Readdir just returns the current string pointer and bumps the
  * string pointer to the nDllExport entry.
  */
-struct direct *
+DllExport struct direct *
 win32_readdir(DIR *dirp)
 {
-    int         len;
-    static int  dummy = 0;
+    long         len;
 
     if (dirp->curr) {
        /* first set up the structure to return */
@@ -785,14 +774,51 @@ win32_readdir(DIR *dirp)
        dirp->dirstr.d_namlen = len;
 
        /* Fake an inode */
-       dirp->dirstr.d_ino = dummy++;
+       dirp->dirstr.d_ino = dirp->curr - dirp->start;
 
-       /* Now set up for the nDllExport call to readdir */
+       /* Now set up for the next call to readdir */
        dirp->curr += len + 1;
-       if (dirp->curr >= (dirp->start + dirp->size)) {
-           dirp->curr = NULL;
+       if (dirp->curr >= dirp->end) {
+           dTHXo;
+           char*               ptr;
+           BOOL                res;
+           WIN32_FIND_DATAW    wFindData;
+           WIN32_FIND_DATAA    aFindData;
+           char                buffer[MAX_PATH*2];
+
+           /* finding the next file that matches the wildcard
+            * (which should be all of them in this directory!).
+            */
+           if (USING_WIDE()) {
+               res = FindNextFileW(dirp->handle, &wFindData);
+               if (res) {
+                   W2AHELPER(wFindData.cFileName, buffer, sizeof(buffer));
+                   ptr = buffer;
+               }
+           }
+           else {
+               res = FindNextFileA(dirp->handle, &aFindData);
+               if (res)
+                   ptr = aFindData.cFileName;
+           }
+           if (res) {
+               long endpos = dirp->end - dirp->start;
+               long newsize = endpos + strlen(ptr) + 1;
+               /* bump the string table size by enough for the
+                * new name and it's null terminator */
+               while (newsize > dirp->size) {
+                   long curpos = dirp->curr - dirp->start;
+                   dirp->size *= 2;
+                   Renew(dirp->start, dirp->size, char);
+                   dirp->curr = dirp->start + curpos;
+               }
+               strcpy(dirp->start + endpos, ptr);
+               dirp->end = dirp->start + newsize;
+               dirp->nfiles++;
+           }
+           else
+               dirp->curr = NULL;
        }
-
        return &(dirp->dirstr);
     } 
     else
@@ -800,34 +826,36 @@ win32_readdir(DIR *dirp)
 }
 
 /* Telldir returns the current string pointer position */
-long
+DllExport long
 win32_telldir(DIR *dirp)
 {
-    return (long) dirp->curr;
+    return (dirp->curr - dirp->start);
 }
 
 
 /* Seekdir moves the string pointer to a previously saved position
- *(Saved by telldir).
+ * (returned by telldir).
  */
-void
+DllExport void
 win32_seekdir(DIR *dirp, long loc)
 {
-    dirp->curr = (char *)loc;
+    dirp->curr = dirp->start + loc;
 }
 
 /* Rewinddir resets the string pointer to the start */
-void
+DllExport void
 win32_rewinddir(DIR *dirp)
 {
     dirp->curr = dirp->start;
 }
 
 /* free the memory allocated by opendir */
-int
+DllExport int
 win32_closedir(DIR *dirp)
 {
-    dPERLOBJ;
+    dTHXo;
+    if (dirp->handle != INVALID_HANDLE_VALUE)
+       FindClose(dirp->handle);
     Safefree(dirp->start);
     Safefree(dirp);
     return 1;
@@ -887,7 +915,7 @@ setgid(gid_t agid)
 char *
 getlogin(void)
 {
-    dTHX;
+    dTHXo;
     char *buf = getlogin_buffer;
     DWORD size = sizeof(getlogin_buffer);
     if (GetUserName(buf,&size))
@@ -905,7 +933,7 @@ chown(const char *path, uid_t owner, gid_t group)
 static long
 find_pid(int pid)
 {
-    dPERLOBJ;
+    dTHXo;
     long child;
     for (child = 0 ; child < w32_num_children ; ++child) {
        if (w32_child_pids[child] == pid)
@@ -918,7 +946,7 @@ static void
 remove_dead_process(long child)
 {
     if (child >= 0) {
-       dPERLOBJ;
+       dTHXo;
        CloseHandle(w32_child_handles[child]);
        Copy(&w32_child_handles[child+1], &w32_child_handles[child],
             (w32_num_children-child-1), HANDLE);
@@ -956,7 +984,7 @@ win32_sleep(unsigned int t)
 DllExport int
 win32_stat(const char *path, struct stat *buffer)
 {
-    dPERLOBJ;
+    dTHXo;
     char       t[MAX_PATH+1]; 
     int                l = strlen(path);
     int                res;
@@ -983,7 +1011,6 @@ win32_stat(const char *path, struct stat *buffer)
        }
     }
     if (USING_WIDE()) {
-       dTHX;
        A2WHELPER(path, wbuffer, sizeof(wbuffer));
        res = _wstat(wbuffer, (struct _stat *)buffer);
     }
@@ -1114,7 +1141,7 @@ win32_longpath(char *path)
        }
        else {
            /* failed a step, just return without side effects */
-           /*PerlIO_printf(PerlIO_stderr(), "Failed to find %s\n", path);*/
+           /*PerlIO_printf(Perl_debug_log, "Failed to find %s\n", path);*/
            *start = sep;
            return Nullch;
        }
@@ -1128,8 +1155,7 @@ win32_longpath(char *path)
 DllExport char *
 win32_getenv(const char *name)
 {
-    dTHX;
-    dPERLOBJ;
+    dTHXo;
     WCHAR wBuffer[MAX_PATH];
     DWORD needlen;
     SV *curitem = Nullsv;
@@ -1150,7 +1176,7 @@ win32_getenv(const char *name)
                                                  (WCHAR*)SvPVX(curitem),
                                                  needlen);
            } while (needlen >= SvLEN(curitem)/sizeof(WCHAR));
-           SvCUR_set(curitem, needlen*sizeof(WCHAR));
+           SvCUR_set(curitem, (needlen*sizeof(WCHAR))+1);
            acuritem = sv_2mortal(newSVsv(curitem));
            W2AHELPER((WCHAR*)SvPVX(acuritem), SvPVX(curitem), SvCUR(curitem));
        }
@@ -1167,7 +1193,7 @@ win32_getenv(const char *name)
        /* allow any environment variables that begin with 'PERL'
           to be stored in the registry */
        if (strncmp(name, "PERL", 4) == 0)
-           (void)get_regstr(name, curitem);
+           (void)get_regstr(name, &curitem);
     }
     if (curitem && SvCUR(curitem))
        return SvPVX(curitem);
@@ -1178,7 +1204,7 @@ win32_getenv(const char *name)
 DllExport int
 win32_putenv(const char *name)
 {
-    dPERLOBJ;
+    dTHXo;
     char* curitem;
     char* val;
     WCHAR* wCuritem;
@@ -1187,10 +1213,9 @@ win32_putenv(const char *name)
 
     if (name) {
        if (USING_WIDE()) {
-           dTHX;
            length = strlen(name)+1;
            New(1309,wCuritem,length,WCHAR);
-           A2WHELPER(name, wCuritem, length*2);
+           A2WHELPER(name, wCuritem, length*sizeof(WCHAR));
            wVal = wcschr(wCuritem, '=');
            if(wVal) {
                *wVal++ = '\0';
@@ -1264,14 +1289,13 @@ win32_times(struct tms *timebuf)
     return 0;
 }
 
-/* fix utime() so it works on directories in NT
- * thanks to Jan Dubois <jan.dubois@ibm.net>
- */
+/* fix utime() so it works on directories in NT */
 static BOOL
 filetime_from_time(PFILETIME pFileTime, time_t Time)
 {
-    struct tm *pTM = gmtime(&Time);
+    struct tm *pTM = localtime(&Time);
     SYSTEMTIME SystemTime;
+    FILETIME LocalTime;
 
     if (pTM == NULL)
        return FALSE;
@@ -1284,13 +1308,14 @@ filetime_from_time(PFILETIME pFileTime, time_t Time)
     SystemTime.wSecond = pTM->tm_sec;
     SystemTime.wMilliseconds = 0;
 
-    return SystemTimeToFileTime(&SystemTime, pFileTime);
+    return SystemTimeToFileTime(&SystemTime, &LocalTime) &&
+           LocalFileTimeToFileTime(&LocalTime, pFileTime);
 }
 
 DllExport int
 win32_utime(const char *filename, struct utimbuf *times)
 {
-    dPERLOBJ;
+    dTHXo;
     HANDLE handle;
     FILETIME ftCreate;
     FILETIME ftAccess;
@@ -1300,7 +1325,6 @@ win32_utime(const char *filename, struct utimbuf *times)
 
     int rc;
     if (USING_WIDE()) {
-       dTHX;
        A2WHELPER(filename, wbuffer, sizeof(wbuffer));
        rc = _wutime(wbuffer, (struct _utimbuf*)times);
     }
@@ -1436,7 +1460,7 @@ win32_uname(struct utsname *name)
 DllExport int
 win32_waitpid(int pid, int *status, int flags)
 {
-    dPERLOBJ;
+    dTHXo;
     int retval = -1;
     if (pid == -1) 
        return win32_wait(status);
@@ -1474,7 +1498,7 @@ win32_wait(int *status)
     /* XXX this wait emulation only knows about processes
      * spawned via win32_spawnvp(P_NOWAIT, ...).
      */
-    dPERLOBJ;
+    dTHXo;
     int i, retval;
     DWORD exitcode, waitcode;
 
@@ -1511,7 +1535,7 @@ static UINT timerid = 0;
 
 static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
 {
-    dPERLOBJ;
+    dTHXo;
     KillTimer(NULL,timerid);
     timerid=0;  
     sighandler(14);
@@ -1529,7 +1553,7 @@ win32_alarm(unsigned int sec)
      * Snag is unless something is looking at the message queue
      * nothing happens :-(
      */ 
-    dPERLOBJ;
+    dTHXo;
     if (sec)
      {
       timerid = SetTimer(NULL,timerid,sec*1000,(TIMERPROC)TimerProc);
@@ -1555,9 +1579,9 @@ extern char *     des_fcrypt(const char *txt, const char *salt, char *cbuf);
 DllExport char *
 win32_crypt(const char *txt, const char *salt)
 {
+    dTHXo;
 #ifdef HAVE_DES_FCRYPT
     dTHR;
-    dPERLOBJ;
     return des_fcrypt(txt, salt, crypt_buffer);
 #else
     die("The crypt() function is unimplemented due to excessive paranoia.");
@@ -1680,7 +1704,7 @@ win32_flock(int fd, int oper)
     HANDLE fh;
 
     if (!IsWinNT()) {
-       dPERLOBJ;
+       dTHXo;
        Perl_croak_nocontext("flock() unimplemented on this platform");
        return -1;
     }
@@ -1779,7 +1803,7 @@ win32_strerror(int e)
     DWORD source = 0;
 
     if (e < 0 || e > sys_nerr) {
-        dTHX;
+        dTHXo;
        if (e < 0)
            e = GetLastError();
 
@@ -1793,7 +1817,7 @@ win32_strerror(int e)
 }
 
 DllExport void
-win32_str_os_error(pTHX_ void *sv, DWORD dwErr)
+win32_str_os_error(void *sv, DWORD dwErr)
 {
     DWORD dwLen;
     char *sMsg;
@@ -1801,12 +1825,14 @@ win32_str_os_error(pTHX_ void *sv, DWORD dwErr)
                          |FORMAT_MESSAGE_IGNORE_INSERTS
                          |FORMAT_MESSAGE_FROM_SYSTEM, NULL,
                           dwErr, 0, (char *)&sMsg, 1, NULL);
+    /* strip trailing whitespace and period */
     if (0 < dwLen) {
-       while (0 < dwLen  &&  isSPACE(sMsg[--dwLen]))
-           ;
+       do {
+           --dwLen;    /* dwLen doesn't include trailing null */
+       } while (0 < dwLen && isSPACE(sMsg[dwLen]));
        if ('.' != sMsg[dwLen])
            dwLen++;
-       sMsg[dwLen]= '\0';
+       sMsg[dwLen] = '\0';
     }
     if (0 == dwLen) {
        sMsg = (char*)LocalAlloc(0, 64/**sizeof(TCHAR)*/);
@@ -1816,7 +1842,7 @@ win32_str_os_error(pTHX_ void *sv, DWORD dwErr)
                            dwErr, GetLastError());
     }
     if (sMsg) {
-       dPERLOBJ;
+       dTHXo;
        sv_setpvn((SV*)sv, sMsg, dwLen);
        LocalFree(sMsg);
     }
@@ -1870,13 +1896,16 @@ win32_fwrite(const void *buf, size_t size, size_t count, FILE *fp)
 DllExport FILE *
 win32_fopen(const char *filename, const char *mode)
 {
-    dPERLOBJ;
+    dTHXo;
     WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH];
+    
+    if (!*filename)
+       return NULL;
+
     if (stricmp(filename, "/dev/null")==0)
        filename = "NUL";
 
     if (USING_WIDE()) {
-       dTHX;
        A2WHELPER(mode, wMode, sizeof(wMode));
        A2WHELPER(filename, wBuffer, sizeof(wBuffer));
        return _wfopen(wBuffer, wMode);
@@ -1892,10 +1921,9 @@ win32_fopen(const char *filename, const char *mode)
 DllExport FILE *
 win32_fdopen(int handle, const char *mode)
 {
-    dPERLOBJ;
+    dTHXo;
     WCHAR wMode[MODE_SIZE];
     if (USING_WIDE()) {
-       dTHX;
        A2WHELPER(mode, wMode, sizeof(wMode));
        return _wfdopen(handle, wMode);
     }
@@ -1905,13 +1933,12 @@ win32_fdopen(int handle, const char *mode)
 DllExport FILE *
 win32_freopen(const char *path, const char *mode, FILE *stream)
 {
-    dPERLOBJ;
+    dTHXo;
     WCHAR wMode[MODE_SIZE], wBuffer[MAX_PATH];
     if (stricmp(path, "/dev/null")==0)
        path = "NUL";
 
     if (USING_WIDE()) {
-       dTHX;
        A2WHELPER(mode, wMode, sizeof(wMode));
        A2WHELPER(path, wBuffer, sizeof(wBuffer));
        return _wfreopen(wBuffer, wMode, stream);
@@ -2082,9 +2109,8 @@ win32_popen(const char *command, const char *mode)
 
     /* start the child */
     {
-       dTHX;
-       dPERLOBJ;
-       if ((childpid = do_spawn_nowait(aTHX_ (char*)command)) == -1)
+       dTHXo;
+       if ((childpid = do_spawn_nowait((char*)command)) == -1)
            goto cleanup;
 
        /* revert stdfd to whatever it was before */
@@ -2126,8 +2152,7 @@ win32_pclose(FILE *pf)
 #ifdef USE_RTL_POPEN
     return _pclose(pf);
 #else
-    dTHX;
-    dPERLOBJ;
+    dTHXo;
     int childpid, status;
     SV *sv;
 
@@ -2163,9 +2188,8 @@ win32_rename(const char *oname, const char *newname)
      * it doesn't work under Windows95!
      */
     if (IsWinNT()) {
-       dPERLOBJ;
+       dTHXo;
        if (USING_WIDE()) {
-           dTHX;
            A2WHELPER(oname, wOldName, sizeof(wOldName));
            A2WHELPER(newname, wNewName, sizeof(wNewName));
            bResult = MoveFileExW(wOldName,wNewName,
@@ -2289,7 +2313,7 @@ win32_tell(int fd)
 DllExport int
 win32_open(const char *path, int flag, ...)
 {
-    dPERLOBJ;
+    dTHXo;
     va_list ap;
     int pmode;
     WCHAR wBuffer[MAX_PATH];
@@ -2302,7 +2326,6 @@ win32_open(const char *path, int flag, ...)
        path = "NUL";
 
     if (USING_WIDE()) {
-       dTHX;
        A2WHELPER(path, wBuffer, sizeof(wBuffer));
        return _wopen(wBuffer, flag, pmode);
     }
@@ -2366,7 +2389,7 @@ win32_chdir(const char *dir)
 static char *
 create_command_line(const char* command, const char * const *args)
 {
-    dPERLOBJ;
+    dTHXo;
     int index;
     char *cmd, *ptr, *arg;
     STRLEN len = strlen(command) + 1;
@@ -2390,7 +2413,7 @@ create_command_line(const char* command, const char * const *args)
 static char *
 qualified_path(const char *cmd)
 {
-    dPERLOBJ;
+    dTHXo;
     char *pathstr;
     char *fullcmd, *curfullcmd;
     STRLEN cmdlen = 0;
@@ -2475,6 +2498,35 @@ GIVE_UP:
     return Nullch;
 }
 
+/* The following are just place holders.
+ * Some hosts may provide and environment that the OS is
+ * not tracking, therefore, these host must provide that
+ * environment and the current directory to CreateProcess
+ */
+
+void*
+get_childenv(void)
+{
+    return NULL;
+}
+
+void
+free_childenv(void* d)
+{
+}
+
+char*
+get_childdir(void)
+{
+    return NULL;
+}
+
+void
+free_childdir(char* d)
+{
+}
+
+
 /* XXX this needs to be made more compatible with the spawnvp()
  * provided by the various RTLs.  In particular, searching for
  * *.{com,bat,cmd} files (as done by the RTLs) is unimplemented.
@@ -2492,8 +2544,10 @@ win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
 #ifdef USE_RTL_SPAWNVP
     return spawnvp(mode, cmdname, (char * const *)argv);
 #else
-    dPERLOBJ;
-    DWORD ret;
+    dTHXo;
+    int ret;
+    void* env;
+    char* dir;
     STARTUPINFO StartupInfo;
     PROCESS_INFORMATION ProcessInformation;
     DWORD create = 0;
@@ -2502,6 +2556,9 @@ win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
                                             ? &argv[1] : argv);
     char *fullcmd = Nullch;
 
+    env = PerlEnv_get_childenv();
+    dir = PerlEnv_get_childdir();
+
     switch(mode) {
     case P_NOWAIT:     /* asynch + remember result */
        if (w32_num_children >= MAXIMUM_WAIT_OBJECTS) {
@@ -2532,11 +2589,6 @@ win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
        create |= CREATE_NEW_CONSOLE;
     }
 
-#ifndef DEBUGGING
-    StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
-    StartupInfo.wShowWindow = SW_HIDE;
-#endif
-
 RETRY:
     if (!CreateProcess(cmdname,                /* search PATH to find executable */
                       cmd,             /* executable, and its arguments */
@@ -2544,8 +2596,8 @@ RETRY:
                       NULL,            /* thread attributes */
                       TRUE,            /* inherit handles */
                       create,          /* creation flags */
-                      NULL,            /* inherit environment */
-                      NULL,            /* inherit cwd */
+                      (LPVOID)env,     /* inherit environment */
+                      dir,             /* inherit cwd */
                       &StartupInfo,
                       &ProcessInformation))
     {
@@ -2570,20 +2622,26 @@ RETRY:
     if (mode == P_NOWAIT) {
        /* asynchronous spawn -- store handle, return PID */
        w32_child_handles[w32_num_children] = ProcessInformation.hProcess;
-       ret = w32_child_pids[w32_num_children] = ProcessInformation.dwProcessId;
+       w32_child_pids[w32_num_children] = ProcessInformation.dwProcessId;
+       ret = (int)ProcessInformation.dwProcessId;
        ++w32_num_children;
     }
     else  {
+       DWORD status;
        WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
-       GetExitCodeProcess(ProcessInformation.hProcess, &ret);
+       GetExitCodeProcess(ProcessInformation.hProcess, &status);
+       ret = (int)status;
        CloseHandle(ProcessInformation.hProcess);
     }
 
     CloseHandle(ProcessInformation.hThread);
+
 RETVAL:
+    PerlEnv_free_childenv(env);
+    PerlEnv_free_childdir(dir);
     Safefree(cmd);
     Safefree(fullcmd);
-    return (int)ret;
+    return ret;
 #endif
 }
 
@@ -2788,9 +2846,9 @@ win32_get_osfhandle(int fd)
 }
 
 DllExport void*
-win32_dynaload(aTHX_ const char*filename)
+win32_dynaload(const char* filename)
 {
-    dPERLOBJ;
+    dTHXo;
     HMODULE hModule;
     if (USING_WIDE()) {
        WCHAR wfilename[MAX_PATH];
@@ -2810,8 +2868,7 @@ win32_add_host(char *nameId, void *data)
      * This must be called before the script is parsed,
      * therefore no locking of threads is needed
      */
-    dTHX;
-    dPERLOBJ;
+    dTHXo;
     struct host_link *link;
     New(1314, link, 1, struct host_link);
     link->host_data = data;
@@ -2824,8 +2881,7 @@ win32_add_host(char *nameId, void *data)
 DllExport void *
 win32_get_host_data(char *nameId)
 {
-    dTHX;
-    dPERLOBJ;
+    dTHXo;
     struct host_link *link = w32_host_link;
     while(link) {
        if(strEQ(link->nameId, nameId))
@@ -2946,43 +3002,63 @@ static
 XS(w32_DomainName)
 {
     dXSARGS;
-#ifndef HAS_NETWKSTAGETINFO
-    /* mingw32 (and Win95) don't have NetWksta*(), so do it the old way */
-    char name[256];
-    DWORD size = sizeof(name);
+    HINSTANCE hNetApi32 = LoadLibrary("netapi32.dll");
+    DWORD (__stdcall *pfnNetApiBufferFree)(LPVOID Buffer);
+    DWORD (__stdcall *pfnNetWkstaGetInfo)(LPWSTR servername, DWORD level,
+                                         void *bufptr);
+
+    if (hNetApi32) {
+       pfnNetApiBufferFree = (DWORD (__stdcall *)(void *))
+           GetProcAddress(hNetApi32, "NetApiBufferFree");
+       pfnNetWkstaGetInfo = (DWORD (__stdcall *)(LPWSTR, DWORD, void *))
+           GetProcAddress(hNetApi32, "NetWkstaGetInfo");
+    }
     EXTEND(SP,1);
-    if (GetUserName(name,&size)) {
-       char sid[1024];
-       DWORD sidlen = sizeof(sid);
+    if (hNetApi32 && pfnNetWkstaGetInfo && pfnNetApiBufferFree) {
+       /* this way is more reliable, in case user has a local account. */
        char dname[256];
        DWORD dnamelen = sizeof(dname);
-       SID_NAME_USE snu;
-       if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen,
-                             dname, &dnamelen, &snu)) {
-           XSRETURN_PV(dname);         /* all that for this */
+       struct {
+           DWORD   wki100_platform_id;
+           LPWSTR  wki100_computername;
+           LPWSTR  wki100_langroup;
+           DWORD   wki100_ver_major;
+           DWORD   wki100_ver_minor;
+       } *pwi;
+       /* NERR_Success *is* 0*/
+       if (0 == pfnNetWkstaGetInfo(NULL, 100, &pwi)) {
+           if (pwi->wki100_langroup && *(pwi->wki100_langroup)) {
+               WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup,
+                                   -1, (LPSTR)dname, dnamelen, NULL, NULL);
+           }
+           else {
+               WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername,
+                                   -1, (LPSTR)dname, dnamelen, NULL, NULL);
+           }
+           pfnNetApiBufferFree(pwi);
+           FreeLibrary(hNetApi32);
+           XSRETURN_PV(dname);
        }
+       FreeLibrary(hNetApi32);
     }
-#else
-    /* this way is more reliable, in case user has a local account.
-     * XXX need dynamic binding of netapi32.dll symbols or this will fail on
-     * Win95. Probably makes more sense to move it into libwin32. */
-    char dname[256];
-    DWORD dnamelen = sizeof(dname);
-    PWKSTA_INFO_100 pwi;
-    EXTEND(SP,1);
-    if (NERR_Success == NetWkstaGetInfo(NULL, 100, (LPBYTE*)&pwi)) {
-       if (pwi->wki100_langroup && *(pwi->wki100_langroup)) {
-           WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup,
-                               -1, (LPSTR)dname, dnamelen, NULL, NULL);
-       }
-       else {
-           WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername,
-                               -1, (LPSTR)dname, dnamelen, NULL, NULL);
+    else {
+       /* Win95 doesn't have NetWksta*(), so do it the old way */
+       char name[256];
+       DWORD size = sizeof(name);
+       if (hNetApi32)
+           FreeLibrary(hNetApi32);
+       if (GetUserName(name,&size)) {
+           char sid[1024];
+           DWORD sidlen = sizeof(sid);
+           char dname[256];
+           DWORD dnamelen = sizeof(dname);
+           SID_NAME_USE snu;
+           if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen,
+                                 dname, &dnamelen, &snu)) {
+               XSRETURN_PV(dname);             /* all that for this */
+           }
        }
-       NetApiBufferFree(pwi);
-       XSRETURN_PV(dname);
     }
-#endif
     XSRETURN_UNDEF;
 }
 
@@ -3216,9 +3292,9 @@ XS(w32_CopyFile)
 }
 
 void
-Perl_init_os_extras(pTHX)
+Perl_init_os_extras(void)
 {
-    dPERLOBJ;
+    dTHXo;
     char *file = __FILE__;
     dXSUB_SYS;