#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: tfind.c,v 1.7 2012/06/25 22:32:45 abs Exp $");
#endif
#include <assert.h>
#define _SEARCH_PRIVATE
#include <stdlib.h>
#include <search.h>
void *
tfind(const void *vkey, void * const *vrootp,
int (*compar)(const void *, const void *))
{
node_t * const *rootp = (node_t * const*)vrootp;
_DIAGASSERT(vkey != NULL);
_DIAGASSERT(compar != NULL);
if (rootp == NULL)
return NULL;
while (*rootp != NULL) {
int r;
if ((r = (*compar)(vkey, (*rootp)->key)) == 0)
return *rootp;
rootp = (r < 0) ?
&(*rootp)->llink :
&(*rootp)->rlink;
}
return NULL;
}