diff options
author | garhve <git@garhve.com> | 2022-12-14 19:53:52 +0800 |
---|---|---|
committer | garhve <git@garhve.com> | 2022-12-14 19:53:52 +0800 |
commit | e654642d76e08a6a6b0517022eaeab29c5819c46 (patch) | |
tree | 7bc15617aab5993099d37448c5564386fe8ff5bf /c/tree/basic.c | |
parent | 9b586cd78e2e87634582bf4d58c6897703c98af9 (diff) |
rewrite program
Diffstat (limited to 'c/tree/basic.c')
-rw-r--r-- | c/tree/basic.c | 71 |
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); +} + |