|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl ungetc(int const c, FILE* const stream)
|
| {
|
| _VALIDATE_RETURN(stream != nullptr, EINVAL, EOF);
|
|
|
| int return_value = EOF;
|
|
|
| _lock_file(stream);
|
| __try
|
| {
|
| return_value = _ungetc_nolock(c, stream);
|
| }
|
| __finally
|
| {
|
| _unlock_file(stream);
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _ungetc_nolock(int const c, FILE* public_stream)
|
| {
|
| __crt_stdio_stream const stream(public_stream);
|
|
|
| _VALIDATE_STREAM_ANSI_RETURN(stream, EINVAL, EOF);
|
|
|
|
|
| if (c == EOF)
|
| return EOF;
|
|
|
|
|
|
|
| bool const is_in_read_only_mode = stream.has_all_of(_IOREAD);
|
| bool const is_in_rw_write_mode = stream.has_all_of(_IOUPDATE | _IOWRITE);
|
|
|
| if (!is_in_read_only_mode && !is_in_rw_write_mode)
|
| return EOF;
|
|
|
|
|
| if (stream->_base == nullptr)
|
| __acrt_stdio_allocate_buffer_nolock(stream.public_stream());
|
|
|
|
|
|
|
| if (stream->_ptr == stream->_base)
|
| {
|
|
|
|
|
| if (stream->_cnt)
|
| return EOF;
|
|
|
| ++stream->_ptr;
|
| }
|
|
|
|
|
| if (stream.is_string_backed())
|
| {
|
| --stream->_ptr;
|
| if (*stream->_ptr != static_cast<char>(c))
|
| {
|
| ++stream->_ptr;
|
| return EOF;
|
| }
|
| }
|
| else
|
| {
|
| --stream->_ptr;
|
| *stream->_ptr = static_cast<char>(c);
|
| }
|
|
|
| ++stream->_cnt;
|
| stream.unset_flags(_IOEOF);
|
| stream.set_flags(_IOREAD);
|
|
|
| return c & 0xff;
|
| }
|
|
|