summaryrefslogtreecommitdiff
path: root/lib/sqfs/dir_reader/get_path.c
blob: bdd8317b8f405b0d506e88888255efadc1b963c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * get_path.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "internal.h"

#include <string.h>
#include <stdlib.h>

char *sqfs_tree_node_get_path(const sqfs_tree_node_t *node)
{
	const sqfs_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((const char *)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((const char *)it->name);
		ptr -= len;

		memcpy(ptr, (const char *)it->name, len);
		*(--ptr) = '/';
	}

	return str;
}