55 lines
853 B
C
55 lines
853 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
|
|
#define MB_BYTES (1024*1024)
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
unsigned long *p;
|
|
unsigned long *m;
|
|
int i = 0;
|
|
int max;
|
|
|
|
const char *file_path = argv[1];
|
|
int fd = open(file_path, O_RDONLY);
|
|
if (fd == -1) {
|
|
perror("open");
|
|
return 1;
|
|
}
|
|
|
|
struct stat sb;
|
|
if (fstat(fd, &sb) == -1) {
|
|
perror("fstat");
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
|
|
size_t file_size = sb.st_size;
|
|
|
|
printf("%ld, %d Kb\n", file_size, file_size/1024);
|
|
void *mapped = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
if (mapped == MAP_FAILED) {
|
|
perror("mmap");
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
m = mapped;
|
|
|
|
max = file_size / 8;
|
|
p = malloc(file_size);
|
|
|
|
for (i = 0; i < max; i++)
|
|
*(p + i) = *(m + i);
|
|
|
|
while(1);
|
|
|
|
return 0;
|
|
}
|
|
|