]> pd.if.org Git - pdclib/blob - functions/stdio/setbuf.c
a68ea27da8ac01aef06c3a9c4c25510f6f049ee8
[pdclib] / functions / stdio / setbuf.c
1 /* $Id$ */
2
3 /* setbuf( FILE *, char * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10
11 #ifndef REGTEST
12
13 void setbuf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf )
14 {
15     if ( buf == NULL )
16     {
17         setvbuf( stream, buf, _IONBF, BUFSIZ );
18     }
19     else
20     {
21         setvbuf( stream, buf, _IOFBF, BUFSIZ );
22     }
23 }
24
25 #endif
26
27 #ifdef TEST
28 #include <_PDCLIB_test.h>
29 #include <stdlib.h>
30
31 int main( void )
32 {
33     /* TODO: Extend testing once setvbuf() is finished. */
34 #ifndef REGTEST
35     char const * const filename = "testfile";
36     char buffer[ BUFSIZ + 1 ];
37     FILE * fh;
38     remove( filename );
39     /* full buffered */
40     TESTCASE( ( fh = fopen( filename, "w" ) ) != NULL );
41     setbuf( fh, buffer );
42     TESTCASE( fh->buffer == buffer );
43     TESTCASE( fh->bufsize == BUFSIZ );
44     TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IOFBF );
45     TESTCASE( fclose( fh ) == 0 );
46     TESTCASE( remove( filename ) == 0 );
47     /* not buffered */
48     TESTCASE( ( fh = fopen( filename, "w" ) ) != NULL );
49     setbuf( fh, NULL );
50     TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IONBF );
51     TESTCASE( fclose( fh ) == 0 );
52     TESTCASE( remove( filename ) == 0 );
53 #else
54     puts( " NOTEST setbuf() test driver is PDCLib-specific." );
55 #endif
56     return TEST_RESULTS;
57 }
58
59 #endif