--- /dev/null
+/* $Id$ */
+
+/* fclose( FILE * )
+
+ This file is part of the Public Domain C Library (PDCLib).
+ Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifndef REGTEST
+#include <_PDCLIB_glue.h>
+
+extern struct _PDCLIB_file_t * _PDCLIB_filelist;
+
+/* FIXME: Last file not removed from list. */
+int fclose( struct _PDCLIB_file_t * stream )
+{
+ struct _PDCLIB_file_t * current = _PDCLIB_filelist;
+ struct _PDCLIB_file_t * previous = NULL;
+ /* Checking that the FILE handle is actually one we had opened before. */
+ while ( current != NULL )
+ {
+ if ( stream == current )
+ {
+ if ( stream->status & _PDCLIB_WROTELAST ) fflush( stream );
+ if ( stream->status & _PDCLIB_LIBBUFFER ) free( stream->buffer );
+ _PDCLIB_close( stream->handle );
+ if ( previous != NULL )
+ {
+ previous = current->next;
+ }
+ else
+ {
+ _PDCLIB_filelist = current->next;
+ }
+ return 0;
+ }
+ previous = current;
+ current = current->next;
+ }
+ return -1;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+ /* FIXME: This is basically fopen() checking. Flushing and buffer-freeing is not checked. */
+ struct _PDCLIB_file_t * file1;
+ struct _PDCLIB_file_t * file2;
+ TESTCASE( _PDCLIB_filelist == NULL );
+ TESTCASE( ( file1 = fopen( "testfile1", "w" ) ) != NULL );
+ TESTCASE( _PDCLIB_filelist == file1 );
+ TESTCASE( ( file2 = fopen( "testfile2", "w" ) ) != NULL );
+ TESTCASE( _PDCLIB_filelist == file2 );
+ TESTCASE( fclose( file2 ) == 0 );
+ TESTCASE( _PDCLIB_filelist == file1 );
+ TESTCASE( ( file2 = fopen( "testfile1", "w" ) ) != NULL );
+ TESTCASE( _PDCLIB_filelist == file2 );
+ TESTCASE( fclose( file1 ) == 0 );
+ TESTCASE( _PDCLIB_filelist == file2 );
+ TESTCASE( fclose( file2 ) == 0 );
+ TESTCASE( _PDCLIB_filelist == NULL );
+ system( "rm testfile1 testfile2" );
+ return TEST_RESULTS;
+}
+
+#endif
#ifndef REGTEST
#include <_PDCLIB_glue.h>
-static FILE * _PDCLIB_filelist = NULL;
+/* FIXME: This approach is a possible attack vector. */
+struct _PDCLIB_file_t * _PDCLIB_filelist = NULL;
/* Helper function that parses the C-style mode string passed to fopen() into
- the PDCLib flags.FREAD, FWRITE, FAPPEND, FRW (read-write) and FBIN (
+ the PDCLib flags FREAD, FWRITE, FAPPEND, FRW (read-write) and FBIN (binary
mode).
*/
static unsigned int filemode( char const * const mode )
}
for ( size_t i = 1; i < 4; ++i )
{
- switch ( mode[1] )
+ switch ( mode[i] )
{
case '+':
if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */
return 0;
}
-FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode )
+struct _PDCLIB_file_t * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode )
{
- FILE * rc;
+ struct _PDCLIB_file_t * rc;
if ( mode == NULL || filename == NULL || filename[0] == '\0' )
{
/* Mode or filename invalid */
return NULL;
}
- if ( ( rc = calloc( 1, sizeof( FILE ) ) ) == NULL )
+ if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) ) ) == NULL )
{
/* no memory for another FILE */
return NULL;
/* Adding to list of open files */
rc->next = _PDCLIB_filelist;
_PDCLIB_filelist = rc;
- /* Setting buffer. TODO: Check for unbuffered? */
+ /* Setting buffer, and mark as internal. TODO: Check for unbuffered? */
if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail;
+ rc->status |= _PDCLIB_LIBBUFFER;
/* TODO: Setting mbstate */
return rc;
fail:
int main( void )
{
- TESTCASE( NO_TESTDRIVER );
+ TESTCASE( filemode( "r" ) == _PDCLIB_FREAD );
+ TESTCASE( filemode( "w" ) == _PDCLIB_FWRITE );
+ TESTCASE( filemode( "a" ) == _PDCLIB_FAPPEND );
+ TESTCASE( filemode( "r+" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW ) );
+ TESTCASE( filemode( "w+" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW ) );
+ TESTCASE( filemode( "a+" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FRW ) );
+ TESTCASE( filemode( "rb" ) == ( _PDCLIB_FREAD | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "wb" ) == ( _PDCLIB_FWRITE | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "ab" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "r+b" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "w+b" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "a+b" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FRW | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "rb+" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "wb+" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
+ TESTCASE( filemode( "ab+" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FRW | _PDCLIB_FBIN ) );
+ TESTCASE( fopen( NULL, NULL ) == NULL );
+ TESTCASE( fopen( NULL, "w" ) == NULL );
+ TESTCASE( fopen( "", NULL ) == NULL );
+ TESTCASE( fopen( "", "w" ) == NULL );
+ TESTCASE( fopen( "foo", "" ) == NULL );
+ TESTCASE( fopen( "testfile", "wq" ) == NULL ); /* Illegal mode */
+ TESTCASE( fopen( "testfile", "wr" ) == NULL ); /* Illegal mode */
+ TESTCASE( fopen( "testfile", "w" ) != NULL );
+ system( "rm testfile" );
return TEST_RESULTS;
}