Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/mman.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <time.h>
  9. #include <sys/stat.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12.  
  13. struct Item
  14. {
  15.   int value;
  16.   uint32_t next_pointer;
  17. };
  18.  
  19. void find_items_layout(char* ptr) {
  20.     struct Item *item = (struct Item*)ptr;
  21.     printf("%d ", item->value);
  22.  
  23.     while (item->next_pointer != 0) {
  24.         item = (struct Item*)(item->next_pointer + ptr);
  25.         printf("%d ", item->value);
  26.     }
  27. }
  28.  
  29. int main(int argc, char* argv[]) {
  30.     char *fname = argv[1];
  31.     int fd = open(fname, O_RDONLY);
  32.  
  33.     if (fd == -1) {
  34.         perror("failed to open file");
  35.         return 2;
  36.     }
  37.  
  38.     FILE * fp = fdopen(fd, "r");
  39.     fseek(fp, 0, SEEK_END);
  40.     int length = ftell(fp);
  41.  
  42.     char *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
  43.     if ((int64_t)ptr == -1) {
  44.         perror("mmap failed");
  45.         return 3;
  46.     }
  47.  
  48.     find_items_layout(ptr);
  49.  
  50.     munmap(ptr, length);
  51.     close(fd);
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement