Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.  
  12.     int fd = open(argv[1], O_RDONLY);
  13.     int flag = 1;
  14.     long offset = atol(argv[2]);
  15.     lseek(fd, offset, SEEK_SET);
  16.     size_t res_sz = 0, res_elem = 0;
  17.     unsigned short size;
  18.     size_t result = read(fd, &size, sizeof(unsigned short));
  19.     if (!result) {
  20.         return 0;
  21.     }
  22.  
  23.     lseek(fd, size, SEEK_CUR);
  24.  
  25.     long long offset_f;
  26.     read (fd, &offset_f, sizeof(long long));
  27.  
  28.     res_sz += size;
  29.     ++(res_elem);
  30.  
  31.     if (!offset_f) {
  32.         return 0;
  33.     }
  34.  
  35.     long long record_size = size + sizeof(unsigned short) + sizeof(long long);
  36.     lseek(fd, offset_f - record_size, SEEK_CUR);
  37.  
  38.     while (flag)
  39.     {
  40.         unsigned short buf1;
  41.         read (fd, &buf1, sizeof(unsigned short));
  42.         lseek(fd, buf1, SEEK_CUR);
  43.         long long buf2;
  44.         read(fd, &buf2, sizeof(long long));
  45.         res_sz += buf1;
  46.         ++(res_elem);
  47.         if (buf2) {
  48.             record_size = buf1 + sizeof(unsigned short) + sizeof(long long);
  49.             lseek(fd, buf2 - record_size, SEEK_CUR);
  50.             flag = 1;
  51.         } else {
  52.             flag = 0;
  53.         }
  54.     }
  55.     printf("%u\n", res_elem);
  56.     printf("%u\n", res_sz);
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement