Cygwin: (mostly) drop NT4 and Samba < 3.0 support
[newlib-cygwin.git] / winsup / cygwin / lsearch.cc
blob9e2d93b7775fc9c5cc64c03bc1f039914a06ec35
1 /* Initial implementation:
2 Copyright (c) 2002 Robert Drehmel
3 All rights reserved.
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);
14 extern "C" void *
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);
21 extern "C" void *
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);
28 static void *
29 lwork (const void *key, const void *base, size_t *nelp, size_t width,
30 int (*compar) (const void *, const void *), int addelem)
32 uint8_t *ep, *endp;
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)
39 return ep;
41 /* lfind () shall return when the key was not found. */
42 if (!addelem)
43 return NULL;
45 /* lsearch () adds the key to the end of the table and increments
46 the number of elements. */
47 memcpy (endp, key, width);
48 ++*nelp;
50 return endp;