X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffopen.c;h=4d249efc1bc6948204dee36719453b006e8188fb;hb=d02f38605b53cdff5460cc6b9e1b2a80c3a2ba4c;hp=04020d94645a1f52dee6f3bd88da52c0e91f4850;hpb=34893ecc2200dc7017c36a54cb6c5f4c2378b5ec;p=pdclib diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index 04020d9..4d249ef 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -1,8 +1,87 @@ -// ---------------------------------------------------------------------------- -// $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$ */ + +/* Release $Name$ */ + +/* 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 const FILE * _PDCLIB_filelist = NULL; + +static 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: + return -1; + } + for ( size_t i = 1; i < 4; ++i ) + { + switch ( mode[1] ) + { + case '+': + if ( rc & _PDCLIB_FRW ) return -1; + rc |= _PDCLIB_FRW; + break; + case 'b': + if ( rc & _PDCLIB_FBIN ) return -1; + rc |= _PDCLIB_FBIN; + break; + case '\0': + return rc; + default: + return -1; + } + } + return -1; +} + +FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ) +{ + FILE * rc; + if ( mode == NULL || filename == NULL || filename[0] == '\0' ) + { + return NULL; + } + if ( ( rc = calloc( 1, sizeof( FILE ) ) ) == NULL ) return rc; /* no space for another FILE */ + if ( ( rc->status = filemode( mode ) ) == -1 ) goto fail; /* invalid mode given */ + if ( ( rc->handle = _PDCLIB_open( filename, rc->status ) ) == -1 ) goto fail; /* OS "open" failed */ + rc->next = _PDCLIB_filelist; + _PDCLIB_filelist = rc; + /* TODO: Continue here: Set up PDCLib FILE contents */ + return rc; +fail: + free( rc ); + return NULL; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + TESTCASE( NO_TESTDRIVER ); + return TEST_RESULTS; +} + +#endif