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_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
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 );
69 /* See the comments on implementation-defined errno values in
88 _PDCLIB_errno = _PDCLIB_ERROR;
91 /* This should be something like EUNKNOWN. */
92 _PDCLIB_errno = _PDCLIB_ERROR;
102 #include <_PDCLIB_test.h>
109 /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
110 incremented by one on each successful open.
115 /* Trying to read non-existent file. */
116 TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
117 /* Writing to file, trying to read from it. */
118 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
119 TESTCASE( write( fh, "test", 4 ) == 4 );
120 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
121 TESTCASE( read( fh, buffer, 4 ) == -1 );
122 TESTCASE( _PDCLIB_close( fh ) == 0 );
123 /* Reading from file, trying to write to it. */
124 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
125 TESTCASE( write( fh, "test", 4 ) == -1 );
126 TESTCASE( _PDCLIB_close( fh ) == 0 );
127 /* Appending to file, trying to read from it. */
128 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
129 TESTCASE( write( fh, "app", 3 ) == 3 );
130 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
131 TESTCASE( read( fh, buffer, 10 ) == -1 );
132 TESTCASE( write( fh, "end", 3 ) == 3 );
133 TESTCASE( _PDCLIB_close( fh ) == 0 );
134 /* Reading and writing from file ("r+"). */
135 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
136 TESTCASE( read( fh, buffer, 10 ) == 10 );
137 TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
138 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
139 TESTCASE( write( fh, "wedo", 4 ) == 4 );
140 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
141 TESTCASE( read( fh, buffer, 10 ) == 10 );
142 TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
143 TESTCASE( _PDCLIB_close( fh ) == 0 );
144 /* Writing and reading from file ("w+"). */
145 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
146 TESTCASE( write( fh, "test", 4 ) == 4 );
147 TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
148 TESTCASE( read( fh, buffer, 2 ) == 2 );
149 TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
150 TESTCASE( write( fh, "sie", 3 ) == 3 );
151 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
152 TESTCASE( read( fh, buffer, 6 ) == 6 );
153 TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
154 TESTCASE( _PDCLIB_close( fh ) == 0 );
155 /* Appending and reading from file ("a+"). */
156 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
157 TESTCASE( write( fh, "baby", 4 ) == 4 );
158 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
159 TESTCASE( read( fh, buffer, 10 ) == 10 );
160 TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
161 TESTCASE( _PDCLIB_close( fh ) == 0 );
163 TESTCASE( remove( testfile ) == 0 );