X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fmmap.c;h=87a2047f7038df21268eb32ce0ce958ec6ab8223;hb=2e2adfa5d7ac912434bef5b8a9b25cfb14d4a4a6;hp=47147310ecf018fec0589168fe47a877d3253d50;hpb=b8b4146a066884ae9493d1642ae2a70412b2a5ca;p=libucw.git diff --git a/lib/mmap.c b/lib/mmap.c index 47147310..87a2047f 100644 --- a/lib/mmap.c +++ b/lib/mmap.c @@ -1,21 +1,26 @@ /* * Sherlock Library -- Mapping of Files * - * (c) 1999 Martin Mares, + * (c) 1999 Martin Mares */ +#include "lib/lib.h" + #include #include #include #include #include -#include "lib.h" +#ifndef MAP_FAILED +#warning System includes do not define 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; @@ -27,10 +32,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); +}