X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=e806b00a2ef76c31879a2aa0a19b55b368962aa6;hb=c5d49235e09fbd58416f10dec2799e61cf3431c8;hp=48c07d2b3ff41d6c2244461bc417832546063161;hpb=393020b6e48719d27699dea6b29e53025bbd5123;p=pdclib diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index 48c07d2..e806b00 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -11,60 +11,32 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> +#include +#include extern struct _PDCLIB_file_t * _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 */ - return NULL; - } - if ( ( rc->status = _PDCLIB_filemode( mode ) ) == 0 ) - { - /* invalid mode */ - free( rc ); - return NULL; - } - rc->handle = _PDCLIB_open( filename, rc->status ); - if ( rc->handle == _PDCLIB_NOHANDLE ) - { - /* OS open() failed */ - free( rc ); - return NULL; - } - /* 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 ) - { - free( rc ); + + _PDCLIB_fd_t fd; + const _PDCLIB_fileops_t * ops; + if(!_PDCLIB_open( &fd, &ops, filename, imode )) { return NULL; } - if ( ( rc->ungetbuf = malloc( _PDCLIB_UNGETCBUFSIZE ) ) == NULL ) - { - free( rc->buffer ); - free( rc ); - return NULL; + + FILE * f = _PDCLIB_fvopen( fd, ops, imode, filename ); + if(!f) { + int saveErrno = errno; + ops->close(fd); + errno = saveErrno; } - rc->bufsize = BUFSIZ; - rc->bufidx = 0; - rc->ungetidx = 0; - /* Setting buffer to _IOLBF because "when opened, a stream is fully - buffered if and only if it can be determined not to refer to an - interactive device." - */ - rc->status |= _PDCLIB_LIBBUFFER | _IOLBF; - /* TODO: Setting mbstate */ - return rc; + return f; } #endif @@ -78,16 +50,18 @@ int main( void ) my system is at once less forgiving (segfaults on mode NULL) and more forgiving (accepts undefined modes). */ - remove( "testfile" ); + 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( fopen( "testfile", "w" ) != NULL ); - remove( "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; }