X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2F_PDCLIB%2Fopen.c;h=133b729d16cd9562dabc0669aed3c4c08118e707;hb=22dbeae441f3f28808ebe124dd20fe6ccfe8e64e;hp=fc82485aec2d730b3505ee6e3e655e1c83faa11b;hpb=f93d55176e4db9edfb1840054169003bcca4d1fb;p=pdclib diff --git a/platform/example/functions/_PDCLIB/open.c b/platform/example/functions/_PDCLIB/open.c index fc82485..133b729 100644 --- a/platform/example/functions/_PDCLIB/open.c +++ b/platform/example/functions/_PDCLIB/open.c @@ -20,15 +20,15 @@ #include #include +#include "/usr/include/errno.h" + 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; @@ -51,14 +51,41 @@ int _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 ) + { + 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_EIO; + default: + _PDCLIB_errno = _PDCLIB_EUNKNOWN; + } } + return rc; } #endif @@ -69,8 +96,6 @@ int _PDCLIB_open( char const * const filename, unsigned int mode ) #include #include -#include - int main( void ) { /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being @@ -78,6 +103,7 @@ int main( void ) */ 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. */ @@ -126,7 +152,7 @@ int main( void ) TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Cleaning up. */ - system( "rm testfile" ); + TESTCASE( remove( "testfile" ) == 0 ); return TEST_RESULTS; }