]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/open.c
fc82485aec2d730b3505ee6e3e655e1c83faa11b
[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 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        FIXME: The permissions of newly created files should not be hardcoded
28        here.
29     */
30     int osmode;
31     switch ( mode & ~_PDCLIB_FBIN )
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     if ( osmode & O_CREAT )
55     {
56         return open( filename, osmode, S_IRUSR | S_IWUSR );
57     }
58     else
59     {
60         return open( filename, osmode );
61     }
62 }
63
64 #endif
65
66 #ifdef TEST
67 #include <_PDCLIB_test.h>
68
69 #include <stdlib.h>
70 #include <string.h>
71
72 #include <errno.h>
73
74 int main( void )
75 {
76     /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being
77        incremented by one on each successful open.
78     */
79     int fh;
80     char buffer[ 10 ];
81     /* Trying to read non-existent file. */
82     TESTCASE( _PDCLIB_open( "testfile", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE );
83     /* Writing to file, trying to read from it. */
84     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE );
85     TESTCASE( write( fh, "test", 4 ) == 4 );
86     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
87     TESTCASE( read( fh, buffer, 4 ) == -1 );
88     TESTCASE( _PDCLIB_close( fh ) == 0 );
89     /* Reading from file, trying to write to it. */
90     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE );
91     TESTCASE( write( fh, "test", 4 ) == -1 );
92     TESTCASE( _PDCLIB_close( fh ) == 0 );
93     /* Appending to file, trying to read from it. */
94     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE );
95     TESTCASE( write( fh, "app", 3 ) == 3 );
96     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
97     TESTCASE( read( fh, buffer, 10 ) == -1 );
98     TESTCASE( write( fh, "end", 3 ) == 3 );
99     TESTCASE( _PDCLIB_close( fh ) == 0 );
100     /* Reading and writing from file ("r+"). */
101     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
102     TESTCASE( read( fh, buffer, 10 ) == 10 );
103     TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 );
104     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
105     TESTCASE( write( fh, "wedo", 4 ) == 4 );
106     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
107     TESTCASE( read( fh, buffer, 10 ) == 10 );
108     TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 );
109     TESTCASE( _PDCLIB_close( fh ) == 0 );
110     /* Writing and reading from file ("w+"). */
111     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
112     TESTCASE( write( fh, "test", 4 ) == 4 );
113     TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 );
114     TESTCASE( read( fh, buffer, 2 ) == 2 );
115     TESTCASE( memcmp( buffer, "es", 2 ) == 0 );
116     TESTCASE( write( fh, "sie", 3 ) == 3 );
117     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
118     TESTCASE( read( fh, buffer, 6 ) == 6 );
119     TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 );
120     TESTCASE( _PDCLIB_close( fh ) == 0 );
121     /* Appending and reading from file ("a+"). */
122     TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE );
123     TESTCASE( write( fh, "baby", 4 ) == 4 );
124     TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 );
125     TESTCASE( read( fh, buffer, 10 ) == 10 );
126     TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 );
127     TESTCASE( _PDCLIB_close( fh ) == 0 );
128     /* Cleaning up. */
129     system( "rm testfile" );
130     return TEST_RESULTS;
131 }
132
133 #endif
134