]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/open.c
Addressed ticket #40 (non-standard errno values).
[pdclib] / platform / example / functions / _PDCLIB / open.c
1 /* $Id$ */
2
3 /* _PDCLIB_open( char const * const, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* This is an example implementation of _PDCLIB_open() fit for use with POSIX
10    kernels.
11 */
12
13 #include <stdio.h>
14
15 #ifndef REGTEST
16 #include <_PDCLIB_glue.h>
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "/usr/include/errno.h"
24
25 int _PDCLIB_open( char const * const filename, unsigned int mode )
26 {
27     /* This is an example implementation of _PDCLIB_open() fit for use with
28        POSIX kernels.
29     */
30     int osmode;
31     switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
32     {
33         case _PDCLIB_FREAD: /* "r" */
34             osmode = O_RDONLY;
35             break;
36         case _PDCLIB_FWRITE: /* "w" */
37             osmode = O_WRONLY | O_CREAT | O_TRUNC;
38             break;
39         case _PDCLIB_FAPPEND: /* "a" */
40             osmode = O_WRONLY | O_APPEND | O_CREAT;
41             break;
42         case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
43             osmode = O_RDWR;
44             break;
45         case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
46             osmode = O_RDWR | O_CREAT | O_TRUNC;
47             break;
48         case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
49             osmode = O_RDWR | O_APPEND | O_CREAT;
50             break;
51         default: /* Invalid mode */
52             return -1;
53     }
54     int rc;
55     if ( osmode & O_CREAT )
56     {
57         rc = open( filename, osmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
58     }
59     else
60     {
61         rc = open( filename, osmode );
62     }
63     if ( rc == -1 )
64     {
65         switch ( errno )
66         {
67             /* See the comments on implementation-defined errno values in
68                <_PDCLIB_config.h>.
69             */
70             case EACCES:
71             case EFAULT:
72             case EINTR:
73             case EISDIR:
74             case ELOOP:
75             case EMFILE:
76             case ENAMETOOLONG:
77             case ENFILE:
78             case ENODEV:
79             case ENOENT:
80             case ENOMEM:
81             case ENOSPC:
82             case ENOTDIR:
83             case EOVERFLOW:
84             case EROFS:
85             case ETXTBSY:
86                 _PDCLIB_errno = _PDCLIB_ERROR;
87                 break;
88             default:
89                 /* This should be something like EUNKNOWN. */
90                 _PDCLIB_errno = _PDCLIB_ERROR;
91                 break;
92         }
93     }
94     return rc;
95 }
96
97 #endif
98
99 #ifdef TEST
100 #include <_PDCLIB_test.h>
101
102 #include <stdlib.h>
103 #include <string.h>
104
105 int main( void )
106 {
107     /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
108        incremented by one on each successful open.
109     */
110     int fh;
111     char buffer[ 10 ];
112     remove( testfile );
113     /* Trying to read non-existent file. */
114     TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
115     /* Writing to file, trying to read from it. */
116     TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
117     TESTCASE( write( fh, "test", 4 ) == 4 );
118     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
119     TESTCASE( read( fh, buffer, 4 ) == -1 );
120     TESTCASE( _PDCLIB_close( fh ) == 0 );
121     /* Reading from file, trying to write to it. */
122     TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
123     TESTCASE( write( fh, "test", 4 ) == -1 );
124     TESTCASE( _PDCLIB_close( fh ) == 0 );
125     /* Appending to file, trying to read from it. */
126     TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
127     TESTCASE( write( fh, "app", 3 ) == 3 );
128     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
129     TESTCASE( read( fh, buffer, 10 ) == -1 );
130     TESTCASE( write( fh, "end", 3 ) == 3 );
131     TESTCASE( _PDCLIB_close( fh ) == 0 );
132     /* Reading and writing from file ("r+"). */
133     TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
134     TESTCASE( read( fh, buffer, 10 ) == 10 );
135     TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
136     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
137     TESTCASE( write( fh, "wedo", 4 ) == 4 );
138     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
139     TESTCASE( read( fh, buffer, 10 ) == 10 );
140     TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
141     TESTCASE( _PDCLIB_close( fh ) == 0 );
142     /* Writing and reading from file ("w+"). */
143     TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
144     TESTCASE( write( fh, "test", 4 ) == 4 );
145     TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
146     TESTCASE( read( fh, buffer, 2 ) == 2 );
147     TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
148     TESTCASE( write( fh, "sie", 3 ) == 3 );
149     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
150     TESTCASE( read( fh, buffer, 6 ) == 6 );
151     TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
152     TESTCASE( _PDCLIB_close( fh ) == 0 );
153     /* Appending and reading from file ("a+"). */
154     TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
155     TESTCASE( write( fh, "baby", 4 ) == 4 );
156     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
157     TESTCASE( read( fh, buffer, 10 ) == 10 );
158     TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
159     TESTCASE( _PDCLIB_close( fh ) == 0 );
160     /* Cleaning up. */
161     TESTCASE( remove( testfile ) == 0 );
162     return TEST_RESULTS;
163 }
164
165 #endif
166