From: Gurusamy Sarathy Date: Fri, 10 Jul 1998 20:46:12 +0000 (+0000) Subject: add win32_rename() that does what docs say X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e24c7c18173514bd97dba167341e7465f92d5ef7;p=p5sagit%2Fp5-mst-13.2.git add win32_rename() that does what docs say p4raw-id: //depot/perl@1403 --- diff --git a/win32/GenCAPI.pl b/win32/GenCAPI.pl index dbe9fd7..8f597a9 100644 --- a/win32/GenCAPI.pl +++ b/win32/GenCAPI.pl @@ -870,6 +870,11 @@ int _win32_stat(const char *name,struct stat *sbufptr) return pPerl->piLIO->NameStat(name, sbufptr, ErrorNo()); } +int _win32_rename(const char *oldname, const char *newname) +{ + return pPerl->piLIO->Rename(oldname, newname, ErrorNo()); +} + int _win32_setmode(int fd, int mode) { return pPerl->piLIO->Setmode(fd, mode, ErrorNo()); @@ -1220,6 +1225,7 @@ U32 * _Perl_opargs (); #undef win32_pipe #undef win32_popen #undef win32_pclose +#undef win32_rename #undef win32_setmode #undef win32_lseek #undef win32_tell @@ -1335,6 +1341,7 @@ U32 * _Perl_opargs (); #define win32_pipe _win32_pipe #define win32_popen _win32_popen #define win32_pclose _win32_pclose +#define win32_rename _win32_rename #define win32_setmode _win32_setmode #define win32_lseek _win32_lseek #define win32_tell _win32_tell @@ -1453,6 +1460,7 @@ int _win32_stat(const char *name,struct stat *sbufptr); int _win32_pipe( int *phandles, unsigned int psize, int textmode ); FILE* _win32_popen( const char *command, const char *mode ); int _win32_pclose( FILE *pf); +int _win32_rename( const char *oldname, const char *newname); int _win32_setmode( int fd, int mode); long _win32_lseek( int fd, long offset, int origin); long _win32_tell( int fd); diff --git a/win32/makedef.pl b/win32/makedef.pl index 7d29c9c..02a9484 100644 --- a/win32/makedef.pl +++ b/win32/makedef.pl @@ -455,6 +455,7 @@ win32_stat win32_pipe win32_popen win32_pclose +win32_rename win32_setmode win32_lseek win32_tell diff --git a/win32/perlhost.h b/win32/perlhost.h index 10abef9..b8a59e4 100644 --- a/win32/perlhost.h +++ b/win32/perlhost.h @@ -402,54 +402,7 @@ public: }; virtual int Rename(const char *OldFileName, const char *newname, int &err) { - char szNewWorkName[MAX_PATH+1]; - WIN32_FIND_DATA fdOldFile, fdNewFile; - HANDLE handle; - char *ptr; - - if((strchr(OldFileName, '\\') || strchr(OldFileName, '/')) - && strchr(newname, '\\') == NULL - && strchr(newname, '/') == NULL) - { - strcpy(szNewWorkName, OldFileName); - if((ptr = strrchr(szNewWorkName, '\\')) == NULL) - ptr = strrchr(szNewWorkName, '/'); - strcpy(++ptr, newname); - } - else - strcpy(szNewWorkName, newname); - - if(stricmp(OldFileName, 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(OldFileName, &fdOldFile); - if(handle != INVALID_HANDLE_VALUE) - { - FindClose(handle); - - handle = FindFirstFile(szNewWorkName, &fdNewFile); - - if(handle != INVALID_HANDLE_VALUE) - FindClose(handle); - 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); - } - } - } - int ret = rename(OldFileName, szNewWorkName); - if(ret) - err = errno; - - return ret; + CALLFUNCRET(win32_rename(OldFileName, newname)) }; virtual int Setmode(int handle, int mode, int &err) { diff --git a/win32/win32.c b/win32/win32.c index ef59a8f..24f42f7 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -181,7 +181,7 @@ static char * get_emd_part(char *prev_path, char *trailing_path, ...) { va_list ap; - char mod_name[MAX_PATH]; + char mod_name[MAX_PATH+1]; char *ptr; char *optr; char *strip; @@ -252,7 +252,7 @@ win32_get_sitelib(char *pl) { char *sitelib = "sitelib"; char regstr[40]; - char pathstr[MAX_PATH]; + char pathstr[MAX_PATH+1]; DWORD datalen; char *path1 = Nullch; char *path2 = Nullch; @@ -885,7 +885,7 @@ win32_sleep(unsigned int t) DllExport int win32_stat(const char *path, struct stat *buffer) { - char t[MAX_PATH]; + char t[MAX_PATH+1]; const char *p = path; int l = strlen(path); int res; @@ -1732,6 +1732,56 @@ win32_pclose(FILE *pf) } DllExport int +win32_rename(const char *oldname, const char *newname) +{ + char szNewWorkName[MAX_PATH+1]; + WIN32_FIND_DATA fdOldFile, fdNewFile; + HANDLE handle; + char *ptr; + + if ((strchr(oldname, '\\') || strchr(oldname, '/')) + && strchr(newname, '\\') == NULL + && strchr(newname, '/') == NULL) + { + strcpy(szNewWorkName, oldname); + if ((ptr = strrchr(szNewWorkName, '\\')) == NULL) + ptr = strrchr(szNewWorkName, '/'); + strcpy(++ptr, newname); + } + else + strcpy(szNewWorkName, newname); + + if (stricmp(oldname, 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(oldname, &fdOldFile); + if (handle != INVALID_HANDLE_VALUE) { + FindClose(handle); + + handle = FindFirstFile(szNewWorkName, &fdNewFile); + + if (handle != INVALID_HANDLE_VALUE) + FindClose(handle); + 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); + } + } + } + return rename(oldname, newname); +} + +DllExport int win32_setmode(int fd, int mode) { return setmode(fd, mode); diff --git a/win32/win32iop.h b/win32/win32iop.h index b22a187..a83ebd5 100644 --- a/win32/win32iop.h +++ b/win32/win32iop.h @@ -74,6 +74,7 @@ DllExport int win32_stat(const char *name,struct stat *sbufptr); DllExport int win32_pipe( int *phandles, unsigned int psize, int textmode ); DllExport FILE* win32_popen( const char *command, const char *mode ); DllExport int win32_pclose( FILE *pf); +DllExport int win32_rename( const char *oldname, const char *newname); DllExport int win32_setmode( int fd, int mode); DllExport long win32_lseek( int fd, long offset, int origin); DllExport long win32_tell( int fd); @@ -203,6 +204,7 @@ END_EXTERN_C #define abort() win32_abort() #define fstat(fd,bufptr) win32_fstat(fd,bufptr) #define stat(pth,bufptr) win32_stat(pth,bufptr) +#define rename(old,new) win32_rename(old,new) #define setmode(fd,mode) win32_setmode(fd,mode) #define lseek(fd,offset,orig) win32_lseek(fd,offset,orig) #define tell(fd) win32_tell(fd)