|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" size_t __cdecl _fwrite_internal(
|
| void const* const buffer,
|
| size_t const size,
|
| size_t const count,
|
| FILE* const stream,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| if (size == 0 || count == 0)
|
| {
|
| return 0;
|
| }
|
|
|
|
|
| _UCRT_VALIDATE_RETURN(ptd, stream != nullptr, EINVAL, 0);
|
|
|
| return __acrt_lock_stream_and_call(stream, [&]() -> size_t
|
| {
|
| __acrt_stdio_temporary_buffering_guard const buffering(stream, ptd);
|
|
|
| return _fwrite_nolock_internal(buffer, size, count, stream, ptd);
|
| });
|
| }
|
|
|
| extern "C" size_t __cdecl fwrite(
|
| void const* const buffer,
|
| size_t const size,
|
| size_t const count,
|
| FILE* const stream
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _fwrite_internal(buffer, size, count, stream, ptd);
|
| }
|
|
|
| extern "C" size_t __cdecl _fwrite_nolock_internal(
|
| void const* const buffer,
|
| size_t const element_size,
|
| size_t const element_count,
|
| FILE* const public_stream,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| if (element_size == 0 || element_count == 0)
|
| {
|
| return 0;
|
| }
|
|
|
| __crt_stdio_stream const stream(public_stream);
|
|
|
| _UCRT_VALIDATE_RETURN(ptd, stream.valid(), EINVAL, 0);
|
| _UCRT_VALIDATE_RETURN(ptd, buffer != nullptr, EINVAL, 0);
|
| _UCRT_VALIDATE_RETURN(ptd, element_count <= (SIZE_MAX / element_size), EINVAL, 0);
|
|
|
|
|
|
|
| unsigned stream_buffer_size = stream.has_any_buffer()
|
| ? stream->_bufsiz
|
| : _INTERNAL_BUFSIZ;
|
|
|
|
|
| size_t const total_bytes = element_size * element_count;
|
|
|
| char const* data = static_cast<char const*>(buffer);
|
|
|
|
|
| size_t remaining_bytes = total_bytes;
|
| while (remaining_bytes != 0)
|
| {
|
|
|
| if (stream.has_big_buffer() && stream->_cnt != 0)
|
| {
|
| if (stream->_cnt < 0)
|
| {
|
| _ASSERTE(("Inconsistent Stream Count. Flush between consecutive read and write", stream->_cnt >= 0));
|
| stream.set_flags(_IOERROR);
|
| return (total_bytes - remaining_bytes) / element_size;
|
| }
|
|
|
| if (stream.has_any_of(_IOREAD))
|
| {
|
| _ASSERTE(("Flush between consecutive read and write.", !stream.has_any_of(_IOREAD)));
|
| return (total_bytes - remaining_bytes) / element_size;
|
| }
|
|
|
| size_t const bytes_to_write = __min(remaining_bytes, static_cast<size_t>(stream->_cnt));
|
|
|
| memcpy(stream->_ptr, data, bytes_to_write);
|
|
|
| remaining_bytes -= bytes_to_write;
|
| stream->_cnt -= static_cast<int>(bytes_to_write);
|
| stream->_ptr += bytes_to_write;
|
| data += bytes_to_write;
|
| }
|
|
|
|
|
| else if (remaining_bytes >= stream_buffer_size)
|
| {
|
|
|
|
|
|
|
| if (stream.has_big_buffer() && __acrt_stdio_flush_nolock(stream.public_stream(), ptd))
|
| {
|
| return (total_bytes - remaining_bytes) / element_size;
|
| }
|
|
|
|
|
|
|
|
|
| size_t const max_bytes_to_write = stream_buffer_size > 0
|
| ? remaining_bytes - remaining_bytes % stream_buffer_size
|
| : remaining_bytes;
|
|
|
| unsigned const bytes_to_write = static_cast<unsigned>(__min(max_bytes_to_write, UINT_MAX - 1));
|
|
|
| unsigned const bytes_actually_written = _write_internal(_fileno(stream.public_stream()), data, bytes_to_write, ptd);
|
| if (bytes_actually_written == UINT_MAX)
|
| {
|
| stream.set_flags(_IOERROR);
|
| return (total_bytes - remaining_bytes) / element_size;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| unsigned const bytes_written = bytes_actually_written > bytes_to_write
|
| ? bytes_to_write
|
| : bytes_actually_written;
|
|
|
|
|
| remaining_bytes -= bytes_written;
|
| data += bytes_written;
|
|
|
| if (bytes_actually_written < bytes_to_write)
|
| {
|
| stream.set_flags(_IOERROR);
|
| return (total_bytes - remaining_bytes) / element_size;
|
| }
|
| }
|
|
|
|
|
|
|
| else
|
| {
|
|
|
|
|
| if (__acrt_stdio_flush_and_write_narrow_nolock(*data, stream.public_stream(), ptd) == EOF)
|
| {
|
| return (total_bytes - remaining_bytes) / element_size;
|
| }
|
|
|
|
|
| ++data;
|
| --remaining_bytes;
|
|
|
| stream_buffer_size = stream->_bufsiz > 0
|
| ? stream->_bufsiz
|
| : 1;
|
| }
|
| }
|
|
|
| return element_count;
|
| }
|
|
|
| extern "C" size_t __cdecl _fwrite_nolock(
|
| void const* const buffer,
|
| size_t const element_size,
|
| size_t const element_count,
|
| FILE* const public_stream
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _fwrite_nolock_internal(buffer, element_size, element_count, public_stream, ptd);
|
| }
|
|
|
|
|