Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <assert.h>
  2. #include <fcntl.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/mman.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10.  
  11. struct Item {
  12. int value;
  13. uint32_t next_pointer;
  14. };
  15.  
  16. int main(int argc, char* argv[])
  17. {
  18. int fd = open(argv[1], O_RDONLY);
  19. if (fd == -1) {
  20. assert(close(fd) == 0);
  21. return 0;
  22. }
  23.  
  24. struct stat s;
  25. assert(fstat(fd, &s) == 0);
  26. if (s.st_size == 0) {
  27. assert(close(fd) == 0);
  28. return 0;
  29. }
  30.  
  31. void* mapped = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
  32. assert(mapped != MAP_FAILED);
  33. char* buf = mapped;
  34.  
  35. struct Item* item = (void*)(buf);
  36. printf("%d ", item->value);
  37. while (item->next_pointer) {
  38. struct Item* help = (void*)(buf + item->next_pointer);
  39. printf("%d ", help->value);
  40. item = help;
  41. }
  42.  
  43. assert(munmap(mapped, s.st_size) == 0);
  44. assert(close(fd) == 0);
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement