|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
|
|
|
|
|
|
| static bool __cdecl is_stream_allocated(long const stream_flags) throw()
|
| {
|
| return (stream_flags & _IOALLOCATED) != 0;
|
| }
|
|
|
| static bool __cdecl is_stream_flushable(long const stream_flags) throw()
|
| {
|
| if ((stream_flags & (_IOREAD | _IOWRITE)) != _IOWRITE)
|
| {
|
| return false;
|
| }
|
|
|
| if ((stream_flags & (_IOBUFFER_CRT | _IOBUFFER_USER)) == 0)
|
| {
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
|
|
| static bool __cdecl is_stream_flushable_or_commitable(long const stream_flags) throw()
|
| {
|
| if (is_stream_flushable(stream_flags))
|
| {
|
| return true;
|
| }
|
|
|
| if (stream_flags & _IOCOMMIT)
|
| {
|
| return true;
|
| }
|
|
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static bool __cdecl common_flush_all_should_try_to_flush_stream(
|
| _In_ __crt_stdio_stream const stream,
|
| _Inout_ int* const flushed_stream_count
|
| ) throw()
|
| {
|
| if (!stream.valid())
|
| {
|
| return false;
|
| }
|
|
|
| long const stream_flags = stream.get_flags();
|
| if (!is_stream_allocated(stream_flags))
|
| {
|
| return false;
|
| }
|
|
|
| if (!is_stream_flushable_or_commitable(stream_flags))
|
| {
|
| ++*flushed_stream_count;
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static int __cdecl common_flush_all(bool const flush_read_mode_streams) throw()
|
| {
|
| int count = 0;
|
| int error = 0;
|
|
|
| __acrt_lock_and_call(__acrt_stdio_index_lock, [&]
|
| {
|
| __crt_stdio_stream_data** const first_file = __piob;
|
| __crt_stdio_stream_data** const last_file = first_file + _nstream;
|
|
|
| for (__crt_stdio_stream_data** it = first_file; it != last_file; ++it)
|
| {
|
| __crt_stdio_stream const stream(*it);
|
|
|
|
|
|
|
| if (!common_flush_all_should_try_to_flush_stream(stream, &count))
|
| {
|
| continue;
|
| }
|
|
|
| __acrt_lock_stream_and_call(stream.public_stream(), [&]
|
| {
|
|
|
|
|
|
|
|
|
| if (!common_flush_all_should_try_to_flush_stream(stream, &count))
|
| {
|
| return;
|
| }
|
|
|
| if (!flush_read_mode_streams && !stream.has_all_of(_IOWRITE))
|
| {
|
| return;
|
| }
|
|
|
| if (_fflush_nolock(stream.public_stream()) != EOF)
|
| {
|
| ++count;
|
| }
|
| else
|
| {
|
| error = EOF;
|
| }
|
| });
|
| }
|
| });
|
|
|
| return flush_read_mode_streams ? count : error;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl fflush(FILE* const public_stream)
|
| {
|
| __crt_stdio_stream const stream(public_stream);
|
|
|
|
|
| if (!stream.valid())
|
| {
|
| return common_flush_all(false);
|
| }
|
|
|
|
|
|
|
|
|
|
|
| if (!is_stream_flushable_or_commitable(stream.get_flags()))
|
| {
|
| return 0;
|
| }
|
|
|
| return __acrt_lock_stream_and_call(stream.public_stream(), [&]
|
| {
|
| return _fflush_nolock(stream.public_stream());
|
| });
|
| }
|
|
|
|
|
|
|
| static int __cdecl _fflush_nolock_internal(FILE* const public_stream, __crt_cached_ptd_host& ptd)
|
| {
|
| __crt_stdio_stream const stream(public_stream);
|
|
|
|
|
| if (!stream.valid())
|
| {
|
| return common_flush_all(false);
|
| }
|
|
|
| if (__acrt_stdio_flush_nolock(stream.public_stream(), ptd) != 0)
|
| {
|
|
|
| return EOF;
|
| }
|
|
|
|
|
| if (stream.has_all_of(_IOCOMMIT))
|
| {
|
| if (_commit(_fileno(public_stream)))
|
| {
|
| return EOF;
|
| }
|
| }
|
|
|
| return 0;
|
| }
|
|
|
| extern "C" int __cdecl _fflush_nolock(FILE* const public_stream)
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _fflush_nolock_internal(public_stream, ptd);
|
| }
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl __acrt_stdio_flush_nolock(FILE* const public_stream, __crt_cached_ptd_host& ptd)
|
| {
|
| __crt_stdio_stream const stream(public_stream);
|
|
|
| if (!is_stream_flushable(stream.get_flags()))
|
| {
|
| return 0;
|
| }
|
|
|
| int const bytes_to_write = static_cast<int>(stream->_ptr - stream->_base);
|
|
|
| __acrt_stdio_reset_buffer(stream);
|
|
|
| if (bytes_to_write <= 0)
|
| {
|
| return 0;
|
| }
|
|
|
| int const bytes_written = _write_internal(_fileno(stream.public_stream()), stream->_base, bytes_to_write, ptd);
|
| if (bytes_to_write != bytes_written)
|
| {
|
| stream.set_flags(_IOERROR);
|
| return EOF;
|
| }
|
|
|
|
|
|
|
| if (stream.has_all_of(_IOUPDATE))
|
| {
|
| stream.unset_flags(_IOWRITE);
|
| }
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl _flushall()
|
| {
|
| return common_flush_all(true);
|
| }
|
|
|