]> pd.if.org Git - pdclib/blobdiff - platform/example/functions/_PDCLIB/open.c
Joined 32 and 64 bit configurations, made platform linking unnecessary.
[pdclib] / platform / example / functions / _PDCLIB / open.c
index 79497266350fb49992c533adb00bd546fe2b8e5e..f661da5bd170ab2be05ec333f1982a25abf3c3e9 100644 (file)
@@ -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;
 }