]> pd.if.org Git - pdclib/blob - functions/stdio/freopen.c
Improved testdrivers.
[pdclib] / functions / stdio / freopen.c
1 /* $Id$ */
2
3 /* freopen( const char *, const 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 #include <_PDCLIB_glue.h>
14 #include <stdlib.h>
15
16 /* Close any file currently associated with the given stream. Open the file
17    identified by the given filename with the given mode (equivalent to fopen()),
18    and associate it with the given stream. If filename is a NULL pointer,
19    attempt to change the mode of the given stream.
20    This implementation allows the following mode changes: TODO
21    (Primary use of this function is to redirect stdin, stdout, and stderr.)
22 */
23
24 struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, struct _PDCLIB_file_t * stream )
25 {
26     /* FIXME: This is ad-hoc (to make the vprintf() testdriver work), and must be checked. */
27     /* FIXME: If filename is NULL, change mode. */
28     if ( filename == NULL ) return NULL;
29     if ( stream->status & _PDCLIB_WROTELAST ) fflush( stream );
30     if ( stream->status & _PDCLIB_LIBBUFFER ) free( stream->buffer );
31     _PDCLIB_close( stream->handle );
32     clearerr( stream );
33     if ( ( mode == NULL ) || ( filename[0] == '\0' ) ) return NULL;
34     if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 ) return NULL;
35     stream->handle = _PDCLIB_open( filename, stream->status );
36     if ( ( stream->buffer = malloc( BUFSIZ ) ) == NULL ) return NULL;
37     stream->bufsize = BUFSIZ;
38     stream->bufidx = 0;
39     stream->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR );
40     /* TODO: Setting mbstate */
41     return stream;
42 }
43
44 #endif
45
46 #ifdef TEST
47 #include <_PDCLIB_test.h>
48
49 int main( void )
50 {
51     TESTCASE( NO_TESTDRIVER );
52     return TEST_RESULTS;
53 }
54
55 #endif