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
| #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int main (int argc, char *argv[])
{
if (argc < 2) return 1;
int fd = open ( argv[1], O_RDONLY);
struct stat sb;
fstat (fd, &sb); // Fill File Status Structure
//void * mmap (void *addr /* Address suggestion to the kernel of where best to map the file*/,
// size_t len /* Length of Mapping */,
// int prot /* Memory Protection Flags */,
// int flags /* Type of Mapping like shared, private, etc*/,
// int fd /* File descriptor */,
// off_t offset /* Offset to start mapping from in file, must be multiple of page size*/);
void *p = mmap (0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); // Map File in Memory
close (fd);
int len;
for (len = 0; len < sb.st_size; len++)
putchar ( * ((char*) p + len ));
munmap (p, sb.st_size); // Un-Map Mapped File from memory
return 0;
}
|