Created
November 1, 2019 03:39
-
-
Save surinoel/7e77319edded1f9afc0f5b5bc701c47d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
#include <sys/mman.h> | |
#include <sys/types.h> | |
int main(int argc, char **argv) { | |
int i; | |
int fd; | |
int ret; | |
struct stat sb; | |
void *ptr; | |
fd = open("ls.txt", O_RDONLY); | |
if(fd < 0) { | |
printf("open() error\n"); | |
return -1; | |
} | |
ret = fstat(fd, &sb); | |
if(ret < 0) { | |
printf("fstat() error\n"); | |
close(fd); | |
return -1; | |
} | |
/* | |
void *mmap(void *addr, size_t length, int prot, int flags, | |
int fd, off_t offset); | |
int munmap(void *addr, size_t length); | |
*/ | |
ptr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
if(ptr == MAP_FAILED) { | |
printf("mmap() error\n"); | |
close(fd); | |
return -1; | |
} | |
for(i=0; i<sb.st_size; i++) { | |
printf("%c", *(char *)(ptr + i)); | |
} | |
munmap(0, sb.st_size); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment