From 51ce3ff756a234868d7cb222da03ce29f7b2c66f Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Fri, 4 Apr 2003 07:55:59 +0000 Subject: [PATCH] Win32 fixes. Big thanks to Bjorn Karlsson. * jam_src/pwc.c: Concert short pathnames to long ones. [SVN r18181] --- historic/jam/src/pwd.c | 98 ++++++++++++++++++++++++++++++++++++++++-- jam_src/pwd.c | 98 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 190 insertions(+), 6 deletions(-) diff --git a/historic/jam/src/pwd.c b/historic/jam/src/pwd.c index 935edeb8f..92d70bd4d 100644 --- a/historic/jam/src/pwd.c +++ b/historic/jam/src/pwd.c @@ -11,18 +11,110 @@ #include +#ifdef NT +#include +#include + +#ifndef INVALID_FILE_ATTRIBUTES +#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) +#endif + + +DWORD ShortPathToLongPath(LPCTSTR lpszShortPath,LPTSTR lpszLongPath,DWORD + cchBuffer) +{ + DWORD i=0; + TCHAR path[_MAX_PATH]={0}; + TCHAR ret[_MAX_PATH]={0}; + DWORD pos=0; + DWORD len=_tcslen(lpszShortPath); + + // Is the string valid? + if (!lpszShortPath) { + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } + + // Is the path valid? + if (GetFileAttributes(lpszShortPath)==INVALID_FILE_ATTRIBUTES) + return 0; + + // Convert "/" to "\" + for (i=0;i3) + _tcscpy(lpszLongPath,lpszShortPath); + return len; + } + _tcsncpy(ret,path,2); + } + + // Expand the path for each subpath, and strip trailing backslashes + for (;pos<=len;++pos) { + if (path[pos]==_T('\\') || (path[pos]==_T('\0') && + path[pos-1]!=_T('\\'))) { + WIN32_FIND_DATA fd; + HANDLE hf=0; + TCHAR c=path[pos]; + path[pos]=_T('\0'); + + hf=FindFirstFile(path, &fd); + if (hf==INVALID_HANDLE_VALUE) + return 0; + + // Append the result + _tcscat(ret,_T("\\")); + _tcscat(ret,fd.cFileName); + path[pos]=c; + FindClose(hf); + } + } + + len=_tcslen(ret)+1; + if (cchBuffer>=len) + _tcscpy(lpszLongPath,ret); + + return len; +} +#endif + LIST* pwd(void) { char buffer[PATH_MAX]; if (getcwd(buffer, sizeof(buffer)) == NULL) { - perror("can not get current directory"); - return L0; + perror("can not get current directory"); + return L0; } else { - return list_new(L0, newstr(buffer)); +#ifdef NT + char buffer2[_MAX_PATH]; + ShortPathToLongPath(buffer, buffer2, _MAX_PATH); + return list_new(L0, newstr(buffer2)); +#else + return list_new(L0, newstr(buffer)); +#endif } } diff --git a/jam_src/pwd.c b/jam_src/pwd.c index 935edeb8f..92d70bd4d 100644 --- a/jam_src/pwd.c +++ b/jam_src/pwd.c @@ -11,18 +11,110 @@ #include +#ifdef NT +#include +#include + +#ifndef INVALID_FILE_ATTRIBUTES +#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) +#endif + + +DWORD ShortPathToLongPath(LPCTSTR lpszShortPath,LPTSTR lpszLongPath,DWORD + cchBuffer) +{ + DWORD i=0; + TCHAR path[_MAX_PATH]={0}; + TCHAR ret[_MAX_PATH]={0}; + DWORD pos=0; + DWORD len=_tcslen(lpszShortPath); + + // Is the string valid? + if (!lpszShortPath) { + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } + + // Is the path valid? + if (GetFileAttributes(lpszShortPath)==INVALID_FILE_ATTRIBUTES) + return 0; + + // Convert "/" to "\" + for (i=0;i3) + _tcscpy(lpszLongPath,lpszShortPath); + return len; + } + _tcsncpy(ret,path,2); + } + + // Expand the path for each subpath, and strip trailing backslashes + for (;pos<=len;++pos) { + if (path[pos]==_T('\\') || (path[pos]==_T('\0') && + path[pos-1]!=_T('\\'))) { + WIN32_FIND_DATA fd; + HANDLE hf=0; + TCHAR c=path[pos]; + path[pos]=_T('\0'); + + hf=FindFirstFile(path, &fd); + if (hf==INVALID_HANDLE_VALUE) + return 0; + + // Append the result + _tcscat(ret,_T("\\")); + _tcscat(ret,fd.cFileName); + path[pos]=c; + FindClose(hf); + } + } + + len=_tcslen(ret)+1; + if (cchBuffer>=len) + _tcscpy(lpszLongPath,ret); + + return len; +} +#endif + LIST* pwd(void) { char buffer[PATH_MAX]; if (getcwd(buffer, sizeof(buffer)) == NULL) { - perror("can not get current directory"); - return L0; + perror("can not get current directory"); + return L0; } else { - return list_new(L0, newstr(buffer)); +#ifdef NT + char buffer2[_MAX_PATH]; + ShortPathToLongPath(buffer, buffer2, _MAX_PATH); + return list_new(L0, newstr(buffer2)); +#else + return list_new(L0, newstr(buffer)); +#endif } }