1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 09:21:26 +03:00

util: add (x)bsearch_r(), the missing counterpart of qsort_r()

This commit is contained in:
Kay Sievers 2012-10-22 14:31:46 +02:00
parent 1d870ac769
commit a9e12476ed
2 changed files with 28 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);