-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmmap.c
51 lines (44 loc) · 1006 Bytes
/
mmap.c
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
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char **argv)
{
int ret;
int fd;
void *addr;
struct stat s;
if (argc != 2) {
printf("%s [filename]\n", argv[0]);
return -1;
}
// before we call open
//
// 1. echo "mmap" > test
// 2. ./a.out test
//
fd = open(argv[1], O_RDWR);
if (fd < 0) {
printf("failed to open %s\n", argv[1]);
return -1;
}
ret = stat(argv[1], &s);
if (ret < 0) {
printf("failed to stat %s\n", argv[1]);
return -1;
}
addr = mmap(NULL, s.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (!addr) {
printf("Failed to mmap %s\n", strerror(errno));
return -1;
}
printf("data at the address %p is %s\n", addr, addr);
munmap(addr, s.st_size);
close(fd);
return 0;
}