- Don't rely on the default calling convention being cdecl for function pointers
[reactos.git] / reactos / lib / sdk / crt / search / bsearch.c
1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <stdlib.h>
3
4 /*
5 * @implemented
6 */
7 void *
8 bsearch(const void *key, const void *base0, size_t nelem,
9 size_t size, int (__cdecl *cmp)(const void *ck, const void *ce))
10 {
11 char *base = (char *)base0;
12 int lim, cmpval;
13 void *p;
14
15 for (lim = nelem; lim != 0; lim >>= 1)
16 {
17 p = base + (lim >> 1) * size;
18 cmpval = (*cmp)(key, p);
19 if (cmpval == 0)
20 return p;
21 if (cmpval > 0)
22 { /* key > p: move right */
23 base = (char *)p + size;
24 lim--;
25 } /* else move left */
26 }
27 return 0;
28 }