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 /* FIXME: This approach is a possible attack vector. */
16 struct _PDCLIB_file_t * _PDCLIB_filelist = NULL;
18 struct _PDCLIB_file_t * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode )
20 struct _PDCLIB_file_t * rc;
21 if ( mode == NULL || filename == NULL || filename[0] == '\0' )
23 /* Mode or filename invalid */
26 if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) ) ) == NULL )
28 /* no memory for another FILE */
31 if ( ( rc->status = _PDCLIB_filemode( mode ) ) == 0 ) goto fail; /* invalid mode */
32 rc->handle = _PDCLIB_open( filename, rc->status );
33 if ( rc->handle == _PDCLIB_NOHANDLE ) goto fail; /* OS open() failed */
34 /* Adding to list of open files */
35 rc->next = _PDCLIB_filelist;
36 _PDCLIB_filelist = rc;
37 /* Setting buffer, and mark as internal. TODO: Check for unbuffered */
38 if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail;
41 rc->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR );
42 /* TODO: Setting mbstate */
52 #include <_PDCLIB_test.h>
56 /* Some of the tests are not executed for regression tests, as the libc on
57 my system is at once less forgiving (segfaults on mode NULL) and more
58 forgiving (accepts undefined modes).
61 TESTCASE( fopen( NULL, NULL ) == NULL );
63 TESTCASE( fopen( NULL, "w" ) == NULL );
65 TESTCASE( fopen( "", NULL ) == NULL );
67 TESTCASE( fopen( "", "w" ) == NULL );
68 TESTCASE( fopen( "foo", "" ) == NULL );
70 TESTCASE( fopen( "testfile", "wq" ) == NULL ); /* Undefined mode */
71 TESTCASE( fopen( "testfile", "wr" ) == NULL ); /* Undefined mode */
73 TESTCASE( fopen( "testfile", "w" ) != NULL );
74 system( "rm testfile" );