From d2c64b8390799b64a24609e689ef87dec9b74dea Mon Sep 17 00:00:00 2001 From: solar Date: Wed, 7 Jun 2006 18:10:53 +0000 Subject: [PATCH] Added fclose(). --- functions/stdio/fclose.c | 73 ++++++++++++++++++++++++++++++++++++++++ functions/stdio/fopen.c | 41 +++++++++++++++++----- internals/_PDCLIB_int.h | 6 +++- 3 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 functions/stdio/fclose.c diff --git a/functions/stdio/fclose.c b/functions/stdio/fclose.c new file mode 100644 index 0000000..ff01058 --- /dev/null +++ b/functions/stdio/fclose.c @@ -0,0 +1,73 @@ +/* $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 +#include + +#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 diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index ad8d876..2abb664 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -12,10 +12,11 @@ #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 ) @@ -38,7 +39,7 @@ 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 */ @@ -60,15 +61,15 @@ static unsigned int filemode( char const * const mode ) 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; @@ -80,8 +81,9 @@ FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restr /* 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: @@ -96,7 +98,30 @@ 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; } diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index 1cc8c01..a0a6370 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -257,10 +257,14 @@ typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t; /* Flags for representing mode (see fopen()). */ #define _PDCLIB_FREAD 1u #define _PDCLIB_FWRITE 2u -#define _PDCLIB_FAPPEND 4u +#define _PDCLIB_FAPPEND 4u #define _PDCLIB_FRW 8u #define _PDCLIB_FBIN 16u +/* Internal flags, made to fit the same status field as the flags above. */ +#define _PDCLIB_WROTELAST 32u +#define _PDCLIB_LIBBUFFER 64u + struct _PDCLIB_file_t { _PDCLIB_fd_t handle; /* OS-specific file descriptor */ -- 2.40.0