Skip to content

Commit 4d517b7

Browse files
committed
Merge 'mingw-getcwd' into HEAD
2 parents 938d4b2 + 0d47cd5 commit 4d517b7

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

compat/mingw.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,32 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
11011101

11021102
char *mingw_getcwd(char *pointer, int len)
11031103
{
1104-
wchar_t wpointer[MAX_PATH];
1105-
if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
1104+
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
1105+
DECLARE_PROC_ADDR(kernel32.dll, DWORD, GetFinalPathNameByHandleW,
1106+
HANDLE, LPWSTR, DWORD, DWORD);
1107+
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1108+
1109+
if (!ret || ret >= ARRAY_SIZE(cwd)) {
1110+
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
1111+
return NULL;
1112+
}
1113+
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1114+
if (!ret && GetLastError() == ERROR_ACCESS_DENIED &&
1115+
INIT_PROC_ADDR(GetFinalPathNameByHandleW)) {
1116+
HANDLE hnd = CreateFileW(cwd, 0,
1117+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1118+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1119+
if (hnd == INVALID_HANDLE_VALUE)
1120+
return NULL;
1121+
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
1122+
CloseHandle(hnd);
1123+
if (!ret || ret >= ARRAY_SIZE(wpointer))
1124+
return NULL;
1125+
if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
1126+
return NULL;
1127+
return pointer;
1128+
}
1129+
if (!ret || ret >= ARRAY_SIZE(wpointer))
11061130
return NULL;
11071131
if (xwcstoutf(pointer, wpointer, len) < 0)
11081132
return NULL;

0 commit comments

Comments
 (0)