Skip to content

Commit 36be153

Browse files
committed
Merge pull request #2449 from dscho/mingw-getcwd-and-symlinks
Do resolve symlinks in `getcwd()`
2 parents 414e599 + 65bfabd commit 36be153

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

compat/mingw.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,18 +1152,16 @@ char *mingw_getcwd(char *pointer, int len)
11521152
{
11531153
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
11541154
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1155+
HANDLE hnd;
11551156

11561157
if (!ret || ret >= ARRAY_SIZE(cwd)) {
11571158
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
11581159
return NULL;
11591160
}
1160-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1161-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1162-
HANDLE hnd = CreateFileW(cwd, 0,
1163-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1164-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1165-
if (hnd == INVALID_HANDLE_VALUE)
1166-
return NULL;
1161+
hnd = CreateFileW(cwd, 0,
1162+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1163+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1164+
if (hnd != INVALID_HANDLE_VALUE) {
11671165
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
11681166
CloseHandle(hnd);
11691167
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1172,13 +1170,11 @@ char *mingw_getcwd(char *pointer, int len)
11721170
return NULL;
11731171
return pointer;
11741172
}
1175-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1176-
return NULL;
1177-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1173+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
11781174
errno = ENOENT;
11791175
return NULL;
11801176
}
1181-
if (xwcstoutf(pointer, wpointer, len) < 0)
1177+
if (xwcstoutf(pointer, cwd, len) < 0)
11821178
return NULL;
11831179
convert_slashes(pointer);
11841180
return pointer;

0 commit comments

Comments
 (0)