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>
106 /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
107 incremented by one on each successful open.
112 /* Trying to read non-existent file. */
113 TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
114 /* Writing to file, trying to read from it. */
115 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
116 TESTCASE( write( fh, "test", 4 ) == 4 );
117 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
118 TESTCASE( read( fh, buffer, 4 ) == -1 );
119 TESTCASE( _PDCLIB_close( fh ) == 0 );
120 /* Reading from file, trying to write to it. */
121 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
122 TESTCASE( write( fh, "test", 4 ) == -1 );
123 TESTCASE( _PDCLIB_close( fh ) == 0 );
124 /* Appending to file, trying to read from it. */
125 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
126 TESTCASE( write( fh, "app", 3 ) == 3 );
127 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
128 TESTCASE( read( fh, buffer, 10 ) == -1 );
129 TESTCASE( write( fh, "end", 3 ) == 3 );
130 TESTCASE( _PDCLIB_close( fh ) == 0 );
131 /* Reading and writing from file ("r+"). */
132 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
133 TESTCASE( read( fh, buffer, 10 ) == 10 );
134 TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
135 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
136 TESTCASE( write( fh, "wedo", 4 ) == 4 );
137 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
138 TESTCASE( read( fh, buffer, 10 ) == 10 );
139 TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
140 TESTCASE( _PDCLIB_close( fh ) == 0 );
141 /* Writing and reading from file ("w+"). */
142 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
143 TESTCASE( write( fh, "test", 4 ) == 4 );
144 TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
145 TESTCASE( read( fh, buffer, 2 ) == 2 );
146 TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
147 TESTCASE( write( fh, "sie", 3 ) == 3 );
148 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
149 TESTCASE( read( fh, buffer, 6 ) == 6 );
150 TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
151 TESTCASE( _PDCLIB_close( fh ) == 0 );
152 /* Appending and reading from file ("a+"). */
153 TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
154 TESTCASE( write( fh, "baby", 4 ) == 4 );
155 TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
156 TESTCASE( read( fh, buffer, 10 ) == 10 );
157 TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
158 TESTCASE( _PDCLIB_close( fh ) == 0 );
160 TESTCASE( remove( testfile ) == 0 );