| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_traits.h>
|
| #include <direct.h>
|
| #include <errno.h>
|
| #include <malloc.h>
|
| #include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
| static int __cdecl is_valid_drive(unsigned const drive_number) throw()
|
| {
|
| if (drive_number > 26)
|
| {
|
| _doserrno = ERROR_INVALID_DRIVE;
|
| _VALIDATE_RETURN(("Invalid Drive Index" ,0), EACCES, 0);
|
| }
|
|
|
| if (drive_number == 0)
|
| return 1;
|
|
|
| #pragma warning(suppress:__WARNING_UNUSED_ASSIGNMENT)
|
| wchar_t const drive_letter = static_cast<wchar_t>(L'A' + drive_number - 1);
|
| wchar_t const drive_string[] = { drive_letter, L':', L'\\', L'\0' };
|
|
|
| UINT const drive_type = GetDriveTypeW(drive_string);
|
| if (drive_type == DRIVE_UNKNOWN || drive_type == DRIVE_NO_ROOT_DIR)
|
| return 0;
|
|
|
| return 1;
|
| }
|
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| template <typename Character>
|
| _Success_(return != 0)
|
| _Ret_z_
|
| static Character* __cdecl common_getdcwd(
|
| int drive_number,
|
| _Out_writes_opt_z_(max_count) Character* user_buffer,
|
| int const max_count,
|
| int const block_use,
|
| _In_opt_z_ char const* const file_name,
|
| int const line_number
|
| ) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
|
|
| _VALIDATE_RETURN(max_count >= 0, EINVAL, nullptr);
|
|
|
| if (drive_number != 0)
|
| {
|
|
|
|
|
| if (!is_valid_drive(drive_number))
|
| {
|
| _doserrno = ERROR_INVALID_DRIVE;
|
| _VALIDATE_RETURN(("Invalid Drive", 0), EACCES, nullptr);
|
| }
|
| }
|
| else
|
| {
|
|
|
| drive_number = _getdrive();
|
| }
|
|
|
| Character drive_string[4];
|
| if (drive_number != 0)
|
| {
|
| drive_string[0] = static_cast<Character>('A' - 1 + drive_number);
|
| drive_string[1] = ':';
|
| drive_string[2] = '.';
|
| drive_string[3] = '\0';
|
| }
|
| else
|
| {
|
| drive_string[0] = '.';
|
| drive_string[1] = '\0';
|
| }
|
|
|
| if (user_buffer == nullptr)
|
| {
|
| __crt_public_win32_buffer<Character> buffer(
|
| __crt_win32_buffer_debug_info(block_use, file_name, line_number)
|
| );
|
| buffer.allocate(max_count);
|
| if (!traits::get_full_path_name(drive_string, buffer))
|
| {
|
| return buffer.detach();
|
| }
|
| return nullptr;
|
| }
|
|
|
|
|
| _VALIDATE_RETURN(max_count > 0, EINVAL, nullptr);
|
| user_buffer[0] = '\0';
|
|
|
| __crt_no_alloc_win32_buffer<Character> buffer(user_buffer, max_count);
|
| if (!traits::get_full_path_name(drive_string, buffer))
|
| {
|
| return user_buffer;
|
| }
|
| return nullptr;
|
| };
|
|
|
|
|
|
|
| extern "C" char* __cdecl _getcwd(
|
| char* const user_buffer,
|
| int const max_length
|
| )
|
| {
|
| return common_getdcwd(0, user_buffer, max_length, _NORMAL_BLOCK, nullptr, 0);
|
| }
|
|
|
| extern "C" wchar_t* __cdecl _wgetcwd(
|
| wchar_t* const user_buffer,
|
| int const max_length
|
| )
|
| {
|
| return common_getdcwd(0, user_buffer, max_length, _NORMAL_BLOCK, nullptr, 0);
|
| }
|
|
|
| extern "C" char* __cdecl _getdcwd(
|
| int const drive_number,
|
| char* const user_buffer,
|
| int const max_length
|
| )
|
| {
|
| return common_getdcwd(drive_number, user_buffer, max_length, _NORMAL_BLOCK, nullptr, 0);
|
| }
|
|
|
| extern "C" wchar_t* __cdecl _wgetdcwd(
|
| int const drive_number,
|
| wchar_t* const user_buffer,
|
| int const max_length
|
| )
|
| {
|
| return common_getdcwd(drive_number, user_buffer, max_length, _NORMAL_BLOCK, nullptr, 0);
|
| }
|
|
|
| #ifdef _DEBUG
|
|
|
| #undef _getcwd_dbg
|
| #undef _getdcwd_dbg
|
|
|
| extern "C" char* __cdecl _getcwd_dbg(
|
| char* const user_buffer,
|
| int const max_length,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| )
|
| {
|
| return common_getdcwd(0, user_buffer, max_length, block_use, file_name, line_number);
|
| }
|
|
|
| extern "C" wchar_t* __cdecl _wgetcwd_dbg(
|
| wchar_t* const user_buffer,
|
| int const max_length,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| )
|
| {
|
| return common_getdcwd(0, user_buffer, max_length, block_use, file_name, line_number);
|
| }
|
|
|
| extern "C" char* __cdecl _getdcwd_dbg(
|
| int const drive_number,
|
| char* const user_buffer,
|
| int const max_length,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| )
|
| {
|
| return common_getdcwd(drive_number, user_buffer, max_length, block_use, file_name, line_number);
|
| }
|
|
|
| extern "C" wchar_t* __cdecl _wgetdcwd_dbg(
|
| int const drive_number,
|
| wchar_t* const user_buffer,
|
| int const max_length,
|
| int const block_use,
|
| char const* const file_name,
|
| int const line_number
|
| )
|
| {
|
| return common_getdcwd(drive_number, user_buffer, max_length, block_use, file_name, line_number);
|
| }
|
|
|
| #endif
|
|
|