diff options
author | Matthew Strapp <msattr@gmail.com> | 2020-10-26 08:12:34 -0500 |
---|---|---|
committer | Matthew Strapp <msattr@gmail.com> | 2020-10-26 08:12:34 -0500 |
commit | d7afb98ca2d9ed18400310f42d3f3c8177959280 (patch) | |
tree | 600442970cb3afb8ab2f56f2a290cfe76d2d0d10 /csci4061/101220_breakout | |
parent | e (diff) | |
download | homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.gz homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.bz2 homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.lz homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.xz homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.tar.zst homework-d7afb98ca2d9ed18400310f42d3f3c8177959280.zip |
Do breakouts
Diffstat (limited to 'csci4061/101220_breakout')
-rw-r--r-- | csci4061/101220_breakout/exercise.c | 88 |
1 files changed, 59 insertions, 29 deletions
diff --git a/csci4061/101220_breakout/exercise.c b/csci4061/101220_breakout/exercise.c index be8bb11..b6e863e 100644 --- a/csci4061/101220_breakout/exercise.c +++ b/csci4061/101220_breakout/exercise.c @@ -7,46 +7,76 @@ #include <fcntl.h> #include <dirent.h> #include <string.h> +#include <time.h> -int numOfEntries(char* path) { - int entries = 0; - while (readdir(path) != NULL) { - entries++; - } return entries; +int numOfEntry(char *path) +{ + int count = 0; + DIR *dir = opendir(path); + struct dirent *entry; + + while ((entry = readdir(dir)) != NULL) + { + + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) + continue; + + count++; + } + + closedir(dir); + + return count; } -int main(int argc, char** argv){ - if (argc < 2) { +int main(int argc, char **argv) +{ + + if (argc < 2) + { printf("Pass the path as an argument to the program"); exit(1); } - char* path = argv[1]; - DIR* dir = opendir(path); - if(dir==NULL){ + char *path = argv[1]; + + DIR *dir = opendir(path); + if (dir == NULL) + { printf("The path passed is invalid"); return -1; } - struct dirent* entry; - DIR* current = dir; - -while ((entry = readdir(dir)) != NULL) { - if (entry->d_type == "DT_DIR"){ - printf("Directory: %s\n", entry->d_name); - printf(" Entries: %d\n", numOfEntries(entry)); - } else if (entry->d_type == "DT_REG") { - printf("Regular File: %s\n", entry->d_name); - printf(" Owner: %d\n"); - printf(" Size: %f\n"); - } else { - printf("%s\n", entry -> d_name); + struct dirent *entry; + + while ((entry = readdir(dir)) != NULL) + { + + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) + continue; + + if (entry->d_type == DT_DIR) + { + char next[strlen(path) + strlen(entry->d_name) + 2]; + next[0] = '\0'; + strcat(next, path); + strcat(next, "/"); + strcat(next, entry->d_name); + printf("Directory: %s\n\tEntries: %d\n", entry->d_name, numOfEntry(next)); } - } - /* - Iterate through the elements in argv[1] - Refer the ls example in slides if you have any doubts - */ - + else if (entry->d_type == DT_REG) + { + struct stat *buf = (struct stat *)malloc(sizeof(struct stat)); + stat(entry->d_name, buf); + printf("Regular File: %s\n\tOwner: %d\n\tSize: %f\n\tInode: %llu\n", + entry->d_name, buf->st_uid, (double)buf->st_size, buf->st_ino); + free(buf); + } + else + { + printf("File: %s\n\tType:%hhu\n", entry->d_name, entry->d_type); + } + } + closedir(dir); return 0; }
\ No newline at end of file |