From 6b909e144dfb5ae4c6918336f1c95d2eac9fe947 Mon Sep 17 00:00:00 2001 From: solar Date: Wed, 7 Jun 2006 05:36:39 +0000 Subject: [PATCH] First working testdriver. --- internals/_PDCLIB_int.h | 10 ++-- platform/example/functions/_PDCLIB/open.c | 59 ++++++++++++++++++++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index b32b14a..1cc8c01 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -255,11 +255,11 @@ typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t; /* -------------------------------------------------------------------------- */ /* Flags for representing mode (see fopen()). */ -#define _PDCLIB_FREAD 1 -#define _PDCLIB_FWRITE 2 -#define _PDCLIB_FAPPEND 4 -#define _PDCLIB_FRW 8 -#define _PDCLIB_FBIN 16 +#define _PDCLIB_FREAD 1u +#define _PDCLIB_FWRITE 2u +#define _PDCLIB_FAPPEND 4u +#define _PDCLIB_FRW 8u +#define _PDCLIB_FBIN 16u struct _PDCLIB_file_t { diff --git a/platform/example/functions/_PDCLIB/open.c b/platform/example/functions/_PDCLIB/open.c index b0b26cb..f7d7778 100644 --- a/platform/example/functions/_PDCLIB/open.c +++ b/platform/example/functions/_PDCLIB/open.c @@ -22,13 +22,45 @@ _PDCLIB_fd_t _PDCLIB_open( char const * const filename, unsigned int mode ) { - int osmode = 0; - if ( mode & _PDCLIB_FRW ) osmode |= O_RDWR; - if ( mode & ( _PDCLIB_FWRITE | _PDCLIB_FAPPEND ) ) osmode |= O_CREAT; - if ( mode & _PDCLIB_FWRITE ) osmode |= O_TRUNC; - if ( mode & _PDCLIB_FAPPEND ) osmode |= O_APPEND; - if ( ( mode & _PDCLIB_FREAD ) && ! ( mode & _PDCLIB_FRW ) ) osmode |= O_RDONLY; - return open( filename, osmode ); + /* FIXME: THIS IS NOT TO BE USED OUT-OF-THE-BOX. + It is a proof-of-concept implementation. E.g. a stream may only be fully + buffered IF IT CAN BE DETERMINED NOT TO REFER TO AN INTERACTIVE DEVICE. + This logic is not represented here, as this is the EXAMPLE platform, and + actual platform overlays may differ widely. Another point is the value + for permissions being hardcoded to 0664 for file creations. + */ + int osmode; + switch ( mode & ~_PDCLIB_FBIN ) + { + case _PDCLIB_FREAD: /* "r" */ + osmode = O_RDONLY; + break; + case _PDCLIB_FWRITE: /* "w" */ + osmode = O_WRONLY | O_CREAT | O_TRUNC; + break; + case _PDCLIB_FAPPEND: /* "a" */ + osmode = O_APPEND | O_CREAT; + break; + case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */ + osmode = O_RDWR; + break; + case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */ + osmode = O_RDWR | O_CREAT | O_TRUNC; + break; + case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */ + osmode = O_RDWR | O_APPEND | O_CREAT; + break; + default: /* Invalid mode */ + return -1; + } + if ( osmode & O_CREAT ) + { + return open( filename, osmode, S_IRUSR | S_IWUSR ); + } + else + { + return open( filename, osmode ); + } } #endif @@ -36,9 +68,20 @@ _PDCLIB_fd_t _PDCLIB_open( char const * const filename, unsigned int mode ) #ifdef TEST #include <_PDCLIB_test.h> +#include + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being + incremented by one on each successful open. + */ + _PDCLIB_fd_t fh; + TESTCASE( _PDCLIB_open( "testfile2", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( "testfile1", _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( write( fh, "test", 4 ) == 4 ); + TESTCASE( close( fh ) == 0 ); + TESTCASE( _PDCLIB_open( "testfile1", _PDCLIB_FREAD ) != _PDCLIB_NOHANDLE ); + system( "rm testfile1" ); return TEST_RESULTS; } -- 2.40.0