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