X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2F_PDCLIB%2Fopen.c;h=f661da5bd170ab2be05ec333f1982a25abf3c3e9;hb=2b793d7f6e3a8e37229e761ef4c92961bd0f686a;hp=79497266350fb49992c533adb00bd546fe2b8e5e;hpb=393020b6e48719d27699dea6b29e53025bbd5123;p=pdclib diff --git a/platform/example/functions/_PDCLIB/open.c b/platform/example/functions/_PDCLIB/open.c index 7949726..f661da5 100644 --- a/platform/example/functions/_PDCLIB/open.c +++ b/platform/example/functions/_PDCLIB/open.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* _PDCLIB_open( char const * const, int ) This file is part of the Public Domain C Library (PDCLib). @@ -26,11 +24,9 @@ int _PDCLIB_open( char const * const filename, unsigned int mode ) { /* This is an example implementation of _PDCLIB_open() fit for use with POSIX kernels. - FIXME: The permissions of newly created files should not be hardcoded - here. */ int osmode; - switch ( mode & ~_PDCLIB_FBIN ) + switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) { case _PDCLIB_FREAD: /* "r" */ osmode = O_RDONLY; @@ -56,7 +52,7 @@ int _PDCLIB_open( char const * const filename, unsigned int mode ) int rc; if ( osmode & O_CREAT ) { - rc = open( filename, osmode, S_IRUSR | S_IWUSR ); + rc = open( filename, osmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); } else { @@ -66,6 +62,9 @@ int _PDCLIB_open( char const * const filename, unsigned int mode ) { switch ( errno ) { + /* See the comments on implementation-defined errno values in + <_PDCLIB_config.h>. + */ case EACCES: case EFAULT: case EINTR: @@ -82,9 +81,12 @@ int _PDCLIB_open( char const * const filename, unsigned int mode ) case EOVERFLOW: case EROFS: case ETXTBSY: - _PDCLIB_errno = _PDCLIB_EIO; + _PDCLIB_errno = _PDCLIB_ERROR; + break; default: - _PDCLIB_errno = _PDCLIB_EUNKNOWN; + /* This should be something like EUNKNOWN. */ + _PDCLIB_errno = _PDCLIB_ERROR; + break; } } return rc; @@ -100,33 +102,34 @@ int _PDCLIB_open( char const * const filename, unsigned int mode ) int main( void ) { +#ifndef REGTEST /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being incremented by one on each successful open. */ int fh; char buffer[ 10 ]; - remove( "testfile" ); + remove( testfile ); /* Trying to read non-existent file. */ - TESTCASE( _PDCLIB_open( "testfile", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); + 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( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); 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( ( 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( ( 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( ( 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 ); @@ -136,7 +139,7 @@ int main( void ) 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( ( 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 ); @@ -147,14 +150,15 @@ int main( void ) 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( ( 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 ); + TESTCASE( remove( testfile ) == 0 ); +#endif return TEST_RESULTS; }