|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <conio.h>
|
| #include <corecrt_internal_lowio.h>
|
| #include <corecrt_internal_securecrt.h>
|
| #include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" wchar_t __console_wchar_buffer = 0;
|
| extern "C" int __console_wchar_buffer_used = 0;
|
|
|
|
|
|
|
|
|
|
|
| extern "C" errno_t __cdecl _cgetws_s(wchar_t* const string_buffer, size_t const size_in_words, size_t* const size_read)
|
| {
|
| _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(string_buffer != nullptr, EINVAL);
|
| _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(size_in_words > 0, EINVAL);
|
| _RESET_STRING(string_buffer, size_in_words);
|
|
|
| _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(size_read != nullptr, EINVAL);
|
|
|
| __acrt_lock(__acrt_conio_lock);
|
| errno_t retval = 0;
|
| __try
|
| {
|
| wchar_t* string = string_buffer;
|
|
|
|
|
|
|
| size_t size_remaining = size_in_words - 1;
|
| *size_read = 0;
|
|
|
|
|
| if (size_remaining == 0)
|
| __leave;
|
|
|
|
|
|
|
| if (__console_wchar_buffer_used != 0)
|
| {
|
| *string++ = __console_wchar_buffer;
|
| --size_remaining;
|
| (*size_read)++;
|
|
|
| if (__console_wchar_buffer == L'\0')
|
| size_remaining = 0;
|
|
|
| __console_wchar_buffer = 0;
|
| }
|
|
|
| if (size_remaining == 0)
|
| __leave;
|
|
|
| |
| |
| |
|
|
|
|
| if (__dcrt_lowio_ensure_console_input_initialized() == FALSE)
|
| {
|
| __acrt_errno_map_os_error(GetLastError());
|
| retval = errno;
|
| __leave;
|
| }
|
|
|
| ULONG old_state;
|
| __dcrt_get_input_console_mode(&old_state);
|
| __dcrt_set_input_console_mode(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT);
|
|
|
| ULONG wchars_read;
|
| BOOL read_console_result = __dcrt_read_console(
|
| string,
|
| static_cast<DWORD>(size_remaining),
|
| &wchars_read);
|
|
|
| if (!read_console_result)
|
| {
|
| __acrt_errno_map_os_error(GetLastError());
|
| retval = errno;
|
| __leave;
|
| }
|
|
|
|
|
| if (wchars_read >= 2 && string[wchars_read - 2] == L'\r')
|
| {
|
| *size_read += wchars_read - 2;
|
| string[wchars_read - 2] = L'\0';
|
| }
|
| else if (wchars_read == size_remaining && string[wchars_read - 1] == L'\r')
|
| {
|
|
|
| string[wchars_read - 1] = L'\0';
|
| *size_read += wchars_read - 1;
|
| }
|
| else if (wchars_read == 1 && string[0] == L'\n')
|
| {
|
|
|
| string[0] = L'\0';
|
| *size_read += 0;
|
| }
|
| else
|
| {
|
| *size_read += wchars_read;
|
| string[wchars_read] = L'\0';
|
| }
|
|
|
| __dcrt_set_input_console_mode(old_state);
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_conio_lock);
|
| }
|
|
|
| return retval;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" wchar_t* __cdecl _cgetws(_Inout_z_ wchar_t* const string)
|
| {
|
| _VALIDATE_CLEAR_OSSERR_RETURN(string != nullptr, EINVAL, nullptr);
|
| _VALIDATE_CLEAR_OSSERR_RETURN(string[0] > 0, EINVAL, nullptr);
|
|
|
| size_t const size_in_words = static_cast<size_t>(string[0]);
|
|
|
| size_t size_read = 0;
|
|
|
|
|
| #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED)
|
| errno_t const result = _cgetws_s(string + 2, size_in_words, &size_read);
|
|
|
|
|
|
|
| #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED)
|
| string[1] = static_cast<wchar_t>(size_read);
|
|
|
| return result == 0 ? string + 2 : nullptr;
|
| }
|
|
|