]> mj.ucw.cz Git - libucw.git/blobdiff - lib/mmap.c
Forgot to commit this one during the "search by age" changes.
[libucw.git] / lib / mmap.c
index 62101f8d66a210c16225765c48cfc48e83f13e4f..2b057b6cf7da2071508b0393a4c06d19f4f136b7 100644 (file)
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#ifndef MAP_FAILED
-#define MAP_FAILED ((void *)-1L)
-#endif
-
 void *
-mmap_file(byte *name, unsigned *len)
+mmap_file(byte *name, unsigned *len, int writeable)
 {
-  int fd = open(name, O_RDONLY);
+  int fd = open(name, writeable ? O_RDWR : O_RDONLY);
   struct stat st;
   void *x;
 
@@ -31,10 +27,21 @@ mmap_file(byte *name, unsigned *len)
     {
       if (len)
        *len = st.st_size;
-      x = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-      if (x == MAP_FAILED)
-       x = NULL;
+      if (st.st_size)
+       {
+         x = mmap(NULL, st.st_size, writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, fd, 0);
+         if (x == MAP_FAILED)
+           x = NULL;
+       }
+      else     /* For empty file, we can return any non-zero address */
+       return "";
     }
   close(fd);
   return x;
 }
+
+void
+munmap_file(void *start, unsigned len)
+{
+  munmap(start, len);
+}