X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=3d9f95ab1a205917d4ec2b0424b6866a248bc99d;hp=e15338319ac07cf8ee3025fefda51be1da36ab7b;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=efc96bda49dee3cfdc433ae5bda80db96b99f963 diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index e153383..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,46 +8,40 @@ #include #ifndef REGTEST -#include <_PDCLIB_glue.h> +#include "_PDCLIB_io.h" +#include "_PDCLIB_glue.h" +#include +#include -/* FIXME: This approach is a possible attack vector. */ -struct _PDCLIB_file_t * _PDCLIB_filelist = NULL; +extern FILE * _PDCLIB_filelist; -struct _PDCLIB_file_t * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ) +FILE * fopen( const char * _PDCLIB_restrict filename, + const char * _PDCLIB_restrict mode ) { - struct _PDCLIB_file_t * rc; - if ( mode == NULL || filename == NULL || filename[0] == '\0' ) - { - /* Mode or filename invalid */ + int imode = _PDCLIB_filemode( mode ); + + if( imode == 0 || filename == NULL ) return NULL; - } - if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) ) ) == NULL ) - { - /* no memory for another FILE */ + + _PDCLIB_fd_t fd; + const _PDCLIB_fileops_t * ops; + if(!_PDCLIB_open( &fd, &ops, filename, imode )) { return NULL; } - if ( ( rc->status = _PDCLIB_filemode( mode ) ) == 0 ) goto fail; /* invalid mode */ - rc->handle = _PDCLIB_open( filename, rc->status ); - if ( rc->handle == _PDCLIB_NOHANDLE ) goto fail; /* OS open() failed */ - /* Adding to list of open files */ - rc->next = _PDCLIB_filelist; - _PDCLIB_filelist = rc; - /* Setting buffer, and mark as internal. TODO: Check for unbuffered */ - if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail; - rc->bufsize = BUFSIZ; - rc->bufidx = 0; - rc->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR ); - /* TODO: Setting mbstate */ - 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 ) { @@ -57,21 +49,18 @@ int main( void ) my system is at once less forgiving (segfaults on mode NULL) and more forgiving (accepts undefined modes). */ -#ifndef REGTEST - TESTCASE( fopen( NULL, NULL ) == NULL ); -#endif + FILE * fh; + remove( testfile ); + TESTCASE_NOREG( fopen( NULL, NULL ) == NULL ); TESTCASE( fopen( NULL, "w" ) == NULL ); -#ifndef REGTEST - TESTCASE( fopen( "", NULL ) == NULL ); -#endif + TESTCASE_NOREG( fopen( "", NULL ) == NULL ); TESTCASE( fopen( "", "w" ) == NULL ); TESTCASE( fopen( "foo", "" ) == NULL ); -#ifndef REGTEST - TESTCASE( fopen( "testfile", "wq" ) == NULL ); /* Undefined mode */ - TESTCASE( fopen( "testfile", "wr" ) == NULL ); /* Undefined mode */ -#endif - TESTCASE( fopen( "testfile", "w" ) != NULL ); - system( "rm testfile" ); + 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; }