summaryrefslogtreecommitdiff
path: root/libc/stdlib/bsearch.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-06-18 07:47:58 +0000
committerEric Andersen <andersen@codepoet.org>2002-06-18 07:47:58 +0000
commit8cd8f955387a7e0c9df9bb302e2a6b807baf39e5 (patch)
treef8825310dd87cbabde2a2167d72a2e4703f45564 /libc/stdlib/bsearch.c
parentadbc99596b067c7ff47dabe44473cd5717bb2cab (diff)
Rework and kill pointless static variable
-Erik
Diffstat (limited to 'libc/stdlib/bsearch.c')
-rw-r--r--libc/stdlib/bsearch.c53
1 files changed, 25 insertions, 28 deletions
diff --git a/libc/stdlib/bsearch.c b/libc/stdlib/bsearch.c
index c5a78959a..6f3817b60 100644
--- a/libc/stdlib/bsearch.c
+++ b/libc/stdlib/bsearch.c
@@ -1,42 +1,39 @@
-
/*
- * This file lifted in toto from 'Dlibs' on the atari ST (RdeBath)
+ * This file originally lifted in toto from 'Dlibs' on the atari ST (RdeBath)
*
*
* Dale Schumacher 399 Beacon Ave.
* (alias: Dalnefre') St. Paul, MN 55104
* dal@syntel.UUCP United States of America
* "It's not reality that's important, but how you perceive things."
+ *
+ * Reworked by Erik Andersen <andersen@uclibc.org>
*/
#include <stdio.h>
-static int _bsearch; /* index of element found, or where to
-
- * insert */
-
-char *bsearch(key, base, num, size, cmp)
-register char *key; /* item to search for */
-register char *base; /* base address */
-int num; /* number of elements */
-register int size; /* element size in bytes */
-register int (*cmp) (); /* comparison function */
+void * bsearch (const void *key, const void *base, size_t num, size_t size,
+ int (*cmp) (const void *, const void *))
{
- register int a, b, c, dir;
+ int dir;
+ size_t a, b, c;
+ const void *p;
- a = 0;
- b = num - 1;
- while (a <= b) {
- c = (a + b) >> 1; /* == ((a + b) / 2) */
- if ((dir = (*cmp) (key, (base + (c * size))))) {
- if (dir < 0)
- b = c - 1;
- else /* (dir > 0) */
- a = c + 1;
- } else {
- _bsearch = c;
- return (base + (c * size));
- }
+ a = 0;
+ b = num;
+ while (a < b)
+ {
+ c = (a + b) >> 1; /* == ((a + b) / 2) */
+ p = (void *)(((const char *) base) + (c * size));
+ dir = (*cmp)(key, p);
+ if (dir < 0) {
+ b = c;
+ } else if (dir > 0) {
+ a = c + 1;
+ } else {
+ return (void *)p;
}
- _bsearch = b;
- return (NULL);
+ }
+
+ return NULL;
}
+