]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/open.c
Joined 32 and 64 bit configurations, made platform linking unnecessary.
[pdclib] / platform / example / functions / _PDCLIB / open.c
1 /* _PDCLIB_open( char const * const, int )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 /* This is an example implementation of _PDCLIB_open() fit for use with POSIX
8    kernels.
9 */
10
11 #include <stdio.h>
12
13 #ifndef REGTEST
14 #include <_PDCLIB_glue.h>
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #include "/usr/include/errno.h"
22
23 int _PDCLIB_open( char const * const filename, unsigned int mode )
24 {
25     /* This is an example implementation of _PDCLIB_open() fit for use with
26        POSIX kernels.
27     */
28     int osmode;
29     switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
30     {
31         case _PDCLIB_FREAD: /* "r" */
32             osmode = O_RDONLY;
33             break;
34         case _PDCLIB_FWRITE: /* "w" */
35             osmode = O_WRONLY | O_CREAT | O_TRUNC;
36             break;
37         case _PDCLIB_FAPPEND: /* "a" */
38             osmode = O_WRONLY | O_APPEND | O_CREAT;
39             break;
40         case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
41             osmode = O_RDWR;
42             break;
43         case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
44             osmode = O_RDWR | O_CREAT | O_TRUNC;
45             break;
46         case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
47             osmode = O_RDWR | O_APPEND | O_CREAT;
48             break;
49         default: /* Invalid mode */
50             return -1;
51     }
52     int rc;
53     if ( osmode & O_CREAT )
54     {
55         rc = open( filename, osmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
56     }
57     else
58     {
59         rc = open( filename, osmode );
60     }
61     if ( rc == -1 )
62     {
63         switch ( errno )
64         {
65             /* See the comments on implementation-defined errno values in
66                <_PDCLIB_config.h>.
67             */
68             case EACCES:
69             case EFAULT:
70             case EINTR:
71             case EISDIR:
72             case ELOOP:
73             case EMFILE:
74             case ENAMETOOLONG:
75             case ENFILE:
76             case ENODEV:
77             case ENOENT:
78             case ENOMEM:
79             case ENOSPC:
80             case ENOTDIR:
81             case EOVERFLOW:
82             case EROFS:
83             case ETXTBSY:
84                 _PDCLIB_errno = _PDCLIB_ERROR;
85                 break;
86             default:
87                 /* This should be something like EUNKNOWN. */
88                 _PDCLIB_errno = _PDCLIB_ERROR;
89                 break;
90         }
91     }
92     return rc;
93 }
94
95 #endif
96
97 #ifdef TEST
98 #include <_PDCLIB_test.h>
99
100 #include <stdlib.h>
101 #include <string.h>
102
103 int main( void )
104 {
105 #ifndef REGTEST
106     /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
107        incremented by one on each successful open.
108     */
109     int fh;
110     char buffer[ 10 ];
111     remove( testfile );
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 );
159     /* Cleaning up. */
160     TESTCASE( remove( testfile ) == 0 );
161 #endif
162     return TEST_RESULTS;
163 }
164
165 #endif
166