| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #include <direct.h>
|
| #include <corecrt_internal_traits.h>
|
| #include <corecrt_internal_win32_buffer.h>
|
| #include <malloc.h>
|
| #include <stdlib.h>
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| template <typename Character>
|
| _Success_(return == 0)
|
| static int __cdecl set_cwd_environment_variable(_In_z_ Character const* const path) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
|
|
|
|
| if ((path[0] == '\\' || path[0] == '/') && path[0] == path[1])
|
| return 0;
|
|
|
| #pragma warning(suppress:28931)
|
| Character const drive_letter = static_cast<Character>(toupper(static_cast<char>(path[0])));
|
| Character const name[] = { '=', drive_letter, ':', '\0' };
|
|
|
| if (traits::set_environment_variable(name, path))
|
| return 0;
|
|
|
| __acrt_errno_map_os_error(GetLastError());
|
| return -1;
|
| }
|
|
|
|
|
|
|
| template <typename Character>
|
| _Success_(return == 0)
|
| static int __cdecl common_chdir(_In_z_ Character const* const path) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
|
|
| _VALIDATE_CLEAR_OSSERR_RETURN(path != nullptr, EINVAL, -1);
|
|
|
| if (!traits::set_current_directory(path))
|
| {
|
| __acrt_errno_map_os_error(GetLastError());
|
| return -1;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Character buffer_initial_storage[MAX_PATH + 1];
|
| __crt_internal_win32_buffer<Character> current_directory_buffer(buffer_initial_storage);
|
|
|
| errno_t const err = traits::get_current_directory(current_directory_buffer);
|
|
|
| if (err != 0) {
|
|
|
| return -1;
|
| }
|
|
|
| return set_cwd_environment_variable(current_directory_buffer.data());
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _chdir(char const* const path)
|
| {
|
| return common_chdir(path);
|
| }
|
|
|
| extern "C" int __cdecl _wchdir(wchar_t const* const path)
|
| {
|
| return common_chdir(path);
|
| }
|
|
|