X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2F_PDCLIB%2Fopen.c;h=2098dd360a866b65db4a7f9a5c5a249d4f72020c;hb=ce0e5d8cd76b50f239fb8e95170502b146247b35;hp=f7d7778a9e59085c25891fc6d88bcf7c566ef85f;hpb=6b909e144dfb5ae4c6918336f1c95d2eac9fe947;p=pdclib diff --git a/platform/example/functions/_PDCLIB/open.c b/platform/example/functions/_PDCLIB/open.c index f7d7778..2098dd3 100644 --- a/platform/example/functions/_PDCLIB/open.c +++ b/platform/example/functions/_PDCLIB/open.c @@ -20,17 +20,15 @@ #include #include -_PDCLIB_fd_t _PDCLIB_open( char const * const filename, unsigned int mode ) +#include "/usr/include/errno.h" + +int _PDCLIB_open( char const * const filename, unsigned int mode ) { - /* 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. + /* This is an example implementation of _PDCLIB_open() fit for use with + POSIX kernels. */ int osmode; - switch ( mode & ~_PDCLIB_FBIN ) + switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) { case _PDCLIB_FREAD: /* "r" */ osmode = O_RDONLY; @@ -39,7 +37,7 @@ _PDCLIB_fd_t _PDCLIB_open( char const * const filename, unsigned int mode ) osmode = O_WRONLY | O_CREAT | O_TRUNC; break; case _PDCLIB_FAPPEND: /* "a" */ - osmode = O_APPEND | O_CREAT; + osmode = O_WRONLY | O_APPEND | O_CREAT; break; case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */ osmode = O_RDWR; @@ -53,14 +51,47 @@ _PDCLIB_fd_t _PDCLIB_open( char const * const filename, unsigned int mode ) default: /* Invalid mode */ return -1; } + int rc; if ( osmode & O_CREAT ) { - return open( filename, osmode, S_IRUSR | S_IWUSR ); + rc = open( filename, osmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); } else { - return open( filename, osmode ); + rc = open( filename, osmode ); + } + if ( rc == -1 ) + { + switch ( errno ) + { + /* See the comments on implementation-defined errno values in + <_PDCLIB_config.h>. + */ + case EACCES: + case EFAULT: + case EINTR: + case EISDIR: + case ELOOP: + case EMFILE: + case ENAMETOOLONG: + case ENFILE: + case ENODEV: + case ENOENT: + case ENOMEM: + case ENOSPC: + case ENOTDIR: + case EOVERFLOW: + case EROFS: + case ETXTBSY: + _PDCLIB_errno = _PDCLIB_ERROR; + break; + default: + /* This should be something like EUNKNOWN. */ + _PDCLIB_errno = _PDCLIB_ERROR; + break; + } } + return rc; } #endif @@ -69,20 +100,67 @@ _PDCLIB_fd_t _PDCLIB_open( char const * const filename, unsigned int mode ) #include <_PDCLIB_test.h> #include +#include int main( void ) { /* 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 ); + int fh; + char buffer[ 10 ]; + remove( testfile ); + /* Trying to read non-existent file. */ + TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); + /* Writing to file, trying to read from it. */ + TESTCASE( ( fh = _PDCLIB_open( testfile, _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" ); + TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); + TESTCASE( read( fh, buffer, 4 ) == -1 ); + TESTCASE( _PDCLIB_close( fh ) == 0 ); + /* Reading from file, trying to write to it. */ + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( write( fh, "test", 4 ) == -1 ); + TESTCASE( _PDCLIB_close( fh ) == 0 ); + /* Appending to file, trying to read from it. */ + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( write( fh, "app", 3 ) == 3 ); + TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); + TESTCASE( read( fh, buffer, 10 ) == -1 ); + TESTCASE( write( fh, "end", 3 ) == 3 ); + TESTCASE( _PDCLIB_close( fh ) == 0 ); + /* Reading and writing from file ("r+"). */ + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( read( fh, buffer, 10 ) == 10 ); + TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 ); + TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); + TESTCASE( write( fh, "wedo", 4 ) == 4 ); + TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); + TESTCASE( read( fh, buffer, 10 ) == 10 ); + TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 ); + TESTCASE( _PDCLIB_close( fh ) == 0 ); + /* Writing and reading from file ("w+"). */ + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( write( fh, "test", 4 ) == 4 ); + TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 ); + TESTCASE( read( fh, buffer, 2 ) == 2 ); + TESTCASE( memcmp( buffer, "es", 2 ) == 0 ); + TESTCASE( write( fh, "sie", 3 ) == 3 ); + TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); + TESTCASE( read( fh, buffer, 6 ) == 6 ); + TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 ); + TESTCASE( _PDCLIB_close( fh ) == 0 ); + /* Appending and reading from file ("a+"). */ + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( write( fh, "baby", 4 ) == 4 ); + TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); + TESTCASE( read( fh, buffer, 10 ) == 10 ); + TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 ); + TESTCASE( _PDCLIB_close( fh ) == 0 ); + /* Cleaning up. */ + TESTCASE( remove( testfile ) == 0 ); return TEST_RESULTS; } #endif +