From dde37c31a72f4773e95faf8223ef450440bdb62c Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Sun, 17 Jan 2021 12:57:21 -0600 Subject: get rid of that trash --- OLD/csci4061/101220_breakout/exercise.c | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 OLD/csci4061/101220_breakout/exercise.c (limited to 'OLD/csci4061/101220_breakout/exercise.c') diff --git a/OLD/csci4061/101220_breakout/exercise.c b/OLD/csci4061/101220_breakout/exercise.c new file mode 100644 index 0000000..b6e863e --- /dev/null +++ b/OLD/csci4061/101220_breakout/exercise.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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) + { + printf("Pass the path as an argument to the program"); + exit(1); + } + + char *path = argv[1]; + + DIR *dir = opendir(path); + if (dir == NULL) + { + printf("The path passed is invalid"); + return -1; + } + 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)); + } + 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 -- cgit v1.2.3