From e654642d76e08a6a6b0517022eaeab29c5819c46 Mon Sep 17 00:00:00 2001 From: garhve Date: Wed, 14 Dec 2022 19:53:52 +0800 Subject: rewrite program --- c/tree/basic.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 c/tree/basic.c (limited to 'c/tree/basic.c') 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); +} + -- cgit v1.2.3-70-g09d2