aboutsummaryrefslogtreecommitdiffstats
path: root/csci4061/101220_breakout/exercise.c
diff options
context:
space:
mode:
authorMatt Strapp <matt@mattstrapp.net>2022-05-24 11:18:46 -0500
committerMatt Strapp <matt@mattstrapp.net>2022-05-24 11:19:55 -0500
commit7a73162607544204032aa66cce755daf21edebda (patch)
tree58578e01f15f34a855d99c32898db9d7a1603e67 /csci4061/101220_breakout/exercise.c
parentdo some stuff (diff)
downloadhomework-7a73162607544204032aa66cce755daf21edebda.tar
homework-7a73162607544204032aa66cce755daf21edebda.tar.gz
homework-7a73162607544204032aa66cce755daf21edebda.tar.bz2
homework-7a73162607544204032aa66cce755daf21edebda.tar.lz
homework-7a73162607544204032aa66cce755daf21edebda.tar.xz
homework-7a73162607544204032aa66cce755daf21edebda.tar.zst
homework-7a73162607544204032aa66cce755daf21edebda.zip
Graduate
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'csci4061/101220_breakout/exercise.c')
-rw-r--r--csci4061/101220_breakout/exercise.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/csci4061/101220_breakout/exercise.c b/csci4061/101220_breakout/exercise.c
new file mode 100644
index 0000000..b6e863e
--- /dev/null
+++ b/csci4061/101220_breakout/exercise.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <string.h>
+#include <time.h>
+
+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