]> pd.if.org Git - pdclib/blob - functions/stdio/fopen.c
_PDCLIB_flushbuffer for win32. correct seeking behaviour.
[pdclib] / functions / stdio / fopen.c
1 /* $Id$ */
2
3 /* fopen( const char *, const char * )
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 #include <stdlib.h>
11
12 #ifndef REGTEST
13 #include <_PDCLIB_glue.h>
14 #include <string.h>
15 #include <errno.h>
16
17 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
18
19 FILE * fopen( const char * _PDCLIB_restrict filename, 
20               const char * _PDCLIB_restrict mode )
21 {
22     int imode = _PDCLIB_filemode( mode );
23     
24     if( imode == 0 || filename == NULL )
25         return NULL;
26
27     _PDCLIB_fd_t fd = _PDCLIB_open( filename, imode );
28     if(fd == _PDCLIB_NOHANDLE) {
29         return NULL;
30     }
31
32     FILE * f = _PDCLIB_fdopen( fd, imode, filename );
33     if(!f) {
34         int saveErrno = errno;
35         _PDCLIB_close( fd );
36         errno = saveErrno;
37     }
38     return f;
39 }
40
41 #endif
42
43 #ifdef TEST
44 #include <_PDCLIB_test.h>
45
46 int main( void )
47 {
48     /* Some of the tests are not executed for regression tests, as the libc on
49        my system is at once less forgiving (segfaults on mode NULL) and more
50        forgiving (accepts undefined modes).
51     */
52     FILE * fh;
53     remove( testfile );
54     TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );
55     TESTCASE( fopen( NULL, "w" ) == NULL );
56     TESTCASE_NOREG( fopen( "", NULL ) == NULL );
57     TESTCASE( fopen( "", "w" ) == NULL );
58     TESTCASE( fopen( "foo", "" ) == NULL );
59     TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */
60     TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */
61     TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );
62     TESTCASE( fclose( fh ) == 0 );
63     TESTCASE( remove( testfile ) == 0 );
64     return TEST_RESULTS;
65 }
66
67 #endif