int main(void) { NTree *tree; char **array; tree = NULL; ntree_insert(&tree, NULL, "/"); ntree_insert(&tree, (array = string_split("/", ' ')), "tmp"); free_str_array(array); ntree_print(tree); ntree_free(tree); return (0); }
/** * read_folder - Reads a folder and subfolders * @path: Path to folder * Description: Puts all files/folders to an ntree */ NTree *read_folder(const char *path) { NTree *tree; int len; for (len=0; path[len]; len++); ntree_insert(&tree, NULL, (char *)path); list_dir ((char *)path, len, &tree); return tree; }
int main(void) { NTree *tree; tree = NULL; ntree_insert(&tree, NULL, "/"); ntree_print(tree); ntree_free(tree); return (0); }
/** * list_dir - Recursively goes through all the folders * @dir_name: path * @len: length of original path * @tree: path to ntree * Description: Puts all file/folder names to an ntree */ void list_dir (char * dir_name, int len, NTree **tree) { DIR * d; d = opendir (dir_name); while (1) { char **array; struct dirent * entry; char * d_name; entry = readdir (d); if (! entry) { break; } d_name = entry->d_name; if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) { continue; } filter_it(dir_name+len); ntree_insert(tree, (array = string_split(dir_name, ' ')), d_name); free_str_array(array); filter_back(dir_name+len); if (entry->d_type & DT_DIR) { if (strcmp(d_name, "..")!=0 && strcmp(d_name, ".")!=0) { char *path; path = string_concat(dir_name, '/', d_name); list_dir (path, len, tree); free(path); } } } closedir (d); }