Advertisement
pasholnahuy

Untitled

Dec 19th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6.  
  7. void print(int fd, int32_t cur_ind, int32_t *ptr, int32_t *buf) {
  8. int read_res;
  9. void *cur_buf = buf;
  10. int cur_size = 0;
  11. while ((read_res = read(fd, cur_buf, 12 - cur_size)) > 0) {
  12. cur_buf = cur_buf + read_res;
  13. cur_size += read_res;
  14. }
  15. *ptr = cur_ind + 1;
  16. if (*(buf + 2) != 0) {
  17. lseek(fd, (*(buf + 2) - *ptr) * 12, SEEK_CUR);
  18. *ptr = *(buf + 2);
  19. print(fd, *(buf + 2), ptr, buf);
  20. }
  21.  
  22. printf("%d ", *buf);
  23. if (*(buf + 1) != 0) {
  24. lseek(fd, (*(buf + 1) - *ptr) * 12, SEEK_CUR);
  25. *ptr = *(buf + 1);
  26. print(fd, *(buf + 1), ptr, buf);
  27. }
  28. return;
  29. }
  30.  
  31. int main(int argc, char *argv[]) {
  32. int fd = open(argv[1], O_RDONLY);
  33. int32_t *buf = malloc(12);
  34. int32_t cur_ind = 0;
  35. int32_t cur_pos = 0;
  36. print(fd, 0, &cur_pos, buf);
  37. printf("\n");
  38. free(buf);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement