-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmmap_read.c
More file actions
43 lines (37 loc) · 790 Bytes
/
mmap_read.c
File metadata and controls
43 lines (37 loc) · 790 Bytes
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
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#define SIZE 445
int main(int argc, char *argv[])
{
const char *memblock;
int fd;
if (argc != 2) {
printf("Expected 1 argument (filename), found %d\n", argc-1);
return 1;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
printf("open fail %s\n", strerror(errno));
return fd;
}
memblock = mmap(NULL, SIZE, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (memblock == MAP_FAILED) {
perror("mmap");
return 2;
}
char c;
for(uint64_t i = 0; i < SIZE; i++)
{
c = memblock[i];
asm volatile("" :: "m" (c));
// printf("%c", memblock[i]);
}
printf("%c", memblock[0]);
return 0;
}