]> pd.if.org Git - pdclib/blob - platform/example_cygwin/functions/_PDCLIB/open.c
Addressed ticket #40 (non-standard errno values).
[pdclib] / platform / example_cygwin / 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        FIXME: The permissions of newly created files should not be hardcoded
30        here.
31     */
32     int osmode;
33     switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
34     {
35         case _PDCLIB_FREAD: /* "r" */
36             osmode = O_RDONLY;
37             break;
38         case _PDCLIB_FWRITE: /* "w" */
39             osmode = O_WRONLY | O_CREAT | O_TRUNC;
40             break;
41         case _PDCLIB_FAPPEND: /* "a" */
42             osmode = O_WRONLY | O_APPEND | O_CREAT;
43             break;
44         case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
45             osmode = O_RDWR;
46             break;
47         case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
48             osmode = O_RDWR | O_CREAT | O_TRUNC;
49             break;
50         case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
51             osmode = O_RDWR | O_APPEND | O_CREAT;
52             break;
53         default: /* Invalid mode */
54             return -1;
55     }
56     int rc;
57     if ( osmode & O_CREAT )
58     {
59         rc = open( filename, osmode, S_IRUSR | S_IWUSR );
60     }
61     else
62     {
63         rc = open( filename, osmode );
64     }
65     if ( rc == -1 )
66     {
67         switch ( errno )
68         {
69             /* See the comments on implementation-defined errno values in
70                <_PDCLIB_config.h>.
71             */
72             case EACCES:
73             case EFAULT:
74             case EINTR:
75             case EISDIR:
76             case ELOOP:
77             case EMFILE:
78             case ENAMETOOLONG:
79             case ENFILE:
80             case ENODEV:
81             case ENOENT:
82             case ENOMEM:
83             case ENOSPC:
84             case ENOTDIR:
85             case EOVERFLOW:
86             case EROFS:
87             case ETXTBSY:
88                 _PDCLIB_errno = _PDCLIB_ERROR;
89                 break;
90             default:
91                 /* This should be something like EUNKNOWN. */
92                 _PDCLIB_errno = _PDCLIB_ERROR;
93                 break;
94         }
95     }
96     return rc;
97 }
98
99 #endif
100
101 #ifdef TEST
102 #include <_PDCLIB_test.h>
103
104 #include <stdlib.h>
105 #include <string.h>
106
107 int main( void )
108 {
109     /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
110        incremented by one on each successful open.
111     */
112     int fh;
113     char buffer[ 10 ];
114     remove( testfile );
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 );
162     /* Cleaning up. */
163     TESTCASE( remove( testfile ) == 0 );
164     return TEST_RESULTS;
165 }
166
167 #endif
168