Advertisement
falchikova_v

Untitled

May 28th, 2020
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <limits.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <dirent.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <string.h>
  11.  
  12. int main (int argc, char * argv[])
  13. {
  14. long long sum_size = 0;
  15. long long max_size = 0;
  16. char max_name[500] = {'\0'};
  17. char *p;
  18. long long max_sum_size = strtol(argv[2], &p, 10);
  19.  
  20. struct dirent *ep;
  21. DIR *dp = opendir(argv[1]);
  22. if (dp != NULL) {
  23. ep = readdir(dp);
  24. while (ep != NULL) {
  25. struct stat buffer;
  26. char file_path[PATH_MAX + 1];
  27. sprintf(file_path, "%s/%s", argv[1], ep->d_name);
  28. int status = stat(file_path, &buffer);
  29. if (status == 0) {
  30. //регулярный файл?
  31. if (S_ISREG(buffer.st_mode) && !S_ISLNK(buffer.st_mode)) {
  32. sum_size += buffer.st_size;
  33. if (buffer.st_size > max_size) {
  34. max_size = buffer.st_size;
  35. strcpy(max_name, ep->d_name);
  36. } else if (buffer.st_size == max_size &&
  37. strcmp(ep->d_name, max_name) < 0) {
  38. strcpy(max_name, ep->d_name);
  39. }
  40. }
  41. }
  42. ep = readdir(dp);
  43. }
  44. closedir(dp);
  45. }
  46. if (sum_size > max_sum_size) {
  47. printf("%s", max_name);
  48. }
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement