X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=f11094416066ed91a9f6bb6d358b4dcebea981c9;hb=5a313c5a48d18419e78d03a04501d7fe21f49d3f;hp=04020d94645a1f52dee6f3bd88da52c0e91f4850;hpb=dcc8a8e99f69e090a03b7b868443addbc0817820;p=pdclib.old diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index 04020d9..f110944 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -1,8 +1,103 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- - -FILE * fopen( const char * restrict filename, const char * restrict mode ) { /* TODO */ }; +/* $Id$ */ + +/* fopen( const char *, const char * ) + + 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> + +static FILE * _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 ( + mode). +*/ +static unsigned int filemode( char const * const mode ) +{ + int rc = 0; + switch ( mode[0] ) + { + case 'r': + rc |= _PDCLIB_FREAD; + break; + case 'w': + rc |= _PDCLIB_FWRITE; + break; + case 'a': + rc |= _PDCLIB_FAPPEND; + break; + default: + /* Other than read, write, or append - invalid */ + return 0; + } + for ( size_t i = 1; i < 4; ++i ) + { + switch ( mode[1] ) + { + case '+': + if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */ + rc |= _PDCLIB_FRW; + break; + case 'b': + if ( rc & _PDCLIB_FBIN ) return 0; /* Duplicates are invalid */ + rc |= _PDCLIB_FBIN; + break; + case '\0': + /* End of mode */ + return rc; + default: + /* Other than read/write or binary - invalid. */ + return 0; + } + } + /* Longer than three chars - invalid. */ + return 0; +} + +FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ) +{ + FILE * rc; + if ( mode == NULL || filename == NULL || filename[0] == '\0' ) + { + /* Mode or filename invalid */ + return NULL; + } + if ( ( rc = calloc( 1, sizeof( FILE ) ) ) == NULL ) + { + /* no memory for another FILE */ + return NULL; + } + if ( ( rc->status = 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. TODO: Check for unbuffered? */ + if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL goto fail; + /* TODO: Setting mbstate */ + return rc; +fail: + free( rc ); + return NULL; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + TESTCASE( NO_TESTDRIVER ); + return TEST_RESULTS; +} + +#endif