Advertisement
MaksNew

Untitled

Mar 31st, 2022
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<sys/types.h>
  5. #include<sys/stat.h>
  6. #include<dirent.h>
  7. #include<errno.h>
  8. #include<limits.h>
  9. #include<libgen.h>
  10.  
  11. char *AppName;
  12.  
  13. int ProcessFolder(FILE *outfile, char *path)
  14. {
  15.     DIR *curDir;
  16.     curDir = opendir(path);
  17.  
  18.     if (curDir == NULL)
  19.     {
  20.         fprintf(stderr, "%s: %s %s\n", AppName, path, strerror(errno));
  21.         errno = 0;
  22.  
  23.         return -1;
  24.     }
  25.  
  26.     struct dirent *dent;
  27.     struct stat buf;
  28.     char *file = alloca(strlen(path) + NAME_MAX + 2);
  29.  
  30.     if (file==NULL)
  31.     {
  32.         fprintf(stderr,"%s: %s.", AppName, strerror(errno));
  33.  
  34.         return -1;
  35.     }
  36.  
  37.     long int sum = 0;
  38.     int count = 0;
  39.     int maxSize = -1;
  40.     char *maxFile = alloca(NAME_MAX);
  41.  
  42.     if (maxFile==NULL)
  43.     {
  44.         fprintf(stderr, "%s: %s.", AppName, strerror(errno));
  45.  
  46.         return -1;
  47.     }
  48.  
  49.     maxFile[0] = 0;
  50.     errno = 0;
  51.  
  52.     while (dent = readdir(curDir))
  53.     {
  54.         if (strcmp(".", dent->d_name) && strcmp("..", dent->d_name))
  55.         {
  56.             strcpy(file, path);
  57.             strcat(file, "/");
  58.             strcat(file, dent->d_name);
  59.  
  60.             if (lstat(file,&buf) == -1)
  61.             {
  62.                 fprintf(stderr, "%s: %s %s\n", AppName, path, strerror(errno));
  63.  
  64.                 return -1;
  65.             }
  66.  
  67.             if (!S_ISDIR(buf.st_mode))
  68.             {
  69.                 if (buf.st_size > maxSize)
  70.                 {
  71.                     maxSize = buf.st_size;
  72.                     strcpy(maxFile, basename(file));
  73.                 }
  74.  
  75.                 sum += buf.st_size;
  76.                 count++;
  77.             }
  78.  
  79.             if (S_ISDIR(buf.st_mode))
  80.             {
  81.                 ProcessFolder(outfile, file);
  82.             }
  83.         }
  84.     }
  85.  
  86.     if (errno != 0)
  87.     {
  88.         fprintf(stderr, "%s: %s %s\n", AppName, path, strerror(errno));
  89.  
  90.         return -1;
  91.     }
  92.  
  93.     printf("%s %d %ld %s\n", path, count, sum, maxFile);
  94.     fprintf(outfile, "%s %d %ld %s\n", path, count, sum, maxFile);
  95.  
  96.     if (closedir(curDir) == -1)
  97.     {
  98.         fprintf(stderr, "%s: %s %s\n", AppName, path, strerror(errno));
  99.  
  100.         return -1;
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
  106. int main(int argc, char **argv)
  107. {
  108.     AppName = alloca(strlen(basename(argv[0])));
  109.     strcpy(AppName, basename(argv[0]));
  110.  
  111.     if (argc < 3)
  112.     {
  113.         fprintf(stderr, "%s: Enter two arguments.\n", AppName);
  114.  
  115.         return -1;
  116.     }
  117.  
  118.     FILE *file;
  119.  
  120.     if ((file = fopen(argv[2], "w")) == NULL)
  121.     {
  122.         fprintf(stderr, "%s: %s %s\n", AppName, argv[2], strerror(errno));
  123.  
  124.         return -1;
  125.     }
  126.  
  127.     if (realpath(argv[1], NULL) == NULL)
  128.     {
  129.         fprintf(stderr, "%s: %s %s\n", AppName, argv[1], strerror(errno));
  130.  
  131.         return -1;
  132.     }
  133.  
  134.     ProcessFolder(file, realpath(argv[1], NULL));
  135.  
  136.     if (fclose(file) != 0)
  137.     {
  138.         fprintf(stderr,"%s: %s %s\n", AppName, argv[2], strerror(errno));
  139.  
  140.         return -1;
  141.     }
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement