Integrate from mainperl.
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
index 3f1c215..2c74fc2 100644 (file)
@@ -38,6 +38,8 @@
 #include "EXTERN.h"
 #include "perl.h"
 
+#include "patchlevel.h"
+
 #define NO_XSLOCKS
 #ifdef PERL_OBJECT
 extern CPerlObj* pPerl;
@@ -91,7 +93,7 @@ static DWORD          os_id(void);
 static void            get_shell(void);
 static long            tokenize(char *str, char **dest, char ***destv);
        int             do_spawn2(char *cmd, int exectype);
-static BOOL            has_redirection(char *ptr);
+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(char *leading, char *trailing, ...);
@@ -145,7 +147,7 @@ GetRegStrFromKey(HKEY hkey, const char *lpszValueName, char** ptr, DWORD* lpData
     if (retval == ERROR_SUCCESS){
        retval = RegQueryValueEx(handle, lpszValueName, 0, &type, NULL, lpDataLen);
        if (retval == ERROR_SUCCESS && type == REG_SZ) {
-           if (*ptr != NULL) {
+           if (*ptr) {
                Renew(*ptr, *lpDataLen, char);
            }
            else {
@@ -154,7 +156,7 @@ GetRegStrFromKey(HKEY hkey, const char *lpszValueName, char** ptr, DWORD* lpData
            retval = RegQueryValueEx(handle, lpszValueName, 0, NULL, (PBYTE)*ptr, lpDataLen);
            if (retval != ERROR_SUCCESS) {
                Safefree(*ptr);
-               *ptr = NULL;
+               *ptr = Nullch;
            }
        }
        RegCloseKey(handle);
@@ -166,7 +168,7 @@ char*
 GetRegStr(const char *lpszValueName, char** ptr, DWORD* lpDataLen)
 {
     *ptr = GetRegStrFromKey(HKEY_CURRENT_USER, lpszValueName, ptr, lpDataLen);
-    if (*ptr == NULL)
+    if (*ptr == Nullch)
     {
        *ptr = GetRegStrFromKey(HKEY_LOCAL_MACHINE, lpszValueName, ptr, lpDataLen);
     }
@@ -176,6 +178,7 @@ GetRegStr(const char *lpszValueName, char** ptr, DWORD* lpDataLen)
 static char *
 get_emd_part(char *prev_path, char *trailing_path, ...)
 {
+    char base[10];
     va_list ap;
     char mod_name[MAX_PATH+1];
     char *ptr;
@@ -186,9 +189,11 @@ get_emd_part(char *prev_path, char *trailing_path, ...)
     va_start(ap, trailing_path);
     strip = va_arg(ap, char *);
 
-    GetModuleFileName((w32_perldll_handle == INVALID_HANDLE_VALUE)
-                     ? GetModuleHandle(NULL)
-                     : w32_perldll_handle, mod_name, sizeof(mod_name));
+    sprintf(base, "%5.3f", (double) 5 + ((double) PATCHLEVEL / (double) 1000));
+
+    GetModuleFileName((HMODULE)((w32_perldll_handle == INVALID_HANDLE_VALUE)
+                               ? GetModuleHandle(NULL) : w32_perldll_handle),
+                     mod_name, sizeof(mod_name));
     ptr = strrchr(mod_name, '\\');
     while (ptr && strip) {
         /* look for directories to skip back */
@@ -196,8 +201,11 @@ get_emd_part(char *prev_path, char *trailing_path, ...)
        *ptr = '\0';
        ptr = strrchr(mod_name, '\\');
        if (!ptr || stricmp(ptr+1, strip) != 0) {
-           *optr = '\\';
-           ptr = optr;
+           if(!(*strip == '5' && *(ptr+1) == '5' && strncmp(strip, base, 5) == 0
+                   && strncmp(ptr+1, base, 5) == 0)) {
+               *optr = '\\';
+               ptr = optr;
+           }
        }
        strip = va_arg(ap, char *);
     }
@@ -209,17 +217,21 @@ get_emd_part(char *prev_path, char *trailing_path, ...)
     va_end(ap);
     strcpy(++ptr, trailing_path);
 
-    newsize = strlen(mod_name) + 1;
-    if (prev_path) {
-       oldsize = strlen(prev_path) + 1;
-       newsize += oldsize;                     /* includes plus 1 for ';' */
-       Renew(prev_path, newsize, char);
-       prev_path[oldsize-1] = ';';
-       strcpy(&prev_path[oldsize], mod_name);
-    }
-    else {
-       New(1311, prev_path, newsize, char);
-       strcpy(prev_path, mod_name);
+    /* only add directory if it exists */
+    if(GetFileAttributes(mod_name) != (DWORD) -1) {
+       /* directory exists */
+       newsize = strlen(mod_name) + 1;
+       if (prev_path) {
+           oldsize = strlen(prev_path) + 1;
+           newsize += oldsize;                 /* includes plus 1 for ';' */
+           Renew(prev_path, newsize, char);
+           prev_path[oldsize-1] = ';';
+           strcpy(&prev_path[oldsize], mod_name);
+       }
+       else {
+           New(1311, prev_path, newsize, char);
+           strcpy(prev_path, mod_name);
+       }
     }
 
     return prev_path;
@@ -236,7 +248,7 @@ win32_get_privlib(char *pl)
     /* $stdlib = $HKCU{"lib-$]"} || $HKLM{"lib-$]"} || $HKCU{"lib"} || $HKLM{"lib"} || "";  */
     sprintf(buffer, "%s-%s", stdlib, pl);
     path = GetRegStr(buffer, &path, &datalen);
-    if (path == NULL)
+    if (!path)
        path = GetRegStr(stdlib, &path, &datalen);
 
     /* $stdlib .= ";$EMD/../../lib" */
@@ -289,17 +301,20 @@ win32_get_sitelib(char *pl)
 
 
 static BOOL
-has_redirection(char *ptr)
+has_shell_metachars(char *ptr)
 {
     int inquote = 0;
     char quote = '\0';
 
     /*
      * Scan string looking for redirection (< or >) or pipe
-     * characters (|) that are not in a quoted string
+     * characters (|) that are not in a quoted string.
+     * Shell variable interpolation (%VAR%) can also happen inside strings.
      */
     while (*ptr) {
        switch(*ptr) {
+       case '%':
+           return TRUE;
        case '\'':
        case '\"':
            if (inquote) {
@@ -457,6 +472,7 @@ do_aspawn(void *vreally, void **vmark, void **vsp)
     int status;
     int flag = P_WAIT;
     int index = 0;
+    STRLEN n_a;
 
     if (sp <= mark)
        return -1;
@@ -470,7 +486,7 @@ do_aspawn(void *vreally, void **vmark, void **vsp)
     }
 
     while (++mark <= sp) {
-       if (*mark && (str = SvPV(*mark, PL_na)))
+       if (*mark && (str = SvPV(*mark, n_a)))
            argv[index++] = str;
        else
            argv[index++] = "";
@@ -478,10 +494,10 @@ do_aspawn(void *vreally, void **vmark, void **vsp)
     argv[index++] = 0;
    
     status = win32_spawnvp(flag,
-                          (const char*)(really ? SvPV(really,PL_na) : argv[0]),
+                          (const char*)(really ? SvPV(really,n_a) : argv[0]),
                           (const char* const*)argv);
 
-    if (status < 0 && errno == ENOEXEC) {
+    if (status < 0 && (errno == ENOEXEC || errno == ENOENT)) {
        /* possible shell-builtin, invoke with shell */
        int sh_items;
        sh_items = w32_perlshell_items;
@@ -491,7 +507,7 @@ do_aspawn(void *vreally, void **vmark, void **vsp)
            argv[sh_items] = w32_perlshell_vec[sh_items];
    
        status = win32_spawnvp(flag,
-                              (const char*)(really ? SvPV(really,PL_na) : argv[0]),
+                              (const char*)(really ? SvPV(really,n_a) : argv[0]),
                               (const char* const*)argv);
     }
 
@@ -521,7 +537,7 @@ do_spawn2(char *cmd, int exectype)
 
     /* Save an extra exec if possible. See if there are shell
      * metacharacters in it */
-    if (!has_redirection(cmd)) {
+    if (!has_shell_metachars(cmd)) {
        New(1301,argv, strlen(cmd) / 2 + 2, char*);
        New(1302,cmd2, strlen(cmd) + 1, char);
        strcpy(cmd2, cmd);
@@ -636,12 +652,8 @@ win32_opendir(char *filename)
        return NULL;
 
     /* check to see if filename is a directory */
-    if (win32_stat(filename, &sbuf) < 0 || (sbuf.st_mode & S_IFDIR) == 0) {
-       /* CRT is buggy on sharenames, so make sure it really isn't */
-       DWORD r = GetFileAttributes(filename);
-       if (r == 0xffffffff || !(r & FILE_ATTRIBUTE_DIRECTORY))
-           return NULL;
-    }
+    if (win32_stat(filename, &sbuf) < 0 || !S_ISDIR(sbuf.st_mode))
+       return NULL;
 
     /* Get us a DIR structure */
     Newz(1303, p, 1, DIR);
@@ -658,6 +670,10 @@ win32_opendir(char *filename)
     /* do the FindFirstFile call */
     fh = FindFirstFile(scanname, &FindData);
     if (fh == INVALID_HANDLE_VALUE) {
+       /* FindFirstFile() fails on empty drives! */
+       if (GetLastError() == ERROR_FILE_NOT_FOUND)
+           return p;
+       Safefree( p);
        return NULL;
     }
 
@@ -881,7 +897,7 @@ win32_sleep(unsigned int t)
 DllExport int
 win32_stat(const char *path, struct stat *buffer)
 {
-    char               t[MAX_PATH+1]; 
+    char       t[MAX_PATH+1]; 
     const char *p = path;
     int                l = strlen(path);
     int                res;
@@ -898,8 +914,31 @@ win32_stat(const char *path, struct stat *buffer)
        }
     }
     res = stat(p,buffer);
+    if (res < 0) {
+       /* CRT is buggy on sharenames, so make sure it really isn't.
+        * XXX using GetFileAttributesEx() will enable us to set
+        * buffer->st_*time (but note that's not available on the
+        * Windows of 1995) */
+       DWORD r = GetFileAttributes(p);
+       if (r != 0xffffffff && (r & FILE_ATTRIBUTE_DIRECTORY)) {
+           buffer->st_mode |= S_IFDIR | S_IREAD;
+           errno = 0;
+           if (!(r & FILE_ATTRIBUTE_READONLY))
+               buffer->st_mode |= S_IWRITE | S_IEXEC;
+           return 0;
+       }
+    }
+    else {
+       if (l == 3 && path[l-2] == ':'
+           && (path[l-1] == '\\' || path[l-1] == '/'))
+       {
+           /* The drive can be inaccessible, some _stat()s are buggy */
+           if (!GetVolumeInformation(path,NULL,0,NULL,NULL,NULL,NULL,0)) {
+               errno = ENOENT;
+               return -1;
+           }
+       }
 #ifdef __BORLANDC__
-    if (res == 0) {
        if (S_ISDIR(buffer->st_mode))
            buffer->st_mode |= S_IWRITE | S_IEXEC;
        else if (S_ISREG(buffer->st_mode)) {
@@ -916,8 +955,8 @@ win32_stat(const char *path, struct stat *buffer)
            else
                buffer->st_mode &= ~S_IEXEC;
        }
-    }
 #endif
+    }
     return res;
 }
 
@@ -926,11 +965,13 @@ win32_stat(const char *path, struct stat *buffer)
 DllExport char *
 win32_getenv(const char *name)
 {
-    static char *curitem = Nullch;
-    static DWORD curlen = 512;
+    static char *curitem = Nullch;     /* XXX threadead */
+    static DWORD curlen = 0;           /* XXX threadead */
     DWORD needlen;
-    if (!curitem)
+    if (!curitem) {
+       curlen = 512;
        New(1305,curitem,curlen,char);
+    }
 
     needlen = GetEnvironmentVariable(name,curitem,curlen);
     if (needlen != 0) {
@@ -940,23 +981,22 @@ win32_getenv(const char *name)
            needlen = GetEnvironmentVariable(name,curitem,curlen);
        }
     }
-    else
-    {
+    else {
        /* allow any environment variables that begin with 'PERL'
-          to be stored in the registry
-       */
-       if(curitem != NULL)
+          to be stored in the registry */
+       if (curitem)
            *curitem = '\0';
 
        if (strncmp(name, "PERL", 4) == 0) {
-           if (curitem != NULL) {
+           if (curitem) {
                Safefree(curitem);
-               curitem = NULL;
+               curitem = Nullch;
+               curlen = 0;
            }
            curitem = GetRegStr(name, &curitem, &curlen);
        }
     }
-    if(curitem != NULL && *curitem == '\0')
+    if (curitem && *curitem == '\0')
        return Nullch;
 
     return curitem;
@@ -1068,10 +1108,10 @@ win32_waitpid(int pid, int *status, int flags)
       return win32_wait(status);
     else {
       rc = cwait(status, pid, WAIT_CHILD);
-    /* cwait() returns differently on Borland */
-#ifdef __BORLANDC__
+    /* cwait() returns "correctly" on Borland */
+#ifndef __BORLANDC__
     if (status)
-       *status =  (((*status >> 8) & 0xff) | ((*status << 8) & 0xff00));
+       *status *= 256;
 #endif
       remove_dead_process((HANDLE)pid);
     }
@@ -1724,12 +1764,11 @@ win32_pclose(FILE *pf)
     /* wait for the child */
     if (cwait(&status, childpid, WAIT_CHILD) == -1)
         return (-1);
-    /* cwait() returns differently on Borland */
-#ifdef __BORLANDC__
-    return (((status >> 8) & 0xff) | ((status << 8) & 0xff00));
-#else
-    return (status);
+    /* cwait() returns "correctly" on Borland */
+#ifndef __BORLANDC__
+    status *= 256;
 #endif
+    return (status);
 
 #endif /* USE_RTL_POPEN */
 }
@@ -1737,51 +1776,102 @@ win32_pclose(FILE *pf)
 DllExport int
 win32_rename(const char *oname, const char *newname)
 {
-    char szNewWorkName[MAX_PATH+1];
-    WIN32_FIND_DATA fdOldFile, fdNewFile;
-    HANDLE handle;
-    char *ptr;
-
-    if ((strchr(oname, '\\') || strchr(oname, '/'))
-       && strchr(newname, '\\') == NULL
-       && strchr(newname, '/') == NULL)
-    {
-       strcpy(szNewWorkName, oname);
-       if ((ptr = strrchr(szNewWorkName, '\\')) == NULL)
-           ptr = strrchr(szNewWorkName, '/');
-       strcpy(++ptr, newname);
+    /* XXX despite what the documentation says about MoveFileEx(),
+     * it doesn't work under Windows95!
+     */
+    if (IsWinNT()) {
+       if (!MoveFileEx(oname,newname,
+                       MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING)) {
+           DWORD err = GetLastError();
+           switch (err) {
+           case ERROR_BAD_NET_NAME:
+           case ERROR_BAD_NETPATH:
+           case ERROR_BAD_PATHNAME:
+           case ERROR_FILE_NOT_FOUND:
+           case ERROR_FILENAME_EXCED_RANGE:
+           case ERROR_INVALID_DRIVE:
+           case ERROR_NO_MORE_FILES:
+           case ERROR_PATH_NOT_FOUND:
+               errno = ENOENT;
+               break;
+           default:
+               errno = EACCES;
+               break;
+           }
+           return -1;
+       }
+       return 0;
     }
-    else
-       strcpy(szNewWorkName, newname);
-
-    if (stricmp(oname, szNewWorkName) != 0) {
-       // check that we're not being fooled by relative paths
-       // and only delete the new file
-       //  1) if it exists
-       //  2) it is not the same file as the old file
-       //  3) old file exist
-       // GetFullPathName does not return the long file name on some systems
-       handle = FindFirstFile(oname, &fdOldFile);
-       if (handle != INVALID_HANDLE_VALUE) {
-           FindClose(handle);
-    
-           handle = FindFirstFile(szNewWorkName, &fdNewFile);
-    
-           if (handle != INVALID_HANDLE_VALUE)
-               FindClose(handle);
+    else {
+       int retval = 0;
+       char tmpname[MAX_PATH+1];
+       char dname[MAX_PATH+1];
+       char *endname = Nullch;
+       STRLEN tmplen = 0;
+       DWORD from_attr, to_attr;
+
+       /* if oname doesn't exist, do nothing */
+       from_attr = GetFileAttributes(oname);
+       if (from_attr == 0xFFFFFFFF) {
+           errno = ENOENT;
+           return -1;
+       }
+
+       /* if newname exists, rename it to a temporary name so that we
+        * don't delete it in case oname happens to be the same file
+        * (but perhaps accessed via a different path)
+        */
+       to_attr = GetFileAttributes(newname);
+       if (to_attr != 0xFFFFFFFF) {
+           /* if newname is a directory, we fail
+            * XXX could overcome this with yet more convoluted logic */
+           if (to_attr & FILE_ATTRIBUTE_DIRECTORY) {
+               errno = EACCES;
+               return -1;
+           }
+           tmplen = strlen(newname);
+           strcpy(tmpname,newname);
+           endname = tmpname+tmplen;
+           for (; endname > tmpname ; --endname) {
+               if (*endname == '/' || *endname == '\\') {
+                   *endname = '\0';
+                   break;
+               }
+           }
+           if (endname > tmpname)
+               endname = strcpy(dname,tmpname);
            else
-               fdNewFile.cFileName[0] = '\0';
-
-           if (strcmp(fdOldFile.cAlternateFileName,
-                      fdNewFile.cAlternateFileName) != 0
-               && strcmp(fdOldFile.cFileName, fdNewFile.cFileName) != 0)
-           {
-               // file exists and not same file
-               DeleteFile(szNewWorkName);
+               endname = ".";
+
+           /* get a temporary filename in same directory
+            * XXX is this really the best we can do? */
+           if (!GetTempFileName((LPCTSTR)endname, "plr", 0, tmpname)) {
+               errno = ENOENT;
+               return -1;
            }
+           DeleteFile(tmpname);
+
+           retval = rename(newname, tmpname);
+           if (retval != 0) {
+               errno = EACCES;
+               return retval;
+           }
+       }
+
+       /* rename oname to newname */
+       retval = rename(oname, newname);
+
+       /* if we created a temporary file before ... */
+       if (endname != Nullch) {
+           /* ...and rename succeeded, delete temporary file/directory */
+           if (retval == 0)
+               DeleteFile(tmpname);
+           /* else restore it to what it was */
+           else
+               (void)rename(tmpname, newname);
        }
+       return retval;
     }
-    return rename(oname, newname);
 }
 
 DllExport int
@@ -2123,9 +2213,10 @@ static
 XS(w32_SetCwd)
 {
     dXSARGS;
+    STRLEN n_a;
     if (items != 1)
        croak("usage: Win32::SetCurrentDirectory($cwd)");
-    if (SetCurrentDirectory(SvPV(ST(0),PL_na)))
+    if (SetCurrentDirectory(SvPV(ST(0),n_a)))
        XSRETURN_YES;
 
     XSRETURN_NO;
@@ -2304,12 +2395,13 @@ XS(w32_Spawn)
     PROCESS_INFORMATION stProcInfo;
     STARTUPINFO stStartInfo;
     BOOL bSuccess = FALSE;
+    STRLEN n_a;
 
     if (items != 3)
        croak("usage: Win32::Spawn($cmdName, $args, $PID)");
 
-    cmd = SvPV(ST(0),PL_na);
-    args = SvPV(ST(1), PL_na);
+    cmd = SvPV(ST(0),n_a);
+    args = SvPV(ST(1), n_a);
 
     memset(&stStartInfo, 0, sizeof(stStartInfo));   /* Clear the block */
     stStartInfo.cb = sizeof(stStartInfo);          /* Set the structure size */