summaryrefslogtreecommitdiff
path: root/libc/stdlib/bsearch.c
blob: 6f3817b6079918f399adacfdff840544a881500d (plain)
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
/*
 * 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>

void * bsearch (const void *key, const void *base, size_t num, size_t size,
	         int (*cmp) (const void *, const void *))
{
    int dir;
    size_t a, b, c;
    const void *p;

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

    return NULL;
}