]> pd.if.org Git - pdclib/blob - functions/stdio/setvbuf.c
Porting current working set from CVS.
[pdclib] / functions / stdio / setvbuf.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* setvbuf( FILE *, char *, int, size_t )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #ifndef REGTEST
15
16 int setvbuf( FILE * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf, int mode, size_t size )
17 {
18     /* TODO: Only allowed on a "virgin" stream; add check. */
19     if ( ( stream->status & ( _IOFBF | _IOLBF | _IONBF ) ) /* Only allowed on "virgin" stream */
20       || ( ( mode != _IOFBF ) && ( mode != _IOLBF ) && ( mode != _IONBF ) ) /* invalid mode */
21       || ( ( buf == NULL ) && ( ( buf = malloc( size ) ) == NULL ) ) /* no memory available */
22     )
23     {
24         return -1;
25     }
26     stream->status |= mode;
27     stream->buffer  = buf;
28     stream->bufsize = size;
29     return 0;
30 }
31
32 #endif
33
34 #ifdef TEST
35 #include <_PDCLIB_test.h>
36
37 int main( void )
38 {
39     TESTCASE( NO_TESTDRIVER );
40     return TEST_RESULTS;
41 }
42
43 #endif