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
14 #include <_PDCLIB_glue.h>
16 #include <sys/types.h>
21 #include "/usr/include/errno.h"
23 int _PDCLIB_open( char const * const filename, unsigned int mode )
25 /* This is an example implementation of _PDCLIB_open() fit for use with
29 switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
31 case _PDCLIB_FREAD: /* "r" */
34 case _PDCLIB_FWRITE: /* "w" */
35 osmode = O_WRONLY | O_CREAT | O_TRUNC;
37 case _PDCLIB_FAPPEND: /* "a" */
38 osmode = O_WRONLY | O_APPEND | O_CREAT;
40 case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
43 case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
44 osmode = O_RDWR | O_CREAT | O_TRUNC;
46 case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
47 osmode = O_RDWR | O_APPEND | O_CREAT;
49 default: /* Invalid mode */
53 if ( osmode & O_CREAT )
55 rc = open( filename, osmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
59 rc = open( filename, osmode );
65 /* See the comments on implementation-defined errno values in
84 _PDCLIB_errno = _PDCLIB_ERROR;
87 /* This should be something like EUNKNOWN. */
88 _PDCLIB_errno = _PDCLIB_ERROR;
98 #include <_PDCLIB_test.h>
105 /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
106 incremented by one on each successful open.
111 /* Trying to read non-existent file. */
112 TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
113 /* Writing to file, trying to read from it. */
114 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
115 TESTCASE( write( fh, "test", 4 ) == 4 );
116 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
117 TESTCASE( read( fh, buffer, 4 ) == -1 );
118 TESTCASE( _PDCLIB_close( fh ) == 0 );
119 /* Reading from file, trying to write to it. */
120 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
121 TESTCASE( write( fh, "test", 4 ) == -1 );
122 TESTCASE( _PDCLIB_close( fh ) == 0 );
123 /* Appending to file, trying to read from it. */
124 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
125 TESTCASE( write( fh, "app", 3 ) == 3 );
126 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
127 TESTCASE( read( fh, buffer, 10 ) == -1 );
128 TESTCASE( write( fh, "end", 3 ) == 3 );
129 TESTCASE( _PDCLIB_close( fh ) == 0 );
130 /* Reading and writing from file ("r+"). */
131 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
132 TESTCASE( read( fh, buffer, 10 ) == 10 );
133 TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
134 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
135 TESTCASE( write( fh, "wedo", 4 ) == 4 );
136 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
137 TESTCASE( read( fh, buffer, 10 ) == 10 );
138 TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
139 TESTCASE( _PDCLIB_close( fh ) == 0 );
140 /* Writing and reading from file ("w+"). */
141 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
142 TESTCASE( write( fh, "test", 4 ) == 4 );
143 TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
144 TESTCASE( read( fh, buffer, 2 ) == 2 );
145 TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
146 TESTCASE( write( fh, "sie", 3 ) == 3 );
147 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
148 TESTCASE( read( fh, buffer, 6 ) == 6 );
149 TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
150 TESTCASE( _PDCLIB_close( fh ) == 0 );
151 /* Appending and reading from file ("a+"). */
152 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
153 TESTCASE( write( fh, "baby", 4 ) == 4 );
154 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
155 TESTCASE( read( fh, buffer, 10 ) == 10 );
156 TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
157 TESTCASE( _PDCLIB_close( fh ) == 0 );
159 TESTCASE( remove( testfile ) == 0 );