#include <sys/types.h>
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/stdlib/lsearch.c,v 1.1 2002/10/16 14:29:22 robert Exp $");
#define _SEARCH_PRIVATE
#include <search.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
static void *lwork(const void *, const void *, size_t *, size_t,
int (*)(const void *, const void *), int);
void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *))
{
return (lwork(key, base, nelp, width, compar, 1));
}
void *lfind(const void *key, const void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *))
{
return (lwork(key, base, nelp, width, compar, 0));
}
static void *
lwork(const void *key, const void *base, size_t *nelp, size_t width,
int (*compar)(const void *, const void *), int addelem)
{
uint8_t *ep, *endp;
ep = (uint8_t *)(uintptr_t)base;
for (endp = (uint8_t *)(ep + width * *nelp); ep < endp; ep += width) {
if (compar(key, ep) == 0)
return (ep);
}
if (!addelem)
return (NULL);
memcpy(endp, key, width);
++*nelp;
return (endp);
}