X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fmmap.c;h=928da4bc176266a694276f93aa102801e4014e08;hb=dc63fe59d2bb4c3eb8be67790a55069c8c0a46a9;hp=2b057b6cf7da2071508b0393a4c06d19f4f136b7;hpb=dc7ed71568558994e9288eb88535dee1f973ff39;p=libucw.git diff --git a/lib/mmap.c b/lib/mmap.c index 2b057b6c..928da4bc 100644 --- a/lib/mmap.c +++ b/lib/mmap.c @@ -1,7 +1,10 @@ /* - * Sherlock Library -- Mapping of Files + * UCW Library -- Mapping of Files * - * (c) 1999 Martin Mares + * (c) 1999--2002 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ #include "lib/lib.h" @@ -13,29 +16,26 @@ #include void * -mmap_file(byte *name, unsigned *len, int writeable) +mmap_file(const char *name, unsigned *len, int writeable) { int fd = open(name, writeable ? O_RDWR : O_RDONLY); struct stat st; void *x; if (fd < 0) - return NULL; + die("open(%s): %m", name); if (fstat(fd, &st) < 0) - x = NULL; - else + die("fstat(%s): %m", name); + if (len) + *len = st.st_size; + if (st.st_size) { - if (len) - *len = st.st_size; - 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 ""; + x = mmap(NULL, st.st_size, writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, fd, 0); + if (x == MAP_FAILED) + die("mmap(%s): %m", name); } + else /* For empty file, we can return any non-zero address */ + x = ""; close(fd); return x; }