X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2F_PDCLIB%2Fopen.c;h=79497266350fb49992c533adb00bd546fe2b8e5e;hb=393020b6e48719d27699dea6b29e53025bbd5123;hp=5feec6e828a89a3c8ccb0e2ff8c14492b2cdf22f;hpb=9b1e6c0a721f9358d9bbc76410e719a878e27edb;p=pdclib diff --git a/platform/example/functions/_PDCLIB/open.c b/platform/example/functions/_PDCLIB/open.c index 5feec6e..7949726 100644 --- a/platform/example/functions/_PDCLIB/open.c +++ b/platform/example/functions/_PDCLIB/open.c @@ -20,14 +20,14 @@ #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. + FIXME: The permissions of newly created files should not be hardcoded + here. */ int osmode; switch ( mode & ~_PDCLIB_FBIN ) @@ -53,14 +53,41 @@ _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 ); } 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 @@ -71,15 +98,14 @@ _PDCLIB_fd_t _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 incremented by one on each successful open. */ - _PDCLIB_fd_t fh; + 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. */ @@ -128,8 +154,9 @@ 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; } #endif +