allow for detecting path duplicates with got_pathlist_insert()
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
diff --git a/lib/got_lib_path.h b/lib/got_lib_path.h
index a3451ce..1ceecd5 100644
--- a/lib/got_lib_path.h
+++ b/lib/got_lib_path.h
@@ -77,9 +77,11 @@ TAILQ_HEAD(got_pathlist_head, got_pathlist_entry);
* The caller should already have initialized the list head. This list stores
* the pointer to the path as-is, i.e. the path is not copied internally and
* must remain available until the list is freed with got_pathlist_free().
+ * If the first argument is not NULL, set it to a pointer to the newly inserted
+ * element, or to a NULL pointer in case the path was already on the list.
*/
-const struct got_error *got_pathlist_insert(struct got_pathlist_head *,
- const char *);
+const struct got_error *got_pathlist_insert(struct got_pathlist_entry **,
+ struct got_pathlist_head *, const char *);
/* Free resources allocated for a path list. */
void got_pathlist_free(struct got_pathlist_head *);
diff --git a/lib/path.c b/lib/path.c
index cc15eae..ab84663 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -213,10 +213,14 @@ got_path_cmp(const char *path1, const char *path2)
}
const struct got_error *
-got_pathlist_insert(struct got_pathlist_head *pathlist, const char *path)
+got_pathlist_insert(struct got_pathlist_entry **inserted,
+ struct got_pathlist_head *pathlist, const char *path)
{
struct got_pathlist_entry *new, *pe;
+ if (inserted)
+ *inserted = NULL;
+
new = malloc(sizeof(*new));
if (new == NULL)
return got_error_from_errno();
@@ -237,12 +241,16 @@ got_pathlist_insert(struct got_pathlist_head *pathlist, const char *path)
return NULL;
} else if (cmp < 0) {
TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
+ if (inserted)
+ *inserted = new;
return NULL;
}
pe = TAILQ_PREV(pe, got_pathlist_head, entry);
}
TAILQ_INSERT_HEAD(pathlist, new, entry);
+ if (inserted)
+ *inserted = new;
return NULL;
}
diff --git a/regress/path/path_test.c b/regress/path/path_test.c
index 24bfb41..7587afc 100644
--- a/regress/path/path_test.c
+++ b/regress/path/path_test.c
@@ -142,7 +142,7 @@ path_list(void)
TAILQ_INIT(&paths);
for (i = 0; i < nitems(path_list_input); i++) {
- err = got_pathlist_insert(&paths, path_list_input[i]);
+ err = got_pathlist_insert(NULL, &paths, path_list_input[i]);
if (err) {
test_printf("%s\n", __func__, err->msg);
return 0;
@@ -177,7 +177,7 @@ path_list_reverse_input(void)
TAILQ_INIT(&paths);
for (i = nitems(path_list_input) - 1; i >= 0; i--) {
- err = got_pathlist_insert(&paths, path_list_input[i]);
+ err = got_pathlist_insert(NULL, &paths, path_list_input[i]);
if (err) {
test_printf("%s\n", __func__, err->msg);
return 0;