]> git.saurik.com Git - minimal.git/blob - mapping.h
Quick naming fixes.
[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 offset, size_t size, size_t *psize, bool ro) {
11 int fd;
12 _syscall(fd = open(path, ro ? O_RDONLY : O_RDWR));
13
14 if (size == _not(size_t)) {
15 struct stat stat;
16 _syscall(fstat(fd, &stat));
17 size = stat.st_size;
18 }
19
20 if (psize != NULL)
21 *psize = size;
22
23 void *base;
24 _syscall(base = mmap(NULL, size, ro ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset));
25
26 _syscall(close(fd));
27 return base;
28 }