]> pd.if.org Git - pdclib/blob - functions/stdio/setvbuf.c
Comment cleanups.
[pdclib] / functions / stdio / setvbuf.c
1 /* setvbuf( FILE *, char *, int, size_t )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <limits.h>
10
11 #ifndef REGTEST
12
13 int setvbuf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf, int mode, size_t size )
14 {
15     switch ( mode )
16     {
17         case _IONBF:
18             /* When unbuffered I/O is requested, we keep the buffer anyway, as
19                we don't want to e.g. flush the stream for every character of a
20                stream being printed.
21             */
22             break;
23         case _IOFBF:
24         case _IOLBF:
25             if ( size > INT_MAX || size == 0 )
26             {
27                 /* PDCLib only supports buffers up to INT_MAX in size. A size
28                    of zero doesn't make sense.
29                 */
30                 return -1;
31             }
32             if ( buf == NULL )
33             {
34                 /* User requested buffer size, but leaves it to library to
35                    allocate the buffer.
36                 */
37                 /* If current buffer is big enough for requested size, but not
38                    over twice as big (and wasting memory space), we use the
39                    current buffer (i.e., do nothing), to save the malloc() / 
40                    free() overhead.
41                 */
42                 if ( ( stream->bufsize < size ) || ( stream->bufsize > ( size << 1 ) ) )
43                 {
44                     /* Buffer too small, or much too large - allocate. */
45                     if ( ( buf = (char *) malloc( size ) ) == NULL )
46                     {
47                         /* Out of memory error. */
48                         return -1;
49                     }
50                     /* This buffer must be free()d on fclose() */
51                     stream->status |= _PDCLIB_FREEBUFFER;
52                 }
53             }
54             stream->buffer = buf;
55             stream->bufsize = size;
56             break;
57         default:
58             /* If mode is something else than _IOFBF, _IOLBF or _IONBF -> exit */
59             return -1;
60     }
61     /* Deleting current buffer mode */
62     stream->status &= ~( _IOFBF | _IOLBF | _IONBF );
63     /* Set user-defined mode */
64     stream->status |= mode;
65     return 0;
66 }
67
68 #endif
69
70 #ifdef TEST
71 #include <_PDCLIB_test.h>
72
73 #include <errno.h>
74
75 #define BUFFERSIZE 500
76
77 int main( void )
78 {
79 #ifndef REGTEST
80     char buffer[ BUFFERSIZE ];
81     FILE * fh;
82     /* full buffered, user-supplied buffer */
83     TESTCASE( ( fh = tmpfile() ) != NULL );
84     TESTCASE( setvbuf( fh, buffer, _IOFBF, BUFFERSIZE ) == 0 );
85     TESTCASE( fh->buffer == buffer );
86     TESTCASE( fh->bufsize == BUFFERSIZE );
87     TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IOFBF );
88     TESTCASE( fclose( fh ) == 0 );
89     /* line buffered, lib-supplied buffer */
90     TESTCASE( ( fh = tmpfile() ) != NULL );
91     TESTCASE( setvbuf( fh, NULL, _IOLBF, BUFFERSIZE ) == 0 );
92     TESTCASE( fh->buffer != NULL );
93     TESTCASE( fh->bufsize == BUFFERSIZE );
94     TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IOLBF );
95     TESTCASE( fclose( fh ) == 0 );
96     /* not buffered, user-supplied buffer */
97     TESTCASE( ( fh = tmpfile() ) != NULL );
98     TESTCASE( setvbuf( fh, buffer, _IONBF, BUFFERSIZE ) == 0 );
99     TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IONBF );
100     TESTCASE( fclose( fh ) == 0 );
101 #else
102     puts( " NOTEST setvbuf() test driver is PDCLib-specific." );
103 #endif
104     return TEST_RESULTS;
105 }
106
107 #endif
108