Advertisement
pochti_da

Untitled

Oct 7th, 2020
2,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 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. #include <limits.h>
  7.  
  8. int
  9. main(int argc, char *argv[])
  10. {
  11.     int fd;
  12.     if ((fd = open(argv[1], O_RDWR)) == -1) {
  13.         return 1;
  14.     }
  15.  
  16.     off_t min_ind = -1, cnt = 0;
  17.     long long min, tmp;
  18.     while (read(fd, &tmp, sizeof(tmp)) == sizeof(tmp)) {
  19.         if (min_ind == -1 || tmp < min) {
  20.             min = tmp;
  21.             min_ind = cnt;
  22.         }
  23.  
  24.         cnt += sizeof(tmp);
  25.     }
  26.  
  27.     if (lseek(fd, min_ind, SEEK_SET) == -1) {
  28.         return 0;
  29.     }
  30.  
  31.     if (min != LLONG_MIN) {
  32.         min = -min;
  33.     }
  34.  
  35.     if (write(fd, &min, sizeof(min)) != sizeof(min)) {
  36.         return 1;
  37.     }
  38.  
  39.     close(fd);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement