Skip to content

Commit acfc4bb

Browse files
committed
Merge 'mingw-getcwd' into HEAD
2 parents c8318aa + 91de7f5 commit acfc4bb

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
@@ -967,8 +967,30 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
967967

968968
char *mingw_getcwd(char *pointer, int len)
969969
{
970-
wchar_t wpointer[MAX_PATH];
971-
if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
970+
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
971+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
972+
HANDLE, LPWSTR, DWORD, DWORD);
973+
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
974+
975+
if (ret < 0 || ret >= ARRAY_SIZE(cwd))
976+
return NULL;
977+
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
978+
if (!ret && GetLastError() == ERROR_ACCESS_DENIED &&
979+
INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
980+
HANDLE hnd = CreateFileW(cwd, 0,
981+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
982+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
983+
if (hnd == INVALID_HANDLE_VALUE)
984+
return NULL;
985+
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
986+
CloseHandle(hnd);
987+
if (!ret || ret >= ARRAY_SIZE(wpointer))
988+
return NULL;
989+
if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
990+
return NULL;
991+
return pointer;
992+
}
993+
if (!ret || ret >= ARRAY_SIZE(wpointer))
972994
return NULL;
973995
if (xwcstoutf(pointer, wpointer, len) < 0)
974996
return NULL;

0 commit comments

Comments
 (0)