Skip to content

Commit 6188a0d

Browse files
committed
Merge pull request #2449 from dscho/mingw-getcwd-and-symlinks
Do resolve symlinks in `getcwd()`
2 parents fa8bd7c + 227e59e commit 6188a0d

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
@@ -1130,18 +1130,16 @@ char *mingw_getcwd(char *pointer, int len)
11301130
{
11311131
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
11321132
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1133+
HANDLE hnd;
11331134

11341135
if (!ret || ret >= ARRAY_SIZE(cwd)) {
11351136
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
11361137
return NULL;
11371138
}
1138-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1139-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1140-
HANDLE hnd = CreateFileW(cwd, 0,
1141-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1142-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1143-
if (hnd == INVALID_HANDLE_VALUE)
1144-
return NULL;
1139+
hnd = CreateFileW(cwd, 0,
1140+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1141+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1142+
if (hnd != INVALID_HANDLE_VALUE) {
11451143
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
11461144
CloseHandle(hnd);
11471145
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1150,13 +1148,11 @@ char *mingw_getcwd(char *pointer, int len)
11501148
return NULL;
11511149
return pointer;
11521150
}
1153-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1154-
return NULL;
1155-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1151+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
11561152
errno = ENOENT;
11571153
return NULL;
11581154
}
1159-
if (xwcstoutf(pointer, wpointer, len) < 0)
1155+
if (xwcstoutf(pointer, cwd, len) < 0)
11601156
return NULL;
11611157
convert_slashes(pointer);
11621158
return pointer;

0 commit comments

Comments
 (0)