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