~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to o/bsearch.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
void *
 
3
bsearch(const void *key, const void *base, size_t nel, size_t keysize, int (*compar)(const void *, const void *))
 
4
{
 
5
  char *beg=base;
 
6
  char *end=base+keysize*(nel-1);
 
7
  char *mid;
 
8
  int cmp,tem;
 
9
  if (nel==0) return 0;
 
10
  cmp=(*compar)(beg,key);
 
11
  if (cmp==0) return beg;
 
12
  if (cmp> 0) return 0;
 
13
  cmp= (*compar)(key,end);
 
14
  if (cmp==0) return end;
 
15
  if (cmp> 0)return 0;
 
16
  /* key is in range from here on */
 
17
 start:
 
18
  if (nel<=2) return 0;
 
19
  tem=nel;
 
20
  nel=nel/2;
 
21
  mid=beg+(nel)*keysize;
 
22
  cmp= (*compar)(key,mid);
 
23
  if (cmp==0) return mid;
 
24
  if (cmp< 0) {end=mid; nel++;
 
25
               goto start;;
 
26
             }
 
27
  beg=mid;
 
28
  nel=tem-(nel);
 
29
  goto start;
 
30
}