blob: 6a1faf16d7528dd96145ee46eeee344a987acbac (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#include <fcntl.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "map.h"
struct map *open_map(const char *fname, bool *isdir) {
if (isdir != NULL) *isdir = false;
int fd = open(fname, O_RDONLY);
struct stat sb;
if (fstat(fd, &sb) == -1) {
//fprintf(stderr, "Kon bestand niet lezen\n");
return NULL;
}
char *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
//fprintf(stderr, "Kon bestand niet projecteren in geheugen\n");
if (isdir != NULL) *isdir = S_ISDIR(sb.st_mode);
return NULL;
}
struct map *res = calloc(1, sizeof(struct map));
res->addr = addr;
res->end = addr + sb.st_size;
res->sb = sb;
res->fd = fd;
return res;
}
void close_map(struct map *m) {
munmap(m->addr, m->sb.st_size);
close(m->fd);
free(m);
}
|