@@ -1101,8 +1101,32 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
1101
1101
1102
1102
char * mingw_getcwd (char * pointer , int len )
1103
1103
{
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 ))
1106
1130
return NULL ;
1107
1131
if (xwcstoutf (pointer , wpointer , len ) < 0 )
1108
1132
return NULL ;
0 commit comments