summaryrefslogtreecommitdiff
path: root/c/tree
diff options
context:
space:
mode:
Diffstat (limited to 'c/tree')
-rw-r--r--c/tree/README1
-rw-r--r--c/tree/main.c79
2 files changed, 80 insertions, 0 deletions
diff --git a/c/tree/README b/c/tree/README
new file mode 100644
index 0000000..d763b5b
--- /dev/null
+++ b/c/tree/README
@@ -0,0 +1 @@
+Trying to Implement tree like with C for understanding C
diff --git a/c/tree/main.c b/c/tree/main.c
new file mode 100644
index 0000000..75a1939
--- /dev/null
+++ b/c/tree/main.c
@@ -0,0 +1,79 @@
+/**
+ * @author : garhve (dev@garhve.com)
+ * @file : main
+ * @created : Friday Nov 25, 2022 20:22:14 CST
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+
+typedef struct regularFile {
+ char * text;
+ int level;
+} regf;
+
+regf rf = {NULL,0};
+
+void error(const char * str)
+{
+ perror(str);
+ exit(EXIT_FAILURE);
+}
+
+bool checkdir(const char * dir)
+{
+ struct stat st;
+ stat(dir,&st);
+
+ return S_ISDIR(st.st_mode);
+}
+
+void append(const char * dir)
+
+void getFiles(const char * cur)
+{
+ DIR * d = opendir(cur);
+ struct dirent * dir;
+
+ if (!d)
+ error("Failed to open directory");
+
+ while ((dir = readdir(d))) {
+ if (dir && (dir[0] == '.' && dir[1] == '\0' || dir[1] == '0'))
+ continue;
+ append(dir->d_name, level);
+
+void printChild(const char * cur)
+{
+ DIR * d = opendir(cur);
+
+ struct dirent * dir;
+
+ if (!d)
+ error("Failed to open directory");
+
+ while ((dir = readdir(d))) {
+ //if (dir->d_name[1] == '.' || strcmp(".",dir->d_name) == 0)
+ if (dir->d_name[1] == '.' || dir->d_name[0] == '.' && dir->d_name[1] == '\0')
+ continue;
+ printf("%s\n", dir->d_name);
+
+ if (checkdir(dir->d_name) == true) {
+ printChild(dir->d_name);
+ }
+ }
+}
+
+int main()
+{
+ char * cur = ".";
+
+ printChild(cur);
+
+ return 0;
+}