summaryrefslogblamecommitdiff
path: root/src/search/lsearch.c
blob: 5eb5cc2bd5bc4e8240dbeb9fb877209917f52283 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                                      
                                           












                                                                
                                           




                                    
#include <search.h>
#include <string.h>

void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
	int (*compar)(const void *, const void *))
{
	char (*p)[width] = base;
	size_t n = *nelp;
	size_t i;

	for (i = 0; i < n; i++)
		if (compar(key, p[i]) == 0)
			return p[i];
	*nelp = n+1;
	return memcpy(p[n], key, width);
}

void *lfind(const void *key, const void *base, size_t *nelp,
	size_t width, int (*compar)(const void *, const void *))
{
	char (*p)[width] = (void *)base;
	size_t n = *nelp;
	size_t i;

	for (i = 0; i < n; i++)
		if (compar(key, p[i]) == 0)
			return p[i];
	return 0;
}