/** * @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->text); free(scan->node); free(scan); scan = head->next; } } bool checkdir(const char * d_name) { struct stat st; stat(d_name,&st); return S_ISDIR(st.st_mode); } void allocText(char ** text, const char * str, size_t len) { if (*text == NULL) { *text = (char *) calloc (len, sizeof(char)); if (!*text) error("newly allocation failed"); memmove(*text,str,len+1); } } nodes * addnodes(const char * d_name, int level) { nodes * new = (nodes *) calloc (1,sizeof(nodes)); if (!new) error("allocation for new node failed"); new->len = strlen(d_name); new->level = level; new->fod = checkdir(d_name); allocText(&new->text, d_name, new->len); allocText(&new->text, '\0', 1); return new; } void addreg(regfile * head, const char * d_name, int level) { 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, level); new->next->next = NULL; } void extractContent(regfile * head, const char * parent, int level) { 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, 0); } } void getfile(regfile * head, const char * parent) { init(head); extractContent(head, parent, 0); }