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 | |
parent | 9b586cd78e2e87634582bf4d58c6897703c98af9 (diff) |
rewrite program
-rwxr-xr-x | c/tree/a.out | bin | 0 -> 21088 bytes | |||
-rw-r--r-- | c/tree/basic.c | 71 | ||||
-rw-r--r-- | c/tree/main.c | 44 | ||||
-rw-r--r-- | c/tree/tree.h | 34 |
4 files changed, 111 insertions, 38 deletions
diff --git a/c/tree/a.out b/c/tree/a.out Binary files differnew file mode 100755 index 0000000..f07baa0 --- /dev/null +++ b/c/tree/a.out 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); +} + diff --git a/c/tree/main.c b/c/tree/main.c index 75a1939..86d2c30 100644 --- a/c/tree/main.c +++ b/c/tree/main.c @@ -4,26 +4,7 @@ * @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); -} +#include "tree.h" bool checkdir(const char * dir) { @@ -33,21 +14,6 @@ bool checkdir(const char * dir) 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); @@ -71,9 +37,11 @@ void printChild(const char * cur) int main() { - char * cur = "."; - - printChild(cur); + regfile head; + getfile(&head,"."); + + empty(&head); + return 0; } diff --git a/c/tree/tree.h b/c/tree/tree.h new file mode 100644 index 0000000..4ccc687 --- /dev/null +++ b/c/tree/tree.h @@ -0,0 +1,34 @@ +/** + * @author : garhve (dev@garhve.com) + * @file : tree + * @created : Wednesday Dec 14, 2022 18:34:27 CST + * @description : include and define main functions and structs + */ + +#ifndef TREE_H +#define TREE_H + +#include <stdio.h> +#include <unistd.h> +#include <dirent.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <sys/stat.h> + +typedef struct inodes { + char * text; + int level; + bool fod; // file or dir +} nodes; + +typedef struct regular { + nodes * node; + struct regular * next; +} regfile; + +void error(const char * str); +void init(regfile * st); +void getfile(regfile * head, const char * str); + +#endif /* end of include guard TREE_H */ |