diff options
author | Kay Sievers <kay@vrfy.org> | 2012-10-22 14:31:46 +0200 |
---|---|---|
committer | Kay Sievers <kay@vrfy.org> | 2012-10-22 14:31:46 +0200 |
commit | a9e12476ed32256690eb801099c41526834b6390 (patch) | |
tree | 73d95988cc9bb4078e2b4c8695be3f3bb9a4c578 /src/shared/util.c | |
parent | 1d870ac769c70bae86fed63d9c5a1c42d2af9da3 (diff) |
util: add (x)bsearch_r(), the missing counterpart of qsort_r()
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 24 |
1 files changed, 24 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; +} |