]> pd.if.org Git - pdclib/blob - functions/stdio/freopen.c
PDCLIB-16: Add _unlocked variations of all I/O routines; move work into these versions
[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     _PDCLIB_close( stream->handle );
37     /* TODO: It is not nice to do this on a stream we just closed.
38        It does not matter with the current implementation of clearerr(),
39        but it might start to matter if someone replaced that implementation.
40     */
41     clearerr( stream );
42     /* The new filename might not fit the old buffer */
43     if ( filename == NULL )
44     {
45         /* Use previous filename */
46         filename = stream->filename;
47     }
48     else if ( ( stream->filename != NULL ) && ( strlen( stream->filename ) >= strlen( filename ) ) )
49     {
50         /* Copy new filename into existing buffer */
51         strcpy( stream->filename, filename );
52     }
53     else
54     {
55         /* Allocate new buffer */
56         if ( ( stream->filename = (char *)malloc( strlen( filename ) ) ) == NULL )
57         {
58             funlockfile( stream );
59             return NULL;
60         }
61         strcpy( stream->filename, filename );
62     }
63     if ( ( mode == NULL ) || ( filename[0] == '\0' ) )
64     {
65         funlockfile( stream );
66         return NULL;
67     }
68     if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 )
69     {
70         funlockfile( stream );
71         return NULL;
72     }
73     /* Re-add the flags we saved above */
74     stream->status |= status;
75     stream->bufidx = 0;
76     stream->bufend = 0;
77     stream->ungetidx = 0;
78     /* TODO: Setting mbstate */
79     if ( ( stream->handle = _PDCLIB_open( filename, stream->status ) ) == _PDCLIB_NOHANDLE )
80     {
81         funlockfile( stream );
82         return NULL;
83     }
84     funlockfile( stream );
85     return stream;
86 }
87
88 #endif
89
90 #ifdef TEST
91 #include <_PDCLIB_test.h>
92
93 int main( void )
94 {
95     FILE * fin;
96     FILE * fout;
97     TESTCASE( ( fin = fopen( testfile1, "wb+" ) ) != NULL );
98     TESTCASE( fputc( 'x', fin ) == 'x' );
99     TESTCASE( fclose( fin ) == 0 );
100     TESTCASE( ( fin = freopen( testfile1, "rb", stdin ) ) != NULL );
101     TESTCASE( getchar() == 'x' );
102
103     TESTCASE( ( fout = freopen( testfile2, "wb+", stdout ) ) != NULL );
104     TESTCASE( putchar( 'x' ) == 'x' );
105     rewind( fout );
106     TESTCASE( fgetc( fout ) == 'x' );
107
108     TESTCASE( fclose( fin ) == 0 );
109     TESTCASE( fclose( fout ) == 0 );
110     TESTCASE( remove( testfile1 ) == 0 );
111     TESTCASE( remove( testfile2 ) == 0 );
112
113     return TEST_RESULTS;
114 }
115
116 #endif