diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-10 22:41:34 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-10 22:41:34 +0200 |
commit | 0742ee3943192434d69092f11946f791f0f25502 (patch) | |
tree | 64a88d77e911d040c0b24741a4d6506e0cf37707 /lib/fstree/get_path.c | |
parent | 69de75de97c17ef7370b26944ee286e4b3dc6266 (diff) |
Add generic function to produce a full path from an fstree node
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
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; +} |