|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
|
|
|
|
|
|
|
|
| static int __cdecl set_buffer(
|
| __crt_stdio_stream const stream,
|
| _In_reads_opt_(buffer_size_in_bytes) char* const buffer,
|
| size_t const buffer_size_in_bytes,
|
| int const new_flag_bits
|
| ) throw()
|
| {
|
| stream.set_flags(new_flag_bits);
|
| stream->_bufsiz = static_cast<int>(buffer_size_in_bytes);
|
| stream->_ptr = buffer;
|
| stream->_base = buffer;
|
| stream->_cnt = 0;
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static int __cdecl _setvbuf_internal(
|
| FILE* const public_stream,
|
| char* const buffer,
|
| int const type,
|
| size_t const buffer_size_in_bytes,
|
| __crt_cached_ptd_host& ptd
|
| )
|
| {
|
| __crt_stdio_stream const stream(public_stream);
|
|
|
| _UCRT_VALIDATE_RETURN(ptd, stream.valid(), EINVAL, -1);
|
|
|
|
|
|
|
| _UCRT_VALIDATE_RETURN(ptd, type == _IONBF || type == _IOFBF || type == _IOLBF, EINVAL, -1);
|
|
|
| if (type == _IOFBF || type == _IOLBF)
|
| {
|
| _UCRT_VALIDATE_RETURN(ptd, 2 <= buffer_size_in_bytes && buffer_size_in_bytes <= INT_MAX, EINVAL, -1);
|
| }
|
|
|
| return __acrt_lock_stream_and_call(stream.public_stream(), [&]
|
| {
|
|
|
| size_t const usable_buffer_size = buffer_size_in_bytes & ~static_cast<size_t>(1);
|
|
|
|
|
| __acrt_stdio_flush_nolock(stream.public_stream(), ptd);
|
| __acrt_stdio_free_buffer_nolock(stream.public_stream());
|
|
|
|
|
|
|
|
|
| stream.unset_flags(_IOBUFFER_CRT | _IOBUFFER_USER | _IOBUFFER_NONE |
|
| _IOBUFFER_SETVBUF | _IOBUFFER_STBUF | _IOCTRLZ);
|
|
|
|
|
| if (type & _IONBF)
|
| {
|
| return set_buffer(stream, reinterpret_cast<char*>(&stream->_charbuf), 2, _IOBUFFER_NONE);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (buffer == nullptr)
|
| {
|
| char* const crt_buffer = _calloc_crt_t(char, usable_buffer_size).detach();
|
| if (!crt_buffer)
|
| {
|
| #ifndef CRTDLL
|
|
|
|
|
| ++_cflush;
|
| #endif
|
|
|
| return -1;
|
| }
|
|
|
| return set_buffer(stream, crt_buffer, usable_buffer_size, _IOBUFFER_CRT | _IOBUFFER_SETVBUF);
|
| }
|
|
|
|
|
| return set_buffer(stream, buffer, usable_buffer_size, _IOBUFFER_USER | _IOBUFFER_SETVBUF);
|
| });
|
| }
|
|
|
| extern "C" int __cdecl setvbuf(
|
| FILE* const public_stream,
|
| char* const buffer,
|
| int const type,
|
| size_t const buffer_size_in_bytes
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return _setvbuf_internal(public_stream, buffer, type, buffer_size_in_bytes, ptd);
|
| }
|
|
|