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.
9 * $FreeBSD: src/lib/libc/stdlib/lsearch.c,v 1.1 2002/10/16 14:29:22 robert Exp $
10 * $DragonFly: src/lib/libc/stdlib/lsearch.c,v 1.1 2008/05/19 10:06:34 corecode Exp $
12 #include <sys/types.h>
14 #define _SEARCH_PRIVATE
16 #include <stdint.h> /* for uint8_t */
17 #include <stdlib.h> /* for NULL */
18 #include <string.h> /* for memcpy() prototype */
20 static void *lwork(const void *, const void *, size_t *, size_t,
21 int (*)(const void *, const void *), int);
23 void *lsearch(const void *key
, void *base
, size_t *nelp
, size_t width
,
24 int (*compar
)(const void *, const void *))
27 return (lwork(key
, base
, nelp
, width
, compar
, 1));
30 void *lfind(const void *key
, const void *base
, size_t *nelp
, size_t width
,
31 int (*compar
)(const void *, const void *))
34 return (lwork(key
, base
, nelp
, width
, compar
, 0));
38 lwork(const void *key
, const void *base
, size_t *nelp
, size_t width
,
39 int (*compar
)(const void *, const void *), int addelem
)
44 * Cast to an integer value first to avoid the warning for removing
47 ep
= (uint8_t *)(uintptr_t)base
;
48 for (endp
= (uint8_t *)(ep
+ width
* *nelp
); ep
< endp
; ep
+= width
) {
49 if (compar(key
, ep
) == 0)
53 /* lfind() shall return when the key was not found. */
58 * lsearch() adds the key to the end of the table and increments
59 * the number of elements.
61 memcpy(endp
, key
, width
);