Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2016-2017 CHASE MARSH
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- //MACROS: CONSTANTS
- #define MAX_SIZE 256
- //global variables for number of directories, and sum.
- int counter = 0;
- long sum = 0;
- void sortUtility(struct dirent *list, int size) { //this method is supposed to perform an insertion sort
- int i,j;
- struct dirent *temp;
- for(i = 1; i < size; i++) {
- j = i - 1;
- while (j >= 0 && strcasecmp(list[j+1].d_name, list[j].d_name) > 0) {
- temp = &list[j+1];
- list[j + 1] = list[j];
- list[j] = *temp;
- j--;
- }
- }
- }
- int getSize(char const *sizeToGet) {//this gets the size and prints the correct suffix of a directory
- struct stat sizes;
- stat(sizeToGet, &sizes);
- if(sizes.st_size < 1024) {
- printf("%u", sizes.st_size);
- printf("B\t");
- sum += sizes.st_size;
- }
- else if(sizes.st_size < (1024*1024)) {
- sizes.st_size = sizes.st_size / 1024;
- printf("%u", sizes.st_size);
- printf("K\t");
- sum += sizes.st_size;
- }
- else {
- printf("%u", sizes.st_size);
- printf("MB\t");
- sum += sizes.st_size;
- }
- return sizes.st_size;
- }
- void formatSize(long size) {//this method formats the suffic based on an integer provided
- if(size < 1024) {
- printf("%d", size);
- printf("B\t");
- }
- else if(size < (1024*1024)) {
- printf("%d", size/1024);
- printf("K\t");
- }
- else {
- printf("%d", size/(1024*1024));
- printf("MB\t");
- }
- }
- void listFiles(char const *directory) {//method to recursively list the directories within the current directory
- DIR *dir = opendir(directory);
- struct dirent *names;
- struct dirent *namelist[1024];
- char *cwd;
- if(dir == NULL) { //if there is nothing then just exit
- return;
- }
- while ((names = readdir(dir)) != NULL) {
- if(!(strcmp(names->d_name, ".") == 0)) { //take care of the "." and ".." directories
- if(!(strcmp(names->d_name, "..") == 0)) {
- counter++;
- if(names->d_type == DT_DIR) { //takes care of child directories
- cwd = malloc(strlen(directory) + strlen(names->d_name) + 2); //allocate memory and add path
- strcpy(cwd, directory);
- strcat(cwd, "/");
- strcat(cwd, names->d_name);
- listFiles(cwd);//recursive call to the method
- free(cwd);//free the memory used
- }
- namelist[counter] = names->d_name; //put all of the directory names into an array
- sortUtility(names, counter);//call sort method
- getSize(names->d_name); //print the sizes
- printf(".%s/", directory);//print the path
- printf("%s\n", names->d_name);//print the actual directory or file name
- }
- }
- counter++; //increase counter of number of directories
- }
- closedir(dir); //close the current working directory
- }
- //main entry point - starts in current working directory
- int main() {
- char startDirectory[MAX_SIZE];
- getcwd(startDirectory, sizeof(startDirectory));
- listFiles(startDirectory);
- formatSize(sum); //calls the formatSize method on sum to get the suffix properly formatted
- printf(".");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement