diff options
author | robert <robert@FreeBSD.org> | 2002-10-16 14:29:23 +0000 |
---|---|---|
committer | robert <robert@FreeBSD.org> | 2002-10-16 14:29:23 +0000 |
commit | 98e716a4fe3c11b8fa3a358acb490ca6d58d7ff6 (patch) | |
tree | 145e091db92040b76b92fbd0f0dddfd41d3cd87a /lib/libc/stdlib/lsearch.c | |
parent | 9782655096a5873d6e6f26617941855764223d29 (diff) | |
download | FreeBSD-src-98e716a4fe3c11b8fa3a358acb490ca6d58d7ff6.zip FreeBSD-src-98e716a4fe3c11b8fa3a358acb490ca6d58d7ff6.tar.gz |
- Remove the lsearch() and lfind() functions and their manpage from
the compatibility library libcompat.
- Add new implementations of lsearch() and lfind() which conform to
IEEE Std 1003.1-2001 to libc. Add a new manual page for them and
add them to the makefile.
- Add function prototypes for lsearch() and lfind() to the search.h
header.
Diffstat (limited to 'lib/libc/stdlib/lsearch.c')
-rw-r--r-- | lib/libc/stdlib/lsearch.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/libc/stdlib/lsearch.c b/lib/libc/stdlib/lsearch.c new file mode 100644 index 0000000..e4d1dd5 --- /dev/null +++ b/lib/libc/stdlib/lsearch.c @@ -0,0 +1,64 @@ +/* + * Initial implementation: + * Copyright (c) 2002 Robert Drehmel + * All rights reserved. + * + * As long as the above copyright statement and this notice remain + * unchanged, you can do what ever you want with this file. + */ +#include <sys/types.h> +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#define _SEARCH_PRIVATE +#include <search.h> +#include <stdint.h> /* for uint8_t */ +#include <stdlib.h> /* for NULL */ +#include <string.h> /* for memcpy() prototype */ + +static void *lwork(const void *, const void *, size_t *, size_t, + int (*)(const void *, const void *), int); + +void *lsearch(const void *key, void *base, size_t *nelp, size_t width, + int (*compar)(const void *, const void *)) +{ + + return (lwork(key, base, nelp, width, compar, 1)); +} + +void *lfind(const void *key, const void *base, size_t *nelp, size_t width, + int (*compar)(const void *, const void *)) +{ + + return (lwork(key, base, nelp, width, compar, 0)); +} + +static void * +lwork(const void *key, const void *base, size_t *nelp, size_t width, + int (*compar)(const void *, const void *), int addelem) +{ + uint8_t *ep, *endp; + + /* + * Cast to an integer value first to avoid the warning for removing + * 'const' via a cast. + */ + ep = (uint8_t *)(uintptr_t)base; + for (endp = (uint8_t *)(ep + width * *nelp); ep < endp; ep += width) { + if (compar(key, ep) == 0) + return (ep); + } + + /* lfind() shall return when the key was not found. */ + if (!addelem) + return (NULL); + + /* + * lsearch() adds the key to the end of the table and increments + * the number of elements. + */ + memcpy(endp, key, width); + ++*nelp; + + return (endp); +} |