3 /* _PDCLIB_open( char const * const, int )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
9 /* This is an example implementation of _PDCLIB_open() fit for use with POSIX
16 #include <_PDCLIB_glue.h>
18 #include <sys/types.h>
23 #include "/usr/include/errno.h"
25 int _PDCLIB_open( char const * const filename, unsigned int mode )
27 /* This is an example implementation of _PDCLIB_open() fit for use with
29 FIXME: The permissions of newly created files should not be hardcoded
33 switch ( mode & ~_PDCLIB_FBIN )
35 case _PDCLIB_FREAD: /* "r" */
38 case _PDCLIB_FWRITE: /* "w" */
39 osmode = O_WRONLY | O_CREAT | O_TRUNC;
41 case _PDCLIB_FAPPEND: /* "a" */
42 osmode = O_WRONLY | O_APPEND | O_CREAT;
44 case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
47 case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
48 osmode = O_RDWR | O_CREAT | O_TRUNC;
50 case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
51 osmode = O_RDWR | O_APPEND | O_CREAT;
53 default: /* Invalid mode */
57 if ( osmode & O_CREAT )
59 rc = open( filename, osmode, S_IRUSR | S_IWUSR );
63 rc = open( filename, osmode );
85 _PDCLIB_errno = _PDCLIB_EIO;
87 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
96 #include <_PDCLIB_test.h>
103 /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
104 incremented by one on each successful open.
108 remove( "testfile" );
109 /* Trying to read non-existent file. */
110 TESTCASE( _PDCLIB_open( "testfile", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
111 /* Writing to file, trying to read from it. */
112 TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
113 TESTCASE( write( fh, "test", 4 ) == 4 );
114 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
115 TESTCASE( read( fh, buffer, 4 ) == -1 );
116 TESTCASE( _PDCLIB_close( fh ) == 0 );
117 /* Reading from file, trying to write to it. */
118 TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
119 TESTCASE( write( fh, "test", 4 ) == -1 );
120 TESTCASE( _PDCLIB_close( fh ) == 0 );
121 /* Appending to file, trying to read from it. */
122 TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
123 TESTCASE( write( fh, "app", 3 ) == 3 );
124 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
125 TESTCASE( read( fh, buffer, 10 ) == -1 );
126 TESTCASE( write( fh, "end", 3 ) == 3 );
127 TESTCASE( _PDCLIB_close( fh ) == 0 );
128 /* Reading and writing from file ("r+"). */
129 TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
130 TESTCASE( read( fh, buffer, 10 ) == 10 );
131 TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
132 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
133 TESTCASE( write( fh, "wedo", 4 ) == 4 );
134 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
135 TESTCASE( read( fh, buffer, 10 ) == 10 );
136 TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
137 TESTCASE( _PDCLIB_close( fh ) == 0 );
138 /* Writing and reading from file ("w+"). */
139 TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
140 TESTCASE( write( fh, "test", 4 ) == 4 );
141 TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
142 TESTCASE( read( fh, buffer, 2 ) == 2 );
143 TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
144 TESTCASE( write( fh, "sie", 3 ) == 3 );
145 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
146 TESTCASE( read( fh, buffer, 6 ) == 6 );
147 TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
148 TESTCASE( _PDCLIB_close( fh ) == 0 );
149 /* Appending and reading from file ("a+"). */
150 TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
151 TESTCASE( write( fh, "baby", 4 ) == 4 );
152 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
153 TESTCASE( read( fh, buffer, 10 ) == 10 );
154 TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
155 TESTCASE( _PDCLIB_close( fh ) == 0 );
157 TESTCASE( remove( "testfile" ) == 0 );