]> pd.if.org Git - pdclib/blob - functions/stdio/freopen.c
FIXES PDCLIB-20 by preserving _PDCLIB_STATUS flag
[pdclib] / functions / stdio / freopen.c
1 /* $Id$ */
2
3 /* freopen( const char *, const char *, FILE * )
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 #include <string.h>
16
17 struct _PDCLIB_file_t * freopen( 
18                             const char * _PDCLIB_restrict filename, 
19                             const char * _PDCLIB_restrict mode, 
20                             struct _PDCLIB_file_t * _PDCLIB_restrict stream )
21 {
22     flockfile( stream );
23
24     unsigned int status = stream->status & 
25         ( _IONBF | _IOLBF | _IOFBF | _PDCLIB_FREEBUFFER 
26         | _PDCLIB_DELONCLOSE | _PDCLIB_STATIC );
27
28     /* TODO: This function can change wide orientation of a stream */
29     if ( stream->status & _PDCLIB_FWRITE )
30     {
31         _PDCLIB_flushbuffer( stream );
32     }
33     if ( ( filename == NULL ) && ( stream->filename == NULL ) )
34     {
35         /* TODO: Special handling for mode changes on std-streams */
36         funlockfile( stream );
37         return NULL;
38     }
39     stream->ops->close(stream->handle);
40     
41     /* TODO: It is not nice to do this on a stream we just closed.
42        It does not matter with the current implementation of clearerr(),
43        but it might start to matter if someone replaced that implementation.
44     */
45     clearerr( stream );
46     /* The new filename might not fit the old buffer */
47     if ( filename == NULL )
48     {
49         /* Use previous filename */
50         filename = stream->filename;
51     }
52     else if ( ( stream->filename != NULL ) && ( strlen( stream->filename ) >= strlen( filename ) ) )
53     {
54         /* Copy new filename into existing buffer */
55         strcpy( stream->filename, filename );
56     }
57     else
58     {
59         /* Allocate new buffer */
60         if ( ( stream->filename = (char *)malloc( strlen( filename ) ) ) == NULL )
61         {
62             funlockfile( stream );
63             return NULL;
64         }
65         strcpy( stream->filename, filename );
66     }
67     if ( ( mode == NULL ) || ( filename[0] == '\0' ) )
68     {
69         funlockfile( stream );
70         return NULL;
71     }
72     if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 )
73     {
74         funlockfile( stream );
75         return NULL;
76     }
77     /* Re-add the flags we saved above */
78     stream->status |= status;
79     stream->bufidx = 0;
80     stream->bufend = 0;
81     stream->ungetidx = 0;
82     /* TODO: Setting mbstate */
83     if ( ! _PDCLIB_open( &stream->handle, &stream->ops, filename, 
84                          stream->status ) )
85     {
86         funlockfile( stream );
87         return NULL;
88     }
89     funlockfile( stream );
90     return stream;
91 }
92
93 #endif
94
95 #ifdef TEST
96 #include <_PDCLIB_test.h>
97
98 int main( void )
99 {
100     FILE * fin;
101     FILE * fout;
102     TESTCASE( ( fin = fopen( testfile1, "wb+" ) ) != NULL );
103     TESTCASE( fputc( 'x', fin ) == 'x' );
104     TESTCASE( fclose( fin ) == 0 );
105     TESTCASE( ( fin = freopen( testfile1, "rb", stdin ) ) != NULL );
106     TESTCASE( getchar() == 'x' );
107
108     TESTCASE( ( fout = freopen( testfile2, "wb+", stdout ) ) != NULL );
109     TESTCASE( putchar( 'x' ) == 'x' );
110     rewind( fout );
111     TESTCASE( fgetc( fout ) == 'x' );
112
113     TESTCASE( fclose( fin ) == 0 );
114     TESTCASE( fclose( fout ) == 0 );
115     TESTCASE( remove( testfile1 ) == 0 );
116     TESTCASE( remove( testfile2 ) == 0 );
117
118     return TEST_RESULTS;
119 }
120
121 #endif