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