pochti_da

Untitled

Oct 12th, 2020
733
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdint.h>
  7. #include <inttypes.h>
  8.  
  9. enum
  10. {
  11.     MIN_ARGS = 2,
  12.     SHIFT = 256,
  13.     SIZE = 2
  14. };
  15.  
  16. int
  17. main(int argc, char *argv[])
  18. {
  19.     int fd;
  20.  
  21.     if (argc < MIN_ARGS ||
  22.             (fd = open(argv[1], O_RDONLY)) == -1) {
  23.         return 1;
  24.     }
  25.  
  26.     uint8_t tmp[SIZE];
  27.     uint16_t min = 0, min_tmp;
  28.     int flag = 1;
  29.  
  30.     while (read(fd, &tmp, sizeof(tmp))) {
  31.         min_tmp = tmp[0] * SHIFT + tmp[1];
  32.  
  33.         if (min_tmp % 2 == 0 && (flag || min_tmp < min)) {
  34.             min = min_tmp;
  35.             flag = 0;
  36.         }
  37.     }
  38.  
  39.     if (!flag) {
  40.         printf("%"PRIu16"\n", min);
  41.     }
  42.     return 0;
  43. }
RAW Paste Data