3 /* setvbuf( FILE *, char *, int, size_t )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
14 int setvbuf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf, int mode, size_t size )
16 /* Only allowed on "virgin" streams (i.e., before first I/O occurs), and
17 a valid value for mode.
19 if ( ( ! stream->status & _PDCLIB_VIRGINSTR ) ||
20 ( ( mode != _IOFBF ) && ( mode != _IOLBF ) && ( mode != _IONBF ) ) )
24 /* If a buffer is provided by user... */
27 /* ...do not free it in library functions like fclose(), freopen(). */
28 stream->status &= ~_PDCLIB_LIBBUFFER;
30 /* If no buffer is provided by user, but required... */
31 else if ( mode != _IONBF )
33 /* Since setvbuf() may be called (successfully) on a stream only once,
34 the stream's buffer at this point should *always* be that allocated
35 by fopen(), but better make sure.
37 if ( ! ( stream->status & _PDCLIB_LIBBUFFER ) )
41 /* Drop old buffer, allocate new one of requested size (unless that is
42 equal to BUFSIZ, in which case we can use the one already allocated
47 if ( ( buf = malloc( size ) ) == NULL )
51 free( stream->buffer );
54 /* Applying new settings to stream. */
55 stream->status &= ~( _IOFBF | _IOLBF | _IONBF );
56 stream->status |= mode;
58 stream->bufsize = size;
59 stream->status &= ~_PDCLIB_VIRGINSTR;
66 #include <_PDCLIB_test.h>
71 TESTCASE( NO_TESTDRIVER );
73 puts( " NOTEST setvbuf() test driver is PDCLib-specific." );