f0379ba64d986823fa6dc6acf37ccd3daf67570a
[fw/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             case EACCES:
70             case EFAULT:
71             case EINTR:
72             case EISDIR:
73             case ELOOP:
74             case EMFILE:
75             case ENAMETOOLONG:
76             case ENFILE:
77             case ENODEV:
78             case ENOENT:
79             case ENOMEM:
80             case ENOSPC:
81             case ENOTDIR:
82             case EOVERFLOW:
83             case EROFS:
84             case ETXTBSY:
85                 _PDCLIB_errno = _PDCLIB_EIO;
86             default:
87                 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
88         }
89     }
90     return rc;
91 }
92
93 #endif
94
95 #ifdef TEST
96 #include <_PDCLIB_test.h>
97
98 #include <stdlib.h>
99 #include <string.h>
100
101 int main( void )
102 {
103     /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
104        incremented by one on each successful open.
105     */
106     int fh;
107     char buffer[ 10 ];
108     remove( "testfile" );
109     /* Trying to read non-existent file. */
110     TESTCASE( _PDCLIB_open( "testfile", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
111     /* Writing to file, trying to read from it. */
112     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
113     TESTCASE( write( fh, "test", 4 ) == 4 );
114     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
115     TESTCASE( read( fh, buffer, 4 ) == -1 );
116     TESTCASE( _PDCLIB_close( fh ) == 0 );
117     /* Reading from file, trying to write to it. */
118     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
119     TESTCASE( write( fh, "test", 4 ) == -1 );
120     TESTCASE( _PDCLIB_close( fh ) == 0 );
121     /* Appending to file, trying to read from it. */
122     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
123     TESTCASE( write( fh, "app", 3 ) == 3 );
124     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
125     TESTCASE( read( fh, buffer, 10 ) == -1 );
126     TESTCASE( write( fh, "end", 3 ) == 3 );
127     TESTCASE( _PDCLIB_close( fh ) == 0 );
128     /* Reading and writing from file ("r+"). */
129     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
130     TESTCASE( read( fh, buffer, 10 ) == 10 );
131     TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
132     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
133     TESTCASE( write( fh, "wedo", 4 ) == 4 );
134     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
135     TESTCASE( read( fh, buffer, 10 ) == 10 );
136     TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
137     TESTCASE( _PDCLIB_close( fh ) == 0 );
138     /* Writing and reading from file ("w+"). */
139     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
140     TESTCASE( write( fh, "test", 4 ) == 4 );
141     TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
142     TESTCASE( read( fh, buffer, 2 ) == 2 );
143     TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
144     TESTCASE( write( fh, "sie", 3 ) == 3 );
145     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
146     TESTCASE( read( fh, buffer, 6 ) == 6 );
147     TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
148     TESTCASE( _PDCLIB_close( fh ) == 0 );
149     /* Appending and reading from file ("a+"). */
150     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
151     TESTCASE( write( fh, "baby", 4 ) == 4 );
152     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
153     TESTCASE( read( fh, buffer, 10 ) == 10 );
154     TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
155     TESTCASE( _PDCLIB_close( fh ) == 0 );
156     /* Cleaning up. */
157     TESTCASE( remove( "testfile" ) == 0 );
158     return TEST_RESULTS;
159 }
160
161 #endif
162