summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/shared/util.c24
-rw-r--r--src/shared/util.h4
2 files changed, 28 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index 42a2e27308..2f0aba88ae 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6069,3 +6069,27 @@ finish:
return 0;
}
+
+/* hey glibc, APIs with callbacks without a user pointer are so useless */
+void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
+ int (*compar) (const void *, const void *, void *),
+ void *arg) {
+ size_t l, u, idx;
+ const void *p;
+ int comparison;
+
+ l = 0;
+ u = nmemb;
+ while (l < u) {
+ idx = (l + u) / 2;
+ p = (void *)(((const char *) base) + (idx * size));
+ comparison = compar(key, p, arg);
+ if (comparison < 0)
+ u = idx;
+ else if (comparison > 0)
+ l = idx + 1;
+ else
+ return (void *)p;
+ }
+ return NULL;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 77d28751f0..a9c39b867e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -563,3 +563,7 @@ bool filename_is_safe(const char *p);
bool string_is_safe(const char *p);
int parse_timestamp(const char *t, usec_t *usec);
+
+void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
+ int (*compar) (const void *, const void *, void *),
+ void *arg);