se7enuts

pvz333

May 31st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <sys/mman.h>
  7.  
  8. #define panic(syscall) \
  9. do { \
  10. perror(syscall); \
  11. return EXIT_FAILURE; \
  12. } while (0)
  13.  
  14. int main(int argc, char * argv[])
  15. {
  16. int fd;
  17. int status;
  18. struct stat st;
  19. int i, intvar;
  20. int file_size;
  21. char *memory;
  22. intvar = atoi(argv[2]);
  23.  
  24. fd = open(argv[1], O_RDONLY);
  25. if (fd < 0)
  26. panic("open");
  27.  
  28. status = fstat(fd, &st);
  29. if (status < 0)
  30. panic("fstat");
  31. file_size = st.st_size;
  32.  
  33.  
  34. memory = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
  35. if (memory == MAP_FAILED)
  36. panic("mmap");
  37.  
  38.  
  39. for (i = 0; i < file_size; i++)
  40. if (intvar -1 == i){
  41. printf("%c", memory[i]);
  42. printf( "%\n");
  43. }
  44. munmap(memory, file_size);
  45. close(fd);
  46. return 0;
  47. }
Add Comment
Please, Sign In to add comment