Advertisement
Lexolordan

Untitled

Dec 8th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <sys/types.h>
  3. #include <sys/mman.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <stdint.h>
  10.  
  11. struct Item {
  12.     int value;
  13.     uint32_t next_pointer;
  14. };
  15.  
  16. int main(int argc, char *argv[]) {
  17.     char *file_name = argv[1];
  18.     int fd = open(file_name, O_RDONLY);
  19.  
  20.     struct Item item;
  21.     struct stat st;
  22.     fstat(fd, &st);
  23.     char *contents = mmap(NULL,
  24.                           st.st_size,
  25.                           PROT_READ,
  26.                           MAP_PRIVATE,
  27.                           fd,
  28.                           0);
  29.     size_t len = st.st_size;
  30.     uint32_t entry = 0;
  31.  
  32.     memcpy(&item, contents, sizeof(item));
  33.     while (item.next_pointer != 0) {
  34.         printf("%d ", item.value);
  35.         entry = item.next_pointer;
  36.         memcpy(&item, contents + entry, sizeof(item));
  37.     }
  38.     printf("%d ", item.value);
  39.  
  40.     munmap(contents, st.st_size);
  41. finally:
  42.     close(fd);
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement