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