]> pd.if.org Git - pdclib/blob - platform/posix/functions/stdio/_PDCLIB_open.c
PDCLib includes with quotes, not <>.
[pdclib] / platform / posix / functions / stdio / _PDCLIB_open.c
1 /* _PDCLIB_open(_PDCLIB_fd_t*, const _PDCLIB_fileops_t**,
2                 char const*, unsigned int)
3
4    This file is part of the Public Domain C Library (PDCLib).
5    Permission is granted to use, modify, and / or redistribute at will.
6 */
7 #ifndef REGTEST
8 #include "_PDCLIB_glue.h"
9 #include "_PDCLIB_io.h"
10 #include <fcntl.h>
11
12
13 extern const _PDCLIB_fileops_t _PDCLIB_fileops;
14
15 bool _PDCLIB_open(
16    _PDCLIB_fd_t* fd, const _PDCLIB_fileops_t** ops,
17    char const * filename, unsigned int mode )
18 {
19     int osmode;
20     switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) )
21     {
22         case _PDCLIB_FREAD: /* "r" */
23             osmode = O_RDONLY;
24             break;
25         case _PDCLIB_FWRITE: /* "w" */
26             osmode = O_WRONLY | O_CREAT | O_TRUNC;
27             break;
28         case _PDCLIB_FAPPEND: /* "a" */
29             osmode = O_WRONLY | O_APPEND | O_CREAT;
30             break;
31         case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
32             osmode = O_RDWR;
33             break;
34         case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
35             osmode = O_RDWR | O_CREAT | O_TRUNC;
36             break;
37         case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
38             osmode = O_RDWR | O_APPEND | O_CREAT;
39             break;
40         default: /* Invalid mode */
41             return -1;
42     }
43
44     fd->sval = open(filename, osmode, 0664);
45     if(fd->sval == -1) {
46         return false;
47     }
48
49     *ops = &_PDCLIB_fileops;
50     return true;
51 }
52 #endif
53
54 #ifdef TEST
55 #include "_PDCLIB_test.h"
56
57 int main( void )
58 {
59     return TEST_RESULTS;
60 }
61
62 #endif