]> pd.if.org Git - pdclib/blob - functions/stdio/fopen.c
PDCLIB-8: First phase of intergation of new I/O backend system (with minimal
[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;
28     const _PDCLIB_fileops_t * ops;
29     if(!_PDCLIB_open( &fd, &ops, filename, imode )) {
30         return NULL;
31     }
32
33     FILE * f = _PDCLIB_fvopen( fd, ops, imode, filename );
34     if(!f) {
35         int saveErrno = errno;
36         ops->close(fd);
37         errno = saveErrno;
38     }
39     return f;
40 }
41
42 #endif
43
44 #ifdef TEST
45 #include <_PDCLIB_test.h>
46
47 int main( void )
48 {
49     /* Some of the tests are not executed for regression tests, as the libc on
50        my system is at once less forgiving (segfaults on mode NULL) and more
51        forgiving (accepts undefined modes).
52     */
53     FILE * fh;
54     remove( testfile );
55     TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );
56     TESTCASE( fopen( NULL, "w" ) == NULL );
57     TESTCASE_NOREG( fopen( "", NULL ) == NULL );
58     TESTCASE( fopen( "", "w" ) == NULL );
59     TESTCASE( fopen( "foo", "" ) == NULL );
60     TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */
61     TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */
62     TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );
63     TESTCASE( fclose( fh ) == 0 );
64     TESTCASE( remove( testfile ) == 0 );
65     return TEST_RESULTS;
66 }
67
68 #endif