X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fposix%2Ffunctions%2Fstdio%2F_PDCLIB_open.c;fp=platform%2Fposix%2Ffunctions%2Fstdio%2F_PDCLIB_open.c;h=9f3cdec90fd9f503b4cc551cbf3a7420fc66b930;hb=9fcf1b27fc705cc131b258478d95ce9de65aabc0;hp=0000000000000000000000000000000000000000;hpb=f9ee8785d3c18603033ffbf7099f2a032186f2f9;p=pdclib.old diff --git a/platform/posix/functions/stdio/_PDCLIB_open.c b/platform/posix/functions/stdio/_PDCLIB_open.c new file mode 100644 index 0000000..9f3cdec --- /dev/null +++ b/platform/posix/functions/stdio/_PDCLIB_open.c @@ -0,0 +1,62 @@ +/* _PDCLIB_open(_PDCLIB_fd_t*, const _PDCLIB_fileops_t**, + char const*, unsigned int) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ +#ifndef REGTEST +#include <_PDCLIB_glue.h> +#include <_PDCLIB_io.h> +#include + +extern const _PDCLIB_fileops_t _PDCLIB_fileops; + +bool _PDCLIB_open( + _PDCLIB_fd_t* fd, const _PDCLIB_fileops_t** ops, + char const * filename, unsigned int mode ) +{ + int osmode; + switch ( mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) + { + case _PDCLIB_FREAD: /* "r" */ + osmode = O_RDONLY; + break; + case _PDCLIB_FWRITE: /* "w" */ + osmode = O_WRONLY | O_CREAT | O_TRUNC; + break; + case _PDCLIB_FAPPEND: /* "a" */ + osmode = O_WRONLY | O_APPEND | O_CREAT; + break; + case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */ + osmode = O_RDWR; + break; + case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */ + osmode = O_RDWR | O_CREAT | O_TRUNC; + break; + case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */ + osmode = O_RDWR | O_APPEND | O_CREAT; + break; + default: /* Invalid mode */ + return -1; + } + + fd->sval = open(filename, osmode, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + if(fd->sval == -1) { + return false; + } + + *ops = &_PDCLIB_fileops; + return true; +} +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif \ No newline at end of file