]> pd.if.org Git - pdclib/blobdiff - platform/posix/functions/stdio/_PDCLIB_open.c
Add an up to date POSIX port (works on Mac OS X). This is fully functional except...
[pdclib] / platform / posix / functions / stdio / _PDCLIB_open.c
diff --git a/platform/posix/functions/stdio/_PDCLIB_open.c b/platform/posix/functions/stdio/_PDCLIB_open.c
new file mode 100644 (file)
index 0000000..9f3cdec
--- /dev/null
@@ -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 <fcntl.h>
+
+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