Advertisement
pochti_da

Untitled

Sep 30th, 2020
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6.  
  7. unsigned long long hash_function(unsigned long long);
  8.  
  9. enum
  10. {
  11.     MIN_ARGS = 4
  12. };
  13.  
  14. int
  15. main(int argc, char *argv[])
  16. {
  17.     unsigned long long h;
  18.     int count;
  19.     if (argc < MIN_ARGS ||
  20.             sscanf(argv[2], "%llx", &h) != 1 ||
  21.             sscanf(argv[3], "%d", &count) != 1) {
  22.         return 0;
  23.     }
  24.    
  25.     int fd = open(argv[1], O_WRONLY | O_CREAT,
  26.             S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  27.     if (fd == -1) {
  28.         return 0;
  29.     }
  30.  
  31.     int file_end = lseek(fd, 0, SEEK_END);
  32.     for (int i = 0; i < count; ++i) {
  33.         off_t offset = file_end + (count - i - 1) * sizeof(h);
  34.         if (lseek(fd, offset, SEEK_SET) == -1 ||
  35.             write(fd, &h, sizeof(h)) != sizeof(h)) {
  36.             return 0;
  37.         }
  38.  
  39.         h = hash_function(h);
  40.     }
  41.  
  42.     close(fd);
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement