]> pd.if.org Git - pdclib/blobdiff - functions/stdio/fopen.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / fopen.c
index ad8d8765716a69752b27c15a911a08d0e9aca4d9..3d9f95ab1a205917d4ec2b0424b6866a248bc99d 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /* fopen( const char *, const char * )
 
    This file is part of the Public Domain C Library (PDCLib).
 #include <stdlib.h>
 
 #ifndef REGTEST
-#include <_PDCLIB_glue.h>
-
-static FILE * _PDCLIB_filelist = NULL;
+#include "_PDCLIB_io.h"
+#include "_PDCLIB_glue.h"
+#include <string.h>
+#include <errno.h>
 
-/* Helper function that parses the C-style mode string passed to fopen() into
-   the PDCLib flags.FREAD, FWRITE, FAPPEND, FRW (read-write) and FBIN (
-   mode).
-*/
-static unsigned int filemode( char const * const mode )
-{
-    int rc = 0;
-    switch ( mode[0] )
-    {
-        case 'r':
-            rc |= _PDCLIB_FREAD;
-            break;
-        case 'w':
-            rc |= _PDCLIB_FWRITE;
-            break;
-        case 'a':
-            rc |= _PDCLIB_FAPPEND;
-            break;
-        default:
-            /* Other than read, write, or append - invalid */
-            return 0;
-    }
-    for ( size_t i = 1; i < 4; ++i )
-    {
-        switch ( mode[1] )
-        {
-            case '+':
-                if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */
-                rc |= _PDCLIB_FRW;
-                break;
-            case 'b':
-                if ( rc & _PDCLIB_FBIN ) return 0; /* Duplicates are invalid */
-                rc |= _PDCLIB_FBIN;
-                break;
-            case '\0':
-                /* End of mode */
-                return rc;
-            default:
-                /* Other than read/write or binary - invalid. */
-                return 0;
-        }
-    }
-    /* Longer than three chars - invalid. */
-    return 0;
-}
+extern FILE * _PDCLIB_filelist;
 
-FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode )
+FILE * fopen( const char * _PDCLIB_restrict filename, 
+              const char * _PDCLIB_restrict mode )
 {
-    FILE * rc;
-    if ( mode == NULL || filename == NULL || filename[0] == '\0' )
-    {
-        /* Mode or filename invalid */
+    int imode = _PDCLIB_filemode( mode );
+    
+    if( imode == 0 || filename == NULL )
         return NULL;
-    }
-    if ( ( rc = calloc( 1, sizeof( FILE ) ) ) == NULL )
-    {
-        /* no memory for another FILE */
+
+    _PDCLIB_fd_t              fd;
+    const _PDCLIB_fileops_t * ops;
+    if(!_PDCLIB_open( &fd, &ops, filename, imode )) {
         return NULL;
     }
-    if ( ( rc->status = filemode( mode ) ) == 0 ) goto fail; /* invalid mode */
-    rc->handle = _PDCLIB_open( filename, rc->status );
-    if ( rc->handle == _PDCLIB_NOHANDLE ) goto fail; /* OS open() failed */
 
-    /* Adding to list of open files */
-    rc->next = _PDCLIB_filelist;
-    _PDCLIB_filelist = rc;
-    /* Setting buffer. TODO: Check for unbuffered? */
-    if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail;
-    /* TODO: Setting mbstate */
-    return rc;
-fail:
-    free( rc );
-    return NULL;
+    FILE * f = _PDCLIB_fvopen( fd, ops, imode, filename );
+    if(!f) {
+        int saveErrno = errno;
+        ops->close(fd);
+        errno = saveErrno;
+    }
+    return f;
 }
 
 #endif
 
 #ifdef TEST
-#include <_PDCLIB_test.h>
+#include "_PDCLIB_test.h"
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    /* Some of the tests are not executed for regression tests, as the libc on
+       my system is at once less forgiving (segfaults on mode NULL) and more
+       forgiving (accepts undefined modes).
+    */
+    FILE * fh;
+    remove( testfile );
+    TESTCASE_NOREG( fopen( NULL, NULL ) == NULL );
+    TESTCASE( fopen( NULL, "w" ) == NULL );
+    TESTCASE_NOREG( fopen( "", NULL ) == NULL );
+    TESTCASE( fopen( "", "w" ) == NULL );
+    TESTCASE( fopen( "foo", "" ) == NULL );
+    TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */
+    TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */
+    TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( remove( testfile ) == 0 );
     return TEST_RESULTS;
 }