* added compilers lcc and bcc (linux86)
[mascara-docs.git] / compilers / linux86-0.16.17 / libc / misc / bsearch.c
blob9898667437067820b87d6006c9d477fe5f76253c
2 /*
3 * This file lifted in toto from 'Dlibs' on the atari ST (RdeBath)
5 *
6 * Dale Schumacher 399 Beacon Ave.
7 * (alias: Dalnefre') St. Paul, MN 55104
8 * dal@syntel.UUCP United States of America
9 * "It's not reality that's important, but how you perceive things."
11 #include <stdio.h>
13 static int _bsearch; /* index of element found, or where to
14 * insert */
16 char *
17 bsearch(key, base, num, size, cmp)
18 register char *key; /* item to search for */
19 register char *base; /* base address */
20 int num; /* number of elements */
21 register int size; /* element size in bytes */
22 register int (*cmp) (); /* comparison function */
24 register int a, b, c, dir;
26 a = 0;
27 b = num - 1;
28 while (a <= b)
30 c = (a + b) >> 1; /* == ((a + b) / 2) */
31 if (dir = (*cmp) ((base + (c * size)), key))
33 if (dir > 0)
34 b = c - 1;
35 else /* (dir < 0) */
36 a = c + 1;
38 else
40 _bsearch = c;
41 return (base + (c * size));
44 _bsearch = b;
45 return (NULL);