1 /* Initial implementation:
2 Copyright (c) 2002 Robert Drehmel
5 As long as the above copyright statement and this notice remain
6 unchanged, you can do what ever you want with this file. */
8 #include <stdint.h> /* for uint8_t */
9 #include <string.h> /* for memcpy () prototype */
11 static void *lwork (const void *, const void *, size_t *, size_t,
12 int (*) (const void *, const void *), int);
15 lsearch (const void *key
, void *base
, size_t *nelp
, size_t width
,
16 int (*compar
) (const void *, const void *))
18 return lwork (key
, base
, nelp
, width
, compar
, 1);
22 lfind (const void *key
, const void *base
, size_t *nelp
, size_t width
,
23 int (*compar
) (const void *, const void *))
25 return lwork (key
, base
, nelp
, width
, compar
, 0);
29 lwork (const void *key
, const void *base
, size_t *nelp
, size_t width
,
30 int (*compar
) (const void *, const void *), int addelem
)
34 /* Cast to an integer value first to avoid the warning for removing
35 'const' via a cast. */
36 ep
= (uint8_t *) (uintptr_t)base
;
37 for (endp
= (uint8_t *) (ep
+ width
* *nelp
); ep
< endp
; ep
+= width
)
38 if (compar (key
, ep
) == 0)
41 /* lfind () shall return when the key was not found. */
45 /* lsearch () adds the key to the end of the table and increments
46 the number of elements. */
47 memcpy (endp
, key
, width
);