#ifndef REGTEST
#include <_PDCLIB_glue.h>
+#include <string.h>
+#include <errno.h>
-/* FIXME: This approach is a possible attack vector. */
-struct _PDCLIB_file_t * _PDCLIB_filelist = NULL;
+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 */
+
+ _PDCLIB_fd_t fd = _PDCLIB_open( filename, imode );
+ if(fd == _PDCLIB_NOHANDLE) {
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_fdopen( fd, imode, filename );
+ if(!f) {
+ int saveErrno = errno;
+ _PDCLIB_close( fd );
+ errno = saveErrno;
+ }
+ return f;
}
#endif
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;
}