X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=ff8e8e8a08862ea7679d54d3457d41531c3a7599;hb=04ff9a4a124eaa87d5d26d90077fb4ed15f3277f;hp=4d249efc1bc6948204dee36719453b006e8188fb;hpb=d02f38605b53cdff5460cc6b9e1b2a80c3a2ba4c;p=pdclib diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index 4d249ef..ff8e8e8 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -1,7 +1,5 @@ /* $Id$ */ -/* Release $Name$ */ - /* fopen( const char *, const char * ) This file is part of the Public Domain C Library (PDCLib). @@ -13,64 +11,31 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> +#include +#include -static const FILE * _PDCLIB_filelist = NULL; +extern struct _PDCLIB_file_t * _PDCLIB_filelist; -static int filemode( char const * const mode ) +FILE * fopen( const char * _PDCLIB_restrict filename, + const char * _PDCLIB_restrict mode ) { - int rc = 0; - switch ( mode[0] ) - { - case 'r': - rc |= _PDCLIB_FREAD; - break; - case 'w': - rc |= _PDCLIB_FWRITE; - break; - case 'a': - rc |= _PDCLIB_FAPPEND; - break; - default: - return -1; - } - for ( size_t i = 1; i < 4; ++i ) - { - switch ( mode[1] ) - { - case '+': - if ( rc & _PDCLIB_FRW ) return -1; - rc |= _PDCLIB_FRW; - break; - case 'b': - if ( rc & _PDCLIB_FBIN ) return -1; - rc |= _PDCLIB_FBIN; - break; - case '\0': - return rc; - default: - return -1; - } - } - return -1; -} + int imode = _PDCLIB_filemode( mode ); + + if( imode == 0 || filename == NULL ) + return NULL; -FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ) -{ - FILE * rc; - if ( mode == NULL || filename == NULL || filename[0] == '\0' ) - { + _PDCLIB_fd_t fd = _PDCLIB_open( filename, imode ); + if(fd == _PDCLIB_NOHANDLE) { return NULL; } - if ( ( rc = calloc( 1, sizeof( FILE ) ) ) == NULL ) return rc; /* no space for another FILE */ - if ( ( rc->status = filemode( mode ) ) == -1 ) goto fail; /* invalid mode given */ - if ( ( rc->handle = _PDCLIB_open( filename, rc->status ) ) == -1 ) goto fail; /* OS "open" failed */ - rc->next = _PDCLIB_filelist; - _PDCLIB_filelist = rc; - /* TODO: Continue here: Set up PDCLib FILE contents */ - return rc; -fail: - free( rc ); - return NULL; + + FILE * f = _PDCLIB_fdopen( fd, imode, filename ); + if(!f) { + int saveErrno = errno; + _PDCLIB_close( fd ); + errno = saveErrno; + } + return f; } #endif @@ -80,7 +45,22 @@ fail: int main( void ) { - TESTCASE( NO_TESTDRIVER ); + /* Some of the tests are not executed for regression tests, as the libc on + my system is at once less forgiving (segfaults on mode NULL) and more + forgiving (accepts undefined modes). + */ + FILE * fh; + remove( testfile ); + TESTCASE_NOREG( fopen( NULL, NULL ) == NULL ); + TESTCASE( fopen( NULL, "w" ) == NULL ); + TESTCASE_NOREG( fopen( "", NULL ) == NULL ); + TESTCASE( fopen( "", "w" ) == NULL ); + TESTCASE( fopen( "foo", "" ) == NULL ); + TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */ + TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */ + TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( remove( testfile ) == 0 ); return TEST_RESULTS; }