X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=5bcf5d81fe95d17e9406017676d24ff6a886ebcf;hb=HEAD;hp=f22287da4669bf8b15441b8076089103aea2a5ad;hpb=d9dcf16664c81809258992e1653ecb68c8079974;p=pdclib.old diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index f22287d..5bcf5d8 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -10,65 +10,34 @@ #include #ifndef REGTEST +#include <_PDCLIB_io.h> #include <_PDCLIB_glue.h> +#include +#include -static const FILE * _PDCLIB_filelist = NULL; +extern FILE * _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; + const _PDCLIB_fileops_t * ops; + if(!_PDCLIB_open( &fd, &ops, filename, imode )) { 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_fvopen( fd, ops, imode, filename ); + if(!f) { + int saveErrno = errno; + ops->close(fd); + errno = saveErrno; + } + return f; } #endif @@ -78,7 +47,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; }