X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fmmap.c;h=87a2047f7038df21268eb32ce0ce958ec6ab8223;hb=2e2adfa5d7ac912434bef5b8a9b25cfb14d4a4a6;hp=46f5cd3add76ef0b9d8643fe415fdd45076eca34;hpb=5b53087fa5a07ff89d34cf3bf3bc1b28809f05c2;p=libucw.git diff --git a/lib/mmap.c b/lib/mmap.c index 46f5cd3a..87a2047f 100644 --- a/lib/mmap.c +++ b/lib/mmap.c @@ -1,25 +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/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; @@ -31,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); +}