3 /* fopen( const char *, const char * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 #include <_PDCLIB_glue.h>
15 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
17 struct _PDCLIB_file_t * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode )
19 struct _PDCLIB_file_t * rc;
20 if ( mode == NULL || filename == NULL || filename[0] == '\0' )
22 /* Mode or filename invalid */
25 if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) ) ) == NULL )
27 /* no memory for another FILE */
30 if ( ( rc->status = _PDCLIB_filemode( mode ) ) == 0 )
36 rc->handle = _PDCLIB_open( filename, rc->status );
37 if ( rc->handle == _PDCLIB_NOHANDLE )
39 /* OS open() failed */
43 /* Adding to list of open files */
44 rc->next = _PDCLIB_filelist;
45 _PDCLIB_filelist = rc;
46 /* Setting buffer, and mark as internal. TODO: Check for unbuffered */
47 if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL )
52 if ( ( rc->ungetbuf = malloc( _PDCLIB_UNGETCBUFSIZE ) ) == NULL )
61 /* Setting buffer to _IOLBF because "when opened, a stream is fully
62 buffered if and only if it can be determined not to refer to an
65 rc->status |= _PDCLIB_LIBBUFFER | _IOLBF;
66 /* TODO: Setting mbstate */
73 #include <_PDCLIB_test.h>
77 /* Some of the tests are not executed for regression tests, as the libc on
78 my system is at once less forgiving (segfaults on mode NULL) and more
79 forgiving (accepts undefined modes).
82 TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );
83 TESTCASE( fopen( NULL, "w" ) == NULL );
84 TESTCASE_NOREG( fopen( "", NULL ) == NULL );
85 TESTCASE( fopen( "", "w" ) == NULL );
86 TESTCASE( fopen( "foo", "" ) == NULL );
87 TESTCASE_NOREG( fopen( "testfile", "wq" ) == NULL ); /* Undefined mode */
88 TESTCASE_NOREG( fopen( "testfile", "wr" ) == NULL ); /* Undefined mode */
89 TESTCASE( fopen( "testfile", "w" ) != NULL );