/*
* internal.c: internal data structures and helpers
*
- * Copyright (C) 2007-2011 David Lutterkort
+ * Copyright (C) 2007-2016 David Lutterkort
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
len += strlen(*path) + 1;
if (REALLOC_N(*path, len) == -1) {
FREE(*path);
+ va_end(ap);
return -1;
}
if (strlen(*path) == 0 || (*path)[strlen(*path)-1] != SEP)
seg += 1;
strcat(*path, seg);
} else {
- if ((*path = malloc(len)) == NULL)
+ if ((*path = malloc(len)) == NULL) {
+ va_end(ap);
return -1;
+ }
strcpy(*path, seg);
}
}
return 0;
}
-char *path_expand(struct tree *tree, const char *ppath) {
+int tree_sibling_index(struct tree *tree) {
struct tree *siblings = tree->parent->children;
- char *path;
- const char *label;
- char *escaped = NULL;
-
- int cnt = 0, ind = 0, r;
+ int cnt = 0, ind = 0;
list_for_each(t, siblings) {
if (streqv(t->label, tree->label)) {
}
}
+ if (cnt > 1) {
+ return ind;
+ } else {
+ return 0;
+ }
+}
+
+char *path_expand(struct tree *tree, const char *ppath) {
+ char *path;
+ const char *label;
+ char *escaped = NULL;
+ int r;
+
+ int ind = tree_sibling_index(tree);
+
if (ppath == NULL)
ppath = "";
- if (tree == NULL)
- label = "(no_tree)";
- else if (tree->label == NULL)
+ if (tree->label == NULL)
label = "(none)";
else
label = tree->label;
if (escaped != NULL)
label = escaped;
- if (cnt > 1) {
+ if (ind > 0) {
r = asprintf(&path, "%s/%s[%d]", ppath, label, ind);
} else {
r = asprintf(&path, "%s/%s", ppath, label);