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