Index: /trunk/kStuff/kRdr/kRdrFile.cpp
===================================================================
--- /trunk/kStuff/kRdr/kRdrFile.cpp	(revision 3594)
+++ /trunk/kStuff/kRdr/kRdrFile.cpp	(revision 3595)
@@ -32,6 +32,20 @@
 #include <k/kHlpAlloc.h>
 #include <k/kHlpString.h>
-
-#if K_OS == K_OS_OS2
+#include <k/kErrors.h>
+
+#if K_OS == K_OS_DARWIN
+# include <sys/fcntl.h>
+# include <sys/mman.h>
+# include <unistd.h>
+extern int kHlpSys_open(const char *filename, int flags, int mode); /* negated errno */
+extern int kHlpSys_close(int fd);
+extern KSSIZE kHlpSys_read(int fd, void *buf, size_t len); /* negated errno */
+extern KI64 kHlpSys_lseek(int fd, int whench, KI64); /* negated errno */
+extern void *kHlpSys_mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
+extern int kHlpSys_mprotect(void *addr, size_t len, int prot);
+extern int kHlpSys_munmap(void *addr, size_t len);
+
+
+#elif K_OS == K_OS_OS2
 # define INCL_ERRORS
 # define INCL_BASE
@@ -188,5 +202,11 @@
     KRDR                Core;
     /** The file handle. */
-#if K_OS == K_OS_OS2
+#if K_OS == K_OS_DARWIN \
+ || K_OS == K_OS_LINUX \
+ || K_OS == K_OS_NETBSD \
+ || K_OS == K_OS_OPENBSD \
+ || K_OS == K_OS_SOLARIS
+    int                 File;
+#elif K_OS == K_OS_OS2
     HFILE               File;
 #elif K_OS == K_OS_WINDOWS
@@ -373,5 +393,5 @@
 
 /** @copydoc KRDROPS::pfnProtect */
-static int      krdrFileProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fUnprotectOrProtect)
+static int krdrFileProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fUnprotectOrProtect)
 {
     PKRDRFILE        pRdrFile = (PKRDRFILE)pRdr;
@@ -556,5 +576,8 @@
     pPrep->cb = (pPrep->cb + (cbPage - 1)) & ~(cbPage- 1);
 
-#if K_OS == K_OS_WINDOWS
+#if K_OS == K_OS_DARWIN
+    /** @todo */
+
+#elif K_OS == K_OS_WINDOWS
     /*
      * The NT memory mapped file API sucks in a lot of ways. Unless you're actually
@@ -803,5 +826,8 @@
 static KSIZE   krdrFilePageSize(PKRDR pRdr)
 {
-#if K_OS == K_OS_OS2
+#if K_OS == K_OS_DARWIN
+    return 0x1000; /** @todo find some header somewhere... */
+
+#elif K_OS == K_OS_OS2
     /* The page size on OS/2 wont change anytime soon. :-) */
     return 0x1000;
@@ -829,5 +855,7 @@
 {
     PKRDRFILE pRdrFile = (PKRDRFILE)pRdr;
-#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
+#if K_OS == K_OS_DARWIN \
+ || K_OS == K_OS_OS2 \
+ || K_OS == K_OS_WINDOWS
     return (KIPTR)pRdrFile->File;
 #else
@@ -847,5 +875,11 @@
     if (pRdrFile->off == -1)
     {
-#if K_OS == K_OS_OS2
+#if K_OS == K_OS_DARWIN
+
+        pRdrFile->off = kHlpSys_lseek(pRdrFile->File, SEEK_CUR, 0);
+        if (pRdrFile->off < 0)
+            pRdrFile->off = -1;
+
+#elif K_OS == K_OS_OS2
         ULONG ulNew;
         APIRET rc = DosSetFilePtr(pRdrFile->File, 0, FILE_CURRENT, &ulNew);
@@ -889,9 +923,5 @@
     /* check for underflow */
     if (pRdrFile->cMappings <= 0)
-#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
-        return ERROR_INVALID_PARAMETER;
-#else
-# error "port me"
-#endif
+        return KERR_INVALID_PARAMETER;
 
     /* decrement usage counter, free mapping if no longer in use. */
@@ -920,13 +950,9 @@
         KSIZE cb = (KSIZE)cbFile;
         if (cb != cbFile)
-            return ERROR_NOT_ENOUGH_MEMORY;
+            return KERR_NO_MEMORY;
 
         pRdrFile->pvMapping = kHlpAlloc(cb);
         if (!pRdrFile->pvMapping)
-#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
-            return ERROR_NOT_ENOUGH_MEMORY;
-#else
-# error "port me"
-#endif
+            return KERR_NO_MEMORY;
         rc = pRdrFile->Core.pOps->pfnRead(pRdr, pRdrFile->pvMapping, cb, 0);
         if (rc)
@@ -955,5 +981,14 @@
     if (pRdrFile->off != off)
     {
-#if K_OS == K_OS_OS2
+#if K_OS == K_OS_DARWIN
+        pRdrFile->off = kHlpSys_lseek(pRdrFile->File, SEEK_SET, off);
+        if (pRdrFile->off < 0)
+        {
+            int rc = (int)-pRdrFile->off;
+            pRdrFile->off = -1;
+            return -rc;
+        }
+
+#elif K_OS == K_OS_OS2
         ULONG ulNew;
         APIRET rc;
@@ -977,5 +1012,5 @@
             int rc = GetLastError();
             if (!rc)
-                rc = ERROR_GEN_FAILURE;
+                rc = KERR_GENERAL_FAILURE;
             pRdrFile->off = -1;
             return rc;
@@ -990,5 +1025,19 @@
      * Do the read.
      */
-#if K_OS == K_OS_OS2
+#if K_OS == K_OS_DARWIN
+    {
+    KSSIZE cbRead;
+
+    cbRead = kHlpSys_read(pRdrFile->File, pvBuf, cb);
+    if (cbRead != cb)
+    {
+        pRdrFile->off = -1;
+        if (cbRead < 0)
+            return -cbRead;
+        return KERR_GENERAL_FAILURE;
+    }
+    }
+
+#elif K_OS == K_OS_OS2
     {
     ULONG cbRead = 0;
@@ -1002,5 +1051,5 @@
     {
         pRdrFile->off = -1;
-        return ERROR_GEN_FAILURE;
+        return KERR_GENERAL_FAILURE;
     }
     }
@@ -1013,5 +1062,5 @@
         int rc = GetLastError();
         if (!rc)
-            rc = ERROR_GEN_FAILURE;
+            rc = KERR_GENERAL_FAILURE;
         pRdrFile->off = -1;
         return rc;
@@ -1020,5 +1069,5 @@
     {
         pRdrFile->off = -1;
-        return ERROR_GEN_FAILURE;
+        return KERR_GENERAL_FAILURE;
     }
     }
@@ -1037,6 +1086,10 @@
 {
     PKRDRFILE    pRdrFile = (PKRDRFILE)pRdr;
-    int             rc;
-#if K_OS == K_OS_OS2
+    int          rc;
+
+#if K_OS == K_OS_DARWIN
+    rc = kHlpSys_close(pRdrFile->File);
+
+#elif K_OS == K_OS_OS2
     rc = DosClose(pRdrFile->File);
 
@@ -1068,13 +1121,42 @@
 
     /*
-     * Open the file and determin its size.
-     */
-#if K_OS == K_OS_OS2
-    ULONG           ulAction = 0;
-    FILESTATUS3     Info;
-    APIRET          rc;
-    HFILE           File = 0;
-    KFOFF           cb;
-    char            szFilename[CCHMAXPATH];
+     * Open the file, determin its size and correct filename.
+     */
+#if K_OS == K_OS_DARWIN
+    int         File;
+    KFOFF       cb;
+    KFOFF       rc;
+    char        szFilename[1024];
+
+    cchFilename = kHlpStrLen(pszFilename);
+    if (cchFilename >= sizeof(szFilename))
+        return KERR_OUT_OF_RANGE;
+    kHlpMemCopy(szFilename, pszFilename, cchFilename + 1);
+    /** @todo normalize the filename. */
+
+# ifdef O_BINARY
+    File = kHlpSys_open(pszFilename, O_RDONLY | O_BINARY, 0);
+# else
+    File = kHlpSys_open(pszFilename, O_RDONLY, 0);
+# endif
+    if (File < 0)
+        return -File;
+
+    cb = kHlpSys_lseek(File, SEEK_END, 0);
+    rc = kHlpSys_lseek(File, SEEK_SET, 0);
+    if (    cb < 0
+        ||  rc < 0)
+    {
+        kHlpSys_close(File);
+        return cb < 0 ? -cb : -rc;
+    }
+
+#elif K_OS == K_OS_OS2
+    ULONG       ulAction = 0;
+    FILESTATUS3 Info;
+    APIRET      rc;
+    HFILE       File = 0;
+    KFOFF       cb;
+    char        szFilename[CCHMAXPATH];
 
     if ((uintptr_t)pszFilename >= 0x20000000)
@@ -1152,17 +1234,16 @@
     pRdrFile = (PKRDRFILE)kHlpAlloc(sizeof(*pRdrFile) + cchFilename);
     if (!pRdrFile)
-#if K_OS == K_OS_OS2
-    {
+    {
+#if K_OS == K_OS_DARWIN
+        kHlpSys_close(File);
+#elif K_OS == K_OS_OS2
         DosClose(File);
-        return ERROR_NOT_ENOUGH_MEMORY;
-    }
 #elif K_OS == K_OS_WINDOWS
-    {
         CloseHandle(File);
-        return ERROR_NOT_ENOUGH_MEMORY;
-    }
 #else
 # error "port me"
 #endif
+        return KERR_NO_MEMORY;
+    }
 
     /*
