1 /* _PDCLIB_open( char const * const, int )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
7 /* This is an example implementation of _PDCLIB_open() fit for use with POSIX
15 #include "_PDCLIB_glue.h"
17 #include <sys/types.h>
22 #include "/usr/include/errno.h"
24 int _PDCLIB_open( char const * const filename, unsigned int mode )
26 /* This is an example implementation of _PDCLIB_open() fit for use with
30 switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
32 case _PDCLIB_FREAD: /* "r" */
35 case _PDCLIB_FWRITE: /* "w" */
36 osmode = O_WRONLY | O_CREAT | O_TRUNC;
38 case _PDCLIB_FAPPEND: /* "a" */
39 osmode = O_WRONLY | O_APPEND | O_CREAT;
41 case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
44 case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
45 osmode = O_RDWR | O_CREAT | O_TRUNC;
47 case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
48 osmode = O_RDWR | O_APPEND | O_CREAT;
50 default: /* Invalid mode */
54 if ( osmode & O_CREAT )
56 rc = open( filename, osmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
60 rc = open( filename, osmode );
66 /* See the comments on implementation-defined errno values in
85 _PDCLIB_errno = _PDCLIB_ERROR;
88 /* This should be something like EUNKNOWN. */
89 _PDCLIB_errno = _PDCLIB_ERROR;
100 #include "_PDCLIB_test.h"
108 /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
109 incremented by one on each successful open.
114 /* Trying to read non-existent file. */
115 TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
116 /* Writing to file, trying to read from it. */
117 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
118 TESTCASE( write( fh, "test", 4 ) == 4 );
119 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
120 TESTCASE( read( fh, buffer, 4 ) == -1 );
121 TESTCASE( _PDCLIB_close( fh ) == 0 );
122 /* Reading from file, trying to write to it. */
123 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
124 TESTCASE( write( fh, "test", 4 ) == -1 );
125 TESTCASE( _PDCLIB_close( fh ) == 0 );
126 /* Appending to file, trying to read from it. */
127 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
128 TESTCASE( write( fh, "app", 3 ) == 3 );
129 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
130 TESTCASE( read( fh, buffer, 10 ) == -1 );
131 TESTCASE( write( fh, "end", 3 ) == 3 );
132 TESTCASE( _PDCLIB_close( fh ) == 0 );
133 /* Reading and writing from file ("r+"). */
134 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
135 TESTCASE( read( fh, buffer, 10 ) == 10 );
136 TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
137 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
138 TESTCASE( write( fh, "wedo", 4 ) == 4 );
139 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
140 TESTCASE( read( fh, buffer, 10 ) == 10 );
141 TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
142 TESTCASE( _PDCLIB_close( fh ) == 0 );
143 /* Writing and reading from file ("w+"). */
144 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
145 TESTCASE( write( fh, "test", 4 ) == 4 );
146 TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
147 TESTCASE( read( fh, buffer, 2 ) == 2 );
148 TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
149 TESTCASE( write( fh, "sie", 3 ) == 3 );
150 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
151 TESTCASE( read( fh, buffer, 6 ) == 6 );
152 TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
153 TESTCASE( _PDCLIB_close( fh ) == 0 );
154 /* Appending and reading from file ("a+"). */
155 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
156 TESTCASE( write( fh, "baby", 4 ) == 4 );
157 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
158 TESTCASE( read( fh, buffer, 10 ) == 10 );
159 TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
160 TESTCASE( _PDCLIB_close( fh ) == 0 );
162 TESTCASE( remove( testfile ) == 0 );