@@ -967,8 +967,30 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
967
967
968
968
char * mingw_getcwd (char * pointer , int len )
969
969
{
970
- wchar_t wpointer [MAX_PATH ];
971
- if (!_wgetcwd (wpointer , ARRAY_SIZE (wpointer )))
970
+ wchar_t cwd [MAX_PATH ], wpointer [MAX_PATH ];
971
+ DECLARE_PROC_ADDR (kernel32 .dll , DWORD , GetFinalPathNameByHandleW ,
972
+ HANDLE , LPWSTR , DWORD , DWORD );
973
+ DWORD ret = GetCurrentDirectoryW (ARRAY_SIZE (cwd ), cwd );
974
+
975
+ if (ret < 0 || ret >= ARRAY_SIZE (cwd ))
976
+ return NULL ;
977
+ ret = GetLongPathNameW (cwd , wpointer , ARRAY_SIZE (wpointer ));
978
+ if (!ret && GetLastError () == ERROR_ACCESS_DENIED &&
979
+ INIT_PROC_ADDR (GetFinalPathNameByHandleW )) {
980
+ HANDLE hnd = CreateFileW (cwd , 0 ,
981
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
982
+ OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
983
+ if (hnd == INVALID_HANDLE_VALUE )
984
+ return NULL ;
985
+ ret = GetFinalPathNameByHandleW (hnd , wpointer , ARRAY_SIZE (wpointer ), 0 );
986
+ CloseHandle (hnd );
987
+ if (!ret || ret >= ARRAY_SIZE (wpointer ))
988
+ return NULL ;
989
+ if (xwcstoutf (pointer , normalize_ntpath (wpointer ), len ) < 0 )
990
+ return NULL ;
991
+ return pointer ;
992
+ }
993
+ if (!ret || ret >= ARRAY_SIZE (wpointer ))
972
994
return NULL ;
973
995
if (xwcstoutf (pointer , wpointer , len ) < 0 )
974
996
return NULL ;
0 commit comments