ack/lang/cem/libcc.ansi/core/stdlib/bsearch.c
David Given d1cdb07719 Realise that the libc core can safely call other libc core functions, even if
they're not defined in the core: so putw() can call stdio stuff, for example.
So the earlier concept of pureness isn't necessary. Rename accordingly.
2018-06-21 23:24:23 +02:00

31 lines
695 B
C

/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Id$ */
#include <stdlib.h>
void* bsearch(register const void* key, register const void* base,
register size_t nmemb, register size_t size,
int (*compar)(const void*, const void*))
{
register const void* mid_point;
register int cmp;
while (nmemb > 0)
{
mid_point = (char*)base + size * (nmemb >> 1);
if ((cmp = (*compar)(key, mid_point)) == 0)
return (void*)mid_point;
if (cmp >= 0)
{
base = (char*)mid_point + size;
nmemb = (nmemb - 1) >> 1;
}
else
nmemb >>= 1;
}
return (void*)NULL;
}