X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=5bcf5d81fe95d17e9406017676d24ff6a886ebcf;hb=HEAD;hp=e15338319ac07cf8ee3025fefda51be1da36ab7b;hpb=4ad1ea669bfc556cf4611ed9b00d2a262f301d31;p=pdclib.old diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index e153383..5bcf5d8 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -10,40 +10,34 @@ #include #ifndef REGTEST +#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 @@ -57,21 +51,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; }