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