2 * Initial implementation:
3 * Copyright (c) 2002 Robert Drehmel
6 * As long as the above copyright statement and this notice remain
7 * unchanged, you can do what ever you want with this file.
10 #define _SEARCH_PRIVATE
12 #include <stdint.h> /* for uint8_t */
13 #include <stdlib.h> /* for NULL */
14 #include <string.h> /* for memcpy() prototype */
16 static void *lwork(const void *, const void *, size_t *, size_t,
17 int (*)(const void *, const void *), int);
19 void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
20 int (*compar)(const void *, const void *))
23 return (lwork(key, base, nelp, width, compar, 1));
26 void *lfind(const void *key, const void *base, size_t *nelp, size_t width,
27 int (*compar)(const void *, const void *))
30 return (lwork(key, base, nelp, width, compar, 0));
34 lwork(const void *key, const void *base, size_t *nelp, size_t width,
35 int (*compar)(const void *, const void *), int addelem)
39 ep = __DECONST(uint8_t *, base);
40 for (endp = (uint8_t *)(ep + width * *nelp); ep < endp; ep += width) {
41 if (compar(key, ep) == 0)
45 /* lfind() shall return when the key was not found. */
50 * lsearch() adds the key to the end of the table and increments
51 * the number of elements.
53 memcpy(endp, key, width);