#include <sys/cdefs.h>
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: tsearch.c,v 1.3 1999/09/16 11:45:37 lukem Exp $");
#endif
#endif
__FBSDID("$FreeBSD: src/lib/libc/stdlib/tsearch.c,v 1.4 2003/01/05 02:43:18 tjr Exp $");
#define _SEARCH_PRIVATE
#include <search.h>
#include <stdlib.h>
void *
tsearch(vkey, vrootp, compar)
const void *vkey;
void **vrootp;
int (*compar)(const void *, const void *);
{
node_t *q;
node_t **rootp = (node_t **)vrootp;
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;
}
q = malloc(sizeof(node_t));
if (q != 0) {
*rootp = q;
q->key = (void *)vkey;
q->llink = q->rlink = NULL;
}
return q;
}