2 * COPYRIGHT: GNU GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS crt library
4 * FILE: lib/sdk/crt/stdio/_flsbuf.c
5 * PURPOSE: Implementation of _flsbuf / _flswbuf
6 * PROGRAMMER: Timo Kreuzer
13 void __cdecl
alloc_buffer(FILE *stream
);
16 _flsbuf(int ch
, FILE *stream
)
20 /* Check if the stream supports flushing */
21 if ((stream
->_flag
& _IOSTRG
) || !(stream
->_flag
& (_IORW
|_IOWRT
)))
23 stream
->_flag
|= _IOERR
;
27 /* Is this was a read buffer */
28 if (stream
->_flag
& _IOREAD
)
30 /* Must be at the end of the file */
31 if (!(stream
->_flag
& _IOEOF
))
33 stream
->_flag
|= _IOERR
;
39 stream
->_ptr
= stream
->_base
;
43 stream
->_flag
&= ~(_IOREAD
|_IOEOF
);
44 stream
->_flag
|= _IOWRT
;
46 /* If we have no buffer, try to allocate one */
47 if (!stream
->_base
&& stream
!= stdout
&& stream
!= stderr
)
52 /* Check if we have a buffer now */
55 /* We have one, check if there is something to write */
56 count
= stream
->_ptr
- stream
->_base
;
58 written
= _write(stream
->_file
, stream
->_base
, count
);
62 /* Reset buffer and put the char into it */
63 stream
->_ptr
= stream
->_base
+ sizeof(TCHAR
);
64 stream
->_cnt
= stream
->_bufsiz
- sizeof(TCHAR
);
65 *(TCHAR
*)stream
->_base
= ch
;
69 /* There is no buffer, write the char directly */
70 count
= sizeof(TCHAR
);
71 written
= _write(stream
->_file
, &ch
, sizeof(TCHAR
));
74 /* Check for failure */
77 stream
->_flag
|= _IOERR
;