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 static FILE * _PDCLIB_filelist = NULL;
17 /* Helper function that parses the C-style mode string passed to fopen() into
18 the PDCLib flags.FREAD, FWRITE, FAPPEND, FRW (read-write) and FBIN (
21 static unsigned int filemode( char const * const mode )
33 rc |= _PDCLIB_FAPPEND;
36 /* Other than read, write, or append - invalid */
39 for ( size_t i = 1; i < 4; ++i )
44 if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */
48 if ( rc & _PDCLIB_FBIN ) return 0; /* Duplicates are invalid */
55 /* Other than read/write or binary - invalid. */
59 /* Longer than three chars - invalid. */
63 FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode )
66 if ( mode == NULL || filename == NULL || filename[0] == '\0' )
68 /* Mode or filename invalid */
71 if ( ( rc = calloc( 1, sizeof( FILE ) ) ) == NULL )
73 /* no memory for another FILE */
76 if ( ( rc->status = filemode( mode ) ) == 0 ) goto fail; /* invalid mode */
77 rc->handle = _PDCLIB_open( filename, rc->status );
78 if ( rc->handle == _PDCLIB_NOHANDLE ) goto fail; /* OS open() failed */
80 /* Adding to list of open files */
81 rc->next = _PDCLIB_filelist;
82 _PDCLIB_filelist = rc;
83 /* Setting buffer. TODO: Check for unbuffered? */
84 if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail;
85 /* TODO: Setting mbstate */
95 #include <_PDCLIB_test.h>
99 TESTCASE( NO_TESTDRIVER );