3 /* freopen( const char *, const char * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 #include <_PDCLIB_glue.h>
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.)
24 struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, struct _PDCLIB_file_t * stream )
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 /* TODO: This function can change wide orientation of a stream */
29 if ( filename == NULL ) return NULL;
30 if ( stream->status & _PDCLIB_WROTELAST ) fflush( stream );
31 if ( stream->status & _PDCLIB_LIBBUFFER ) free( stream->buffer );
32 _PDCLIB_close( stream->handle );
34 if ( ( mode == NULL ) || ( filename[0] == '\0' ) ) return NULL;
35 if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 ) return NULL;
36 stream->handle = _PDCLIB_open( filename, stream->status );
37 if ( ( stream->buffer = malloc( BUFSIZ ) ) == NULL ) return NULL;
38 stream->bufsize = BUFSIZ;
40 stream->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR );
41 /* TODO: Setting mbstate */
48 #include <_PDCLIB_test.h>
52 TESTCASE( NO_TESTDRIVER );