Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. /*
  2.  * Copyright 2016-2017 CHASE MARSH
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <strings.h>
  10. #include <dirent.h>
  11. #include <unistd.h>
  12. #include <ctype.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15.  
  16. //MACROS: CONSTANTS
  17. #define MAX_SIZE 256
  18.  
  19. //global variables for number of directories, and sum.
  20. int counter = 0;
  21. long sum = 0;
  22.  
  23. void sortUtility(struct dirent *list, int size) { //this method is supposed to perform an insertion sort
  24.     int i,j;
  25.     struct dirent *temp;
  26.  
  27.     for(i = 1; i < size; i++) {
  28.         j = i - 1;
  29.         while (j >= 0 && strcasecmp(list[j+1].d_name, list[j].d_name) > 0) {
  30.             temp = &list[j+1];
  31.             list[j + 1] = list[j];
  32.             list[j] = *temp;
  33.             j--;
  34.         }
  35.     }
  36.  
  37. }
  38.  
  39. int getSize(char const *sizeToGet) {//this gets the size and prints the correct suffix of a directory
  40.             struct stat sizes;
  41.  
  42.             stat(sizeToGet, &sizes);
  43.  
  44.             if(sizes.st_size < 1024) {
  45.                 printf("%u", sizes.st_size);
  46.                 printf("B\t");
  47.                 sum += sizes.st_size;
  48.             }
  49.             else if(sizes.st_size < (1024*1024)) {
  50.                 sizes.st_size = sizes.st_size / 1024;
  51.                 printf("%u", sizes.st_size);
  52.                 printf("K\t");
  53.                 sum += sizes.st_size;
  54.             }
  55.             else {
  56.                 printf("%u", sizes.st_size);
  57.                 printf("MB\t");
  58.                 sum += sizes.st_size;
  59.             }
  60.             return sizes.st_size;
  61.  
  62. }
  63.  
  64. void formatSize(long size) {//this method formats the suffic based on an integer provided
  65.             if(size < 1024) {
  66.                 printf("%d", size);
  67.                 printf("B\t");
  68.  
  69.             }
  70.             else if(size < (1024*1024)) {
  71.                 printf("%d", size/1024);
  72.                 printf("K\t");
  73.             }
  74.             else {
  75.                 printf("%d", size/(1024*1024));
  76.                 printf("MB\t");
  77.             }
  78.  
  79. }
  80.  
  81. void listFiles(char const *directory) {//method to recursively list the directories within the current directory
  82.  
  83.  
  84.     DIR *dir = opendir(directory);
  85.     struct dirent *names;
  86.     struct dirent *namelist[1024];
  87.     char *cwd;
  88.  
  89.     if(dir == NULL) { //if there is nothing then just exit
  90.         return;
  91.     }
  92.  
  93.         while ((names = readdir(dir)) != NULL) {
  94.             if(!(strcmp(names->d_name, ".") == 0)) { //take care of the "." and ".." directories
  95.                 if(!(strcmp(names->d_name, "..") == 0)) {
  96.                     counter++;
  97.  
  98.                     if(names->d_type == DT_DIR) { //takes care of child directories
  99.  
  100.                         cwd = malloc(strlen(directory) + strlen(names->d_name) + 2); //allocate memory and add path
  101.                         strcpy(cwd, directory);
  102.                         strcat(cwd, "/");
  103.                         strcat(cwd, names->d_name);
  104.  
  105.                         listFiles(cwd);//recursive call to the method
  106.  
  107.                         free(cwd);//free the memory used
  108.                     }
  109.  
  110.                     namelist[counter] = names->d_name; //put all of the directory names into an array
  111.                     sortUtility(names, counter);//call sort method
  112.  
  113.                     getSize(names->d_name); //print the sizes
  114.                     printf(".%s/", directory);//print the path
  115.                     printf("%s\n", names->d_name);//print the actual directory or file name
  116.  
  117.                 }
  118.             }
  119.             counter++; //increase counter of number of directories
  120.         }
  121.  
  122.  
  123.         closedir(dir); //close the current working directory
  124.  
  125. }
  126.  
  127. //main entry point - starts in current working directory
  128. int main() {
  129.         char startDirectory[MAX_SIZE];
  130.  
  131.         getcwd(startDirectory, sizeof(startDirectory));
  132.         listFiles(startDirectory);
  133.         formatSize(sum); //calls the formatSize method on sum to get the suffix properly formatted
  134.         printf(".");
  135.  
  136.         return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement