@@ -954,8 +954,30 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
954
954
955
955
char * mingw_getcwd (char * pointer , int len )
956
956
{
957
- wchar_t wpointer [MAX_PATH ];
958
- if (!_wgetcwd (wpointer , ARRAY_SIZE (wpointer )))
957
+ wchar_t cwd [MAX_PATH ], wpointer [MAX_PATH ];
958
+ DECLARE_PROC_ADDR (kernel32 .dll , DWORD , GetFinalPathNameByHandleW ,
959
+ HANDLE , LPWSTR , DWORD , DWORD );
960
+ DWORD ret = GetCurrentDirectoryW (ARRAY_SIZE (cwd ), cwd );
961
+
962
+ if (ret < 0 || ret >= ARRAY_SIZE (cwd ))
963
+ return NULL ;
964
+ ret = GetLongPathNameW (cwd , wpointer , ARRAY_SIZE (wpointer ));
965
+ if (!ret && GetLastError () == ERROR_ACCESS_DENIED &&
966
+ INIT_PROC_ADDR (GetFinalPathNameByHandleW )) {
967
+ HANDLE hnd = CreateFileW (cwd , 0 ,
968
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
969
+ OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
970
+ if (hnd == INVALID_HANDLE_VALUE )
971
+ return NULL ;
972
+ ret = GetFinalPathNameByHandleW (hnd , wpointer , ARRAY_SIZE (wpointer ), 0 );
973
+ CloseHandle (hnd );
974
+ if (!ret || ret >= ARRAY_SIZE (wpointer ))
975
+ return NULL ;
976
+ if (xwcstoutf (pointer , normalize_ntpath (wpointer ), len ) < 0 )
977
+ return NULL ;
978
+ return pointer ;
979
+ }
980
+ if (!ret || ret >= ARRAY_SIZE (wpointer ))
959
981
return NULL ;
960
982
if (xwcstoutf (pointer , wpointer , len ) < 0 )
961
983
return NULL ;
0 commit comments