X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample_cygwin%2Ffunctions%2F_PDCLIB%2Fopen.c;fp=platform%2Fexample_cygwin%2Ffunctions%2F_PDCLIB%2Fopen.c;h=79497266350fb49992c533adb00bd546fe2b8e5e;hb=0c316ee3b4dc0712797877f5dc0f4a789646154d;hp=0000000000000000000000000000000000000000;hpb=ffa73a4a2673fece7cd63c471476db6bc4aef9e6;p=pdclib diff --git a/platform/example_cygwin/functions/_PDCLIB/open.c b/platform/example_cygwin/functions/_PDCLIB/open.c new file mode 100644 index 0000000..7949726 --- /dev/null +++ b/platform/example_cygwin/functions/_PDCLIB/open.c @@ -0,0 +1,162 @@ +/* $Id$ */ + +/* _PDCLIB_open( char const * const, int ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +/* This is an example implementation of _PDCLIB_open() fit for use with POSIX + kernels. +*/ + +#include + +#ifndef REGTEST +#include <_PDCLIB_glue.h> + +#include +#include +#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 ) + { + 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_WRONLY | 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; + } + int rc; + if ( osmode & O_CREAT ) + { + rc = open( filename, osmode, S_IRUSR | S_IWUSR ); + } + else + { + 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 + +#ifdef TEST +#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. + */ + 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( 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 +