blob: 19d7129ddae691261d555f33fec9f3912cce8929 (
plain) (
blame)
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
|
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "mapping.h"
int map_file(const char* filename, void **addr, size_t *size)
{
struct stat info;
int error = 0;
int fd;
error = stat(filename, &info);
if (!error) {
*size = info.st_size;
if (info.st_size > 0) {
fd = open(filename, O_RDWR);
if (fd >= 0) {
*addr = mmap(NULL, *size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE,
fd, 0);
if (*addr == MAP_FAILED)
error = -1;
close(fd);
} else
error = fd;
} else
*addr = NULL;
}
return error;
}
|