123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include "_stdio.h"
- #if (_IOFBF != 0) || (_IOLBF != 1) || (_IONBF != 2)
- #error Assumption violated -- values of _IOFBF, _IOLBF, _IONBF
- #endif
- #if (__FLAG_FBF != 0) || (__FLAG_NBF != (2*__FLAG_LBF))
- #error Assumption violated for buffering mode flags
- #endif
- int setvbuf(register FILE * __restrict stream, register char * __restrict buf,
- int mode, size_t size)
- {
- #ifdef __STDIO_BUFFERS
- int retval = EOF;
- int alloc_flag = 0;
- __STDIO_AUTO_THREADLOCK_VAR;
- __STDIO_AUTO_THREADLOCK(stream);
- __STDIO_STREAM_VALIDATE(stream);
- if (((unsigned int) mode) > 2) {
- __set_errno(EINVAL);
- goto ERROR;
- }
-
- #ifdef __STDIO_FLEXIBLE_SETVBUF
-
- if (stream->__modeflags & (__MASK_READING|__FLAG_WRITING)) {
- goto ERROR;
- }
- #else
-
- if (stream->__modeflags & (__MASK_READING|__FLAG_WRITING
- |__FLAG_NARROW|__FLAG_WIDE
- |__FLAG_ERROR|__FLAG_EOF)
- ) {
- goto ERROR;
- }
- #endif
- stream->__modeflags &= ~(__MASK_BUFMODE);
- stream->__modeflags |= mode * __FLAG_LBF;
- if ((mode == _IONBF) || !size) {
- size = 0;
- buf = NULL;
- } else if (!buf) {
- if ((__STDIO_STREAM_BUFFER_SIZE(stream) == size)
- || !(buf = malloc(size))
- ) {
- goto DONE;
- }
- alloc_flag = __FLAG_FREEBUF;
- }
- if (stream->__modeflags & __FLAG_FREEBUF) {
- stream->__modeflags &= ~(__FLAG_FREEBUF);
- free(stream->__bufstart);
- }
- stream->__modeflags |= alloc_flag;
- stream->__bufstart = (unsigned char *) buf;
- stream->__bufend = (unsigned char *) buf + size;
- __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
- __STDIO_STREAM_DISABLE_GETC(stream);
- __STDIO_STREAM_DISABLE_PUTC(stream);
- DONE:
- retval = 0;
- ERROR:
- __STDIO_STREAM_VALIDATE(stream);
- __STDIO_AUTO_THREADUNLOCK(stream);
- return retval;
- #else
- if (mode == _IONBF) {
- return 0;
- }
- if (((unsigned int) mode) > 2) {
- __set_errno(EINVAL);
- }
- return EOF;
- #endif
- }
- libc_hidden_def(setvbuf)
|