Skip to content

Commit a9c3d2e

Browse files
committed
Merge 'mingw-getcwd' into HEAD
2 parents 096549c + e0a236f commit a9c3d2e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

compat/mingw.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,30 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
954954

955955
char *mingw_getcwd(char *pointer, int len)
956956
{
957-
wchar_t wpointer[MAX_PATH];
958-
if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
957+
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
958+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
959+
HANDLE, LPWSTR, DWORD, DWORD);
960+
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
961+
962+
if (ret < 0 || ret >= ARRAY_SIZE(cwd))
963+
return NULL;
964+
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
965+
if (!ret && GetLastError() == ERROR_ACCESS_DENIED &&
966+
INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
967+
HANDLE hnd = CreateFileW(cwd, 0,
968+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
969+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
970+
if (hnd == INVALID_HANDLE_VALUE)
971+
return NULL;
972+
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
973+
CloseHandle(hnd);
974+
if (!ret || ret >= ARRAY_SIZE(wpointer))
975+
return NULL;
976+
if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
977+
return NULL;
978+
return pointer;
979+
}
980+
if (!ret || ret >= ARRAY_SIZE(wpointer))
959981
return NULL;
960982
if (xwcstoutf(pointer, wpointer, len) < 0)
961983
return NULL;

0 commit comments

Comments
 (0)