]> mj.ucw.cz Git - libucw.git/blob - lib/mmap.c
Implemented mmap_file().
[libucw.git] / lib / mmap.c
1 /*
2  *      Sherlock Library -- Mapping of Files
3  *
4  *      (c) 1999 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #include <sys/mman.h>
12
13 #include "lib.h"
14
15 void *
16 mmap_file(byte *name, unsigned *len)
17 {
18   int fd = open(name, O_RDONLY);
19   struct stat st;
20   void *x;
21
22   if (fd < 0)
23     return NULL;
24   if (fstat(fd, &st) < 0)
25     x = NULL;
26   else
27     {
28       if (len)
29         *len = st.st_size;
30       x = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
31       if (x == MAP_FAILED)
32         x = NULL;
33     }
34   close(fd);
35   return x;
36 }