3 /* freopen( const char *, const char *, FILE * )
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 any mode changes.
21 (Primary use of this function is to redirect stdin, stdout, and stderr.)
23 struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
25 unsigned int status = stream->status & ( _IONBF | _IOLBF | _IOFBF | _PDCLIB_FREEBUFFER | _PDCLIB_DELONCLOSE );
26 /* TODO: This function can change wide orientation of a stream */
27 if ( stream->status & _PDCLIB_FWRITE )
29 _PDCLIB_flushbuffer( stream );
31 _PDCLIB_close( stream->handle );
33 /* FIXME: Copy filename into the FILE structure. */
34 /* FIXME: filename cannot reside in "big block" memory */
35 if ( filename == NULL )
37 filename = stream->filename;
39 if ( ( mode == NULL ) || ( filename[0] == '\0' ) )
43 if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 )
47 /* Re-add the flags we saved above */
48 stream->status |= status;
52 /* TODO: Setting mbstate */
53 if ( ( stream->handle = _PDCLIB_open( filename, stream->status ) ) == _PDCLIB_NOHANDLE )
63 #include <_PDCLIB_test.h>
67 TESTCASE( NO_TESTDRIVER );