Skip to content

Commit 531ffae

Browse files
committed
Merge pull request #2449 from dscho/mingw-getcwd-and-symlinks
Do resolve symlinks in `getcwd()`
2 parents 4e6dca1 + 0b77572 commit 531ffae

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
@@ -1150,18 +1150,16 @@ char *mingw_getcwd(char *pointer, int len)
11501150
{
11511151
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
11521152
DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
1153+
HANDLE hnd;
11531154

11541155
if (!ret || ret >= ARRAY_SIZE(cwd)) {
11551156
errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
11561157
return NULL;
11571158
}
1158-
ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
1159-
if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
1160-
HANDLE hnd = CreateFileW(cwd, 0,
1161-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1162-
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1163-
if (hnd == INVALID_HANDLE_VALUE)
1164-
return NULL;
1159+
hnd = CreateFileW(cwd, 0,
1160+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1161+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1162+
if (hnd != INVALID_HANDLE_VALUE) {
11651163
ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
11661164
CloseHandle(hnd);
11671165
if (!ret || ret >= ARRAY_SIZE(wpointer))
@@ -1170,13 +1168,11 @@ char *mingw_getcwd(char *pointer, int len)
11701168
return NULL;
11711169
return pointer;
11721170
}
1173-
if (!ret || ret >= ARRAY_SIZE(wpointer))
1174-
return NULL;
1175-
if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
1171+
if (GetFileAttributesW(cwd) == INVALID_FILE_ATTRIBUTES) {
11761172
errno = ENOENT;
11771173
return NULL;
11781174
}
1179-
if (xwcstoutf(pointer, wpointer, len) < 0)
1175+
if (xwcstoutf(pointer, cwd, len) < 0)
11801176
return NULL;
11811177
convert_slashes(pointer);
11821178
return pointer;

0 commit comments

Comments
 (0)