Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #define panic(syscall) \
- do { \
- perror(syscall); \
- return EXIT_FAILURE; \
- } while (0)
- int main(int argc, char * argv[])
- {
- int fd;
- int status;
- struct stat st;
- int i, intvar;
- int file_size;
- char *memory;
- intvar = atoi(argv[2]);
- fd = open(argv[1], O_RDONLY);
- if (fd < 0)
- panic("open");
- status = fstat(fd, &st);
- if (status < 0)
- panic("fstat");
- file_size = st.st_size;
- memory = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (memory == MAP_FAILED)
- panic("mmap");
- for (i = 0; i < file_size; i++)
- if (intvar -1 == i){
- printf("%c", memory[i]);
- printf( "%\n");
- }
- munmap(memory, file_size);
- close(fd);
- return 0;
- }
Add Comment
Please, Sign In to add comment