diff options
author | Rich Felker <dalias@aerifal.cx> | 2011-06-25 18:18:57 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2011-06-25 18:18:57 -0400 |
commit | febbd12d00883a716a9edca25011f8aa306b859b (patch) | |
tree | 45d291973571bebe85963add9ba94d4c8556d5e3 /src/search/lsearch.c | |
parent | 49388f3b7b72a1695bef05f64439b602b2e77a53 (diff) | |
download | musl-febbd12d00883a716a9edca25011f8aa306b859b.tar.gz |
XSI search.h API implementation by Szabolcs Nagy
Diffstat (limited to 'src/search/lsearch.c')
-rw-r--r-- | src/search/lsearch.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/search/lsearch.c b/src/search/lsearch.c new file mode 100644 index 00000000..63f31922 --- /dev/null +++ b/src/search/lsearch.c @@ -0,0 +1,31 @@ +#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(p[i], key) == 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(p[i], key) == 0) + return p[i]; + return 0; +} + + |