Advertisement
Eugene0091

find max size file and sum all files

Apr 21st, 2021 (edited)
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <time.h>
  7. #include <dirent.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <libgen.h>
  11. #define false 0
  12. #define true 1
  13.  
  14. char *ProgramName;
  15. unsigned long long sizeFile = 0;
  16. unsigned long long sizeAllFiles = 0;
  17. char *pathfile = NULL;
  18. char *Concat(char *s1, char *s2)
  19. {
  20.     size_t len1 = strlen(s1);
  21.     size_t len2 = strlen(s2);
  22.     char *result = malloc(len1 + len2 + 1);
  23.     if (!result)
  24.     {
  25.         fprintf(stderr, "malloc() failed: insufficient memory!\n");
  26.         return NULL;
  27.     }
  28.     memcpy(result, s1, len1);
  29.     memcpy(result + len1, s2, len2 + 1);
  30.     return result;
  31. }
  32.  
  33. void ProceedErrors(char *Message, char *FilePath, char *LinkPath)
  34. {
  35.     fprintf(stderr, "%s: %s %s %s\n", basename(ProgramName), Message, FilePath, LinkPath);
  36. }
  37.  
  38. bool ParametersCountCheck(int Count)
  39. {
  40.     if (Count == 2)
  41.         return true;
  42.     ProceedErrors("Error! Wrong console parameters count! Need 1 parameters.", "", "");
  43.     return false;
  44. }
  45.  
  46. bool SelfOrParentDirectoryCheck(char *ContentName)
  47. {
  48.     return ((strcmp(ContentName, ".") != 0) && (strcmp(ContentName, "..") != 0));
  49. }
  50.  
  51. bool NotCatalogCheck(struct dirent *DirectoryContent)
  52. {
  53.     return ((*DirectoryContent).d_type != 4);
  54. }
  55.  
  56. char *GetNextDirectoryPath(char *DirectoryPath, char *ContentName)
  57. {
  58.     return Concat(Concat(DirectoryPath, "/"), ContentName);
  59. }
  60.  
  61. void AnalyzeDirectoryContent(char *DirectoryPath)
  62. {
  63.     DIR *Directory;
  64.     struct dirent *DirectoryContent;
  65.     if (Directory = opendir(DirectoryPath))
  66.     {
  67.         errno = 0;
  68.         DirectoryContent = readdir(Directory);
  69.         while (DirectoryContent != NULL)
  70.         {
  71.             char *ContentName = (*DirectoryContent).d_name;
  72.             if (SelfOrParentDirectoryCheck(ContentName))
  73.                 if (NotCatalogCheck(DirectoryContent))
  74.                 {
  75.                     char *pathByFile = Concat(DirectoryPath, "/");
  76.                     pathByFile = Concat(pathByFile, ContentName);
  77.                     struct stat st1;
  78.                     stat(pathByFile, &st1);
  79.                     sizeAllFiles += st1.st_size;
  80.                     if (st1.st_size > sizeFile)
  81.                     {
  82.                         sizeFile = st1.st_size;
  83.                         pathfile = pathByFile;
  84.                     }
  85.                 }
  86.                 else
  87.                 {
  88.                     char *NextDirectory = GetNextDirectoryPath(DirectoryPath, ContentName);
  89.                     AnalyzeDirectoryContent(NextDirectory);
  90.                 }
  91.             errno = 0;
  92.             DirectoryContent = readdir(Directory);
  93.         }
  94.     }
  95. }
  96.  
  97. int main(int argc, char *argv[], char *envp[])
  98. {
  99.     if (ParametersCountCheck(argc))
  100.     {
  101.         ProgramName = argv[0];
  102.         char *SourceDirectoryPath = argv[1];
  103.         AnalyzeDirectoryContent(SourceDirectoryPath);
  104.         printf("Max file size: %llu bytes\nDir:%s\nSum all files: %llu bytes\n", sizeFile, pathfile, sizeAllFiles);
  105.         return true;
  106.     }
  107.     return false;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement