Enlightenment CVS committal

Author  : doursse
Project : e17
Module  : proto/evil

Dir     : e17/proto/evil/src/lib/mman


Modified Files:
        mman.c Makefile.am 


Log Message:
* src/lib/Evil.h:
* src/lib/evil.c: (evil_last_error_get):
add evil_last_error_get() function to get
useful error string creation.
* src/lib/dlfcn/dlfcn.c: (get_last_error), (dlopen), (dlsym),
(dladdr):
use UNICODE check instead of compiler checks when needed.
use evil_last_error_get()
* src/lib/mman/mman.c: (mmap), (munmap):
file mapping does not work on Windows CE < 5.0.
if it is the case, just read the file and return
the data.
use evil_last_error_get()
* src/lib/mman/Makefile.am:
* src/lib/Makefile.am:
add libevil dependancy

===================================================================
RCS file: /cvs/e/e17/proto/evil/src/lib/mman/mman.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -3 -r1.5 -r1.6
--- mman.c      26 Apr 2008 16:27:46 -0000      1.5
+++ mman.c      8 Jun 2008 21:39:49 -0000       1.6
@@ -1,4 +1,6 @@
+#include <stdio.h>
 #include <sys/types.h>
+#include <unistd.h>
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
@@ -8,6 +10,8 @@
 # include <io.h>
 #endif /* ! __CEGCC__ */
 
+#include "../Evil.h"
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif /* HAVE_CONFIG_H */
@@ -21,13 +25,15 @@
 #endif /* HAVE___ATTRIBUTE__ */
 
 #if defined(__CEGCC__)
-# define CreateFileMapping CreateFileMappingW
 # define _get_osfhandle get_osfhandle
 # elif defined (__MINGW32CE__)
 # define _get_osfhandle(FILEDES) ((long)FILEDES)
 #endif /* ! __CEGCC__ && ! __MINGW32CE__ */
 
 
+/***** API *****/
+
+
 void *
 mmap(void  *addr __UNUSED__,
      size_t len,
@@ -36,86 +42,187 @@
      int    fd,
      off_t  offset)
 {
+   OSVERSIONINFO os_version;
    void  *data;
-   HANDLE fm;
-   DWORD  protect = PAGE_NOACCESS;
-   DWORD  access = 0;
 
-   /* support only MAP_SHARED */
-   if (!(flags & MAP_SHARED)) return (void *)~0;
+   os_version.dwOSVersionInfoSize = sizeof(os_version);
+   if (!GetVersionEx(&os_version))
+     {
+        char *str;
+
+        str = evil_last_error_get();
+        fprintf(stderr, "[Evil] [mmap] GetVersionEx failed: %s\n", str);
+        free(str);
+
+        return MAP_FAILED;
+     }
 
-   if (prot & PROT_EXEC)
+   if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_CE) &&
+       (os_version.dwMajorVersion < 5))
      {
-        if (prot & PROT_READ)
+        size_t size;
+
+        data = malloc(len);
+        if (!data)
           {
-             if (prot & PROT_WRITE)
-               protect = PAGE_EXECUTE_READWRITE;
-             else
-               protect = PAGE_EXECUTE_READ;
+             fprintf (stderr, "[Evil] [mmap] malloc failed\n");
+             return MAP_FAILED;
           }
-        else
+
+        size = read(fd, data, len);
+        if (size != len)
           {
-             if (prot & PROT_WRITE)
-               protect = PAGE_EXECUTE_WRITECOPY;
-             else
-               protect = PAGE_EXECUTE;
+             fprintf (stderr, "[Evil] [mmap] read failed\n");
+             free(data);
+             return MAP_FAILED;
+          }
+
+        if (lseek(fd, 0, SEEK_SET) == -1)
+          {
+             fprintf (stderr, "[Evil] [mmap] lseek failed\n");
+             free(data);
+             return MAP_FAILED;
           }
      }
    else
      {
-        if (prot & PROT_READ)
+        HANDLE fm;
+        DWORD  protect = PAGE_NOACCESS;
+        DWORD  access = 0;
+        HANDLE handle;
+
+        /* support only MAP_SHARED */
+        if (!(flags & MAP_SHARED))
+          return MAP_FAILED;
+
+        if (prot & PROT_EXEC)
           {
-             if (prot & PROT_WRITE)
-               protect = PAGE_READWRITE;
+             if (prot & PROT_READ)
+               {
+                  if (prot & PROT_WRITE)
+                    protect = PAGE_EXECUTE_READWRITE;
+                  else
+                    protect = PAGE_EXECUTE_READ;
+               }
              else
-               protect = PAGE_READONLY;
+               {
+                  if (prot & PROT_WRITE)
+                    protect = PAGE_EXECUTE_WRITECOPY;
+                  else
+                    protect = PAGE_EXECUTE;
+               }
+          }
+        else
+          {
+             if (prot & PROT_READ)
+               {
+                  if (prot & PROT_WRITE)
+                    protect = PAGE_READWRITE;
+                  else
+                    protect = PAGE_READONLY;
+               }
+             else if (prot & PROT_WRITE)
+               protect = PAGE_WRITECOPY;
+          }
+
+        handle = (HANDLE)_get_osfhandle(fd);
+        if (handle == INVALID_HANDLE_VALUE)
+          {
+             fprintf(stderr, "[Evil] [mmap] _get_osfhandle failed\n");
+
+             return MAP_FAILED;
+          }
+
+        fm = CreateFileMapping(handle, NULL, protect, 0, 0, NULL);
+        if (!fm)
+          {
+             char *str;
+
+             str = evil_last_error_get();
+             fprintf(stderr, "[Evil] [mmap] CreateFileMapping failed: %s\n", 
str);
+             free(str);
+
+             return MAP_FAILED;
           }
-        else if (prot & PROT_WRITE)
-          protect = PAGE_WRITECOPY;
-     }
 
-   fm = CreateFileMapping((HANDLE)_get_osfhandle (fd),
-                          NULL,
-                          protect,
-                          0,
-                          0,
-                          NULL);
-   if (!fm) return (void *)~0;
-
-   if (protect & PAGE_READWRITE)
-     access = FILE_MAP_ALL_ACCESS;
-   if (protect & PAGE_WRITECOPY)
-     access = FILE_MAP_COPY;
+        if (protect & PAGE_READWRITE)
+          access = FILE_MAP_ALL_ACCESS;
+        if (protect & PAGE_WRITECOPY)
+          access = FILE_MAP_COPY;
 #if 0
-   if (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ))
-     access = FILE_MAP_EXECUTE;
+        if (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ))
+          access = FILE_MAP_EXECUTE;
 #endif
-   if (protect & (PAGE_READWRITE | PAGE_READONLY))
-     access = FILE_MAP_READ;
-   else
-     {
-        if (protect & PAGE_READWRITE)
-          access = FILE_MAP_WRITE;
-     }
+        if (protect & (PAGE_READWRITE | PAGE_READONLY))
+          access = FILE_MAP_READ;
+        else
+          {
+             if (protect & PAGE_READWRITE)
+               access = FILE_MAP_WRITE;
+          }
 
-   data = MapViewOfFile(fm,
-                        access,
-                        offset & 0xffff0000,
-                        offset & 0x0000ffff,
-                        len);
-   CloseHandle(fm);
+        data = MapViewOfFile(fm,
+                             access,
+                             offset & 0xffff0000,
+                             offset & 0x0000ffff,
+                             len);
+        CloseHandle(fm);
 
-   if (!data) return (void *)~0;
-   else return data;
+        if (!data)
+          {
+             char *str;
+
+             str = evil_last_error_get();
+             fprintf(stderr, "[Evil] [mmap] MapViewOfFile failed: %s\n", str);
+             free(str);
+
+             return MAP_FAILED;
+          }
+     }
+
+   return data;
 }
 
 int
 munmap(void  *addr,
        size_t len __UNUSED__)
 {
-   BOOL res;
+   OSVERSIONINFO os_version;
 
-   res = UnmapViewOfFile(addr);
+   os_version.dwOSVersionInfoSize = sizeof(os_version);
+   if (!GetVersionEx(&os_version))
+     {
+        char *str;
 
-   return (res == 0) ? -1 : 0;
+        str = evil_last_error_get();
+        fprintf(stderr, "[Evil] [munmap] GetVersionEx failed: %s\n", str);
+        free(str);
+
+        return -1;
+     }
+
+   if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_CE) &&
+       (os_version.dwMajorVersion < 5))
+     {
+        if (addr && (addr != MAP_FAILED))
+          free(addr);
+
+        return 0;
+     }
+   else
+     {
+        BOOL res;
+
+        res = UnmapViewOfFile(addr);
+        if (!res)
+          {
+             char *str;
+
+             str = evil_last_error_get();
+             fprintf(stderr, "[Evil] [munmap] UnmapViewOfFile failed: %s\n", 
str);
+             free(str);
+          }
+
+        return (res == 0) ? -1 : 0;
+     }
 }
===================================================================
RCS file: /cvs/e/e17/proto/evil/src/lib/mman/Makefile.am,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -3 -r1.3 -r1.4
--- Makefile.am 26 Apr 2008 16:27:46 -0000      1.3
+++ Makefile.am 8 Jun 2008 21:39:49 -0000       1.4
@@ -7,5 +7,6 @@
 libmman_la_SOURCES = mman.c
 
 libmman_la_CFLAGS = @win32_cflags@
-libmman_la_DEPENDENCIES = $(top_builddir)/config.h
+libmman_la_LIBADD = $(top_builddir)/src/lib/libevil.la
+libmman_la_DEPENDENCIES = $(top_builddir)/config.h 
$(top_builddir)/src/lib/libevil.la
 libmman_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info 
@version_info@



-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
enlightenment-cvs mailing list
enlightenment-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to