X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=3d9f95ab1a205917d4ec2b0424b6866a248bc99d;hp=f22287da4669bf8b15441b8076089103aea2a5ad;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=b08f4b52b1cd1f7a9553c0f357a7c90859fa3e73 diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index f22287d..3d9f95a 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* fopen( const char *, const char * ) This file is part of the Public Domain C Library (PDCLib). @@ -10,75 +8,59 @@ #include #ifndef REGTEST -#include <_PDCLIB_glue.h> +#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 #ifdef TEST -#include <_PDCLIB_test.h> +#include "_PDCLIB_test.h" 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; }