Imported Upstream version 1.12.0
[platform/upstream/augeas.git] / src / internal.c
index e149fe6..ef83b71 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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
@@ -52,6 +52,7 @@ int pathjoin(char **path, int nseg, ...) {
             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)
@@ -60,8 +61,10 @@ int pathjoin(char **path, int nseg, ...) {
                 seg += 1;
             strcat(*path, seg);
         } else {
-            if ((*path = malloc(len)) == NULL)
+            if ((*path = malloc(len)) == NULL) {
+                va_end(ap);
                 return -1;
+            }
             strcpy(*path, seg);
         }
     }
@@ -331,14 +334,10 @@ int __aug_close_memstream(struct memstream *ms) {
     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)) {
@@ -348,12 +347,25 @@ char *path_expand(struct tree *tree, const char *ppath) {
         }
     }
 
+    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;
@@ -365,7 +377,7 @@ char *path_expand(struct tree *tree, const char *ppath) {
     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);