summaryrefslogtreecommitdiff
path: root/c/tree/basic.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/tree/basic.c')
-rw-r--r--c/tree/basic.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/c/tree/basic.c b/c/tree/basic.c
new file mode 100644
index 0000000..8ce0daa
--- /dev/null
+++ b/c/tree/basic.c
@@ -0,0 +1,71 @@
+/**
+ * @author : garhve (dev@garhve.com)
+ * @file : basic
+ * @created : Wednesday Dec 14, 2022 18:36:27 CST
+ * @description : basic functions go here
+ */
+
+#include "tree.h"
+
+void error(const char * str)
+{
+ perror(str);
+ exit(EXIT_FAILURE);
+}
+
+void init(regfile * head)
+{
+ head->node = NULL;
+ head->next = NULL;
+
+ printf("Initialize done!\n");
+}
+
+void empty(regfile * head)
+{
+ regfile * scan = head->next;
+
+ while (head->next != NULL) {
+ head->next = scan->next;
+ free(scan->node);
+ free(scan);
+ scan = head->next;
+ }
+}
+
+void addreg(regfile * head, const char * d_name)
+{
+ regfile * new = head;
+
+ while (new->next)
+ new = new->next;
+ new->next = (regfile *) calloc (1,sizeof(regfile));
+
+ if (!new->next)
+ error("allocation for new regfile failed");
+ new->next->node = addnodes(d_name);
+ new->next->next = NULL;
+}
+
+void extractContent(regfile * head, const char * parent)
+{
+ DIR * d = opendir(parent);
+ struct dirent * dir;
+
+ if (!d)
+ error("failed to open directory\n");
+
+ while ((dir = readdir(d))) {
+ if (dir->d_name[1] == '.' || (dir->d_name[0] == '.' && dir->d_name[1] == '\0'))
+ continue;
+ addreg(head, dir->d_name);
+ }
+}
+
+void getfile(regfile * head, const char * parent)
+{
+ init(head);
+
+ extractContent(head, parent);
+}
+