/*
- * Sherlock Library -- Mapping of Files
+ * UCW Library -- Mapping of Files
*
- * (c) 1999 Martin Mares <mj@ucw.cz>
+ * (c) 1999--2002 Martin Mares <mj@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
*/
#include "lib/lib.h"
#include <sys/stat.h>
#include <sys/mman.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, int writeable)
{
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;
}