From 0742ee3943192434d69092f11946f791f0f25502 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 10 Jun 2019 22:41:34 +0200 Subject: Add generic function to produce a full path from an fstree node Signed-off-by: David Oberhollenzer --- lib/fstree/get_path.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/fstree/get_path.c (limited to 'lib/fstree/get_path.c') 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 +#include + +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; +} -- cgit v1.2.3