The_Law

Untitled

Nov 5th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <dirent.h>
  4. #include <unistd.h>
  5. #include <limits.h>
  6. #include <string.h>
  7.  
  8. int
  9. main(int argc, char *argv[])
  10. {
  11.     DIR *main_dir = opendir(argv[1]);
  12.     if (main_dir == NULL) {
  13.         return 1;
  14.     }
  15.     unsigned long long sum = 0;
  16.     struct dirent *curr_dir;
  17.     while ((curr_dir = readdir(main_dir)) != NULL) {
  18.         _Bool is_eq1 = strcmp(curr_dir->d_name, ".");
  19.         _Bool is_eq2 = strcmp(curr_dir->d_name, "..");
  20.         _Bool is_symb = 'A' <= curr_dir->d_name[0] && curr_dir->d_name[0] <= 'Z';
  21.         if (is_eq1 && is_eq2 && is_symb) {
  22.             char folder[PATH_MAX];
  23.             snprintf(folder, PATH_MAX, "%s%s", argv[1], curr_dir->d_name);
  24.             struct stat f;
  25.             _Bool is_exists = stat(folder, &f) != -1;
  26.             _Bool is_reg = S_ISREG(f.st_mode);
  27.             _Bool is_uid_equ = f.st_uid == getuid();
  28.             sum = is_exists && is_reg && is_uid_equ ? sum + f.st_size : sum;
  29.         }
  30.     }
  31.     printf("%llu", sum);
  32.     if (closedir(main_dir) == -1) {
  33.         return -1;
  34.     }
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment