]> git.saurik.com Git - minimal.git/blob - mapping.h
6882d0312b28c4a850fb0c4a1153e5c30ba188c4
[minimal.git] / mapping.h
1 #include <sys/mman.h>
2
3 #include <errno.h>
4 #include <unistd.h>
5
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9
10 void *Map(const char *path, size_t *size, bool ro) {
11 int fd;
12 _syscall(fd = open(path, ro ? O_RDONLY : O_RDWR));
13
14 struct stat stat;
15 _syscall(fstat(fd, &stat));
16
17 if (size != NULL)
18 *size = stat.st_size;
19
20 void *base;
21 _syscall(base = mmap(0, stat.st_size, ro ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
22
23 _syscall(close(fd));
24 return base;
25 }