diff options
Diffstat (limited to 'lib/fstree/get_path.c')
-rw-r--r-- | lib/fstree/get_path.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/fstree/get_path.c b/lib/fstree/get_path.c new file mode 100644 index 0000000..4b6f61e --- /dev/null +++ b/lib/fstree/get_path.c @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "fstree.h" + +#include <string.h> +#include <stdlib.h> + +char *fstree_get_path(tree_node_t *node) +{ + tree_node_t *it; + char *str, *ptr; + size_t len = 0; + + if (node->parent == NULL) + return strdup("/"); + + for (it = node; it != NULL && it->parent != NULL; it = it->parent) { + len += strlen(it->name) + 1; + } + + str = malloc(len + 1); + if (str == NULL) + return NULL; + + ptr = str + len; + *ptr = '\0'; + + for (it = node; it != NULL && it->parent != NULL; it = it->parent) { + len = strlen(it->name); + ptr -= len; + + memcpy(ptr, it->name, len); + *(--ptr) = '/'; + } + + return str; +} |