1e866f279Sdrochner /*
2e866f279Sdrochner * Copyright (c) 1989, 1993
3e866f279Sdrochner * The Regents of the University of California. All rights reserved.
4e866f279Sdrochner *
5e866f279Sdrochner * This code is derived from software contributed to Berkeley by
6e866f279Sdrochner * Roger L. Snyder.
7e866f279Sdrochner *
8e866f279Sdrochner * Redistribution and use in source and binary forms, with or without
9e866f279Sdrochner * modification, are permitted provided that the following conditions
10e866f279Sdrochner * are met:
11e866f279Sdrochner * 1. Redistributions of source code must retain the above copyright
12e866f279Sdrochner * notice, this list of conditions and the following disclaimer.
13e866f279Sdrochner * 2. Redistributions in binary form must reproduce the above copyright
14e866f279Sdrochner * notice, this list of conditions and the following disclaimer in the
15e866f279Sdrochner * documentation and/or other materials provided with the distribution.
16e866f279Sdrochner * 3. Neither the name of the University nor the names of its contributors
17e866f279Sdrochner * may be used to endorse or promote products derived from this software
18e866f279Sdrochner * without specific prior written permission.
19e866f279Sdrochner *
20e866f279Sdrochner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21e866f279Sdrochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22e866f279Sdrochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23e866f279Sdrochner * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24e866f279Sdrochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25e866f279Sdrochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26e866f279Sdrochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27e866f279Sdrochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28e866f279Sdrochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29e866f279Sdrochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30e866f279Sdrochner * SUCH DAMAGE.
31e866f279Sdrochner */
32e866f279Sdrochner
33e866f279Sdrochner #include <sys/cdefs.h>
34e866f279Sdrochner #if defined(LIBC_SCCS) && !defined(lint)
35e866f279Sdrochner #if 0
36e866f279Sdrochner static char sccsid[] = "@(#)lsearch.c 8.1 (Berkeley) 6/4/93";
37e866f279Sdrochner #else
38*9e66e6d7Sabs __RCSID("$NetBSD: lsearch.c,v 1.7 2012/06/25 22:32:45 abs Exp $");
39e866f279Sdrochner #endif
40e866f279Sdrochner #endif /* LIBC_SCCS and not lint */
41e866f279Sdrochner
42e866f279Sdrochner #include <sys/types.h>
43e866f279Sdrochner
44e866f279Sdrochner #include <assert.h>
45e866f279Sdrochner #include <errno.h>
46e866f279Sdrochner #include <string.h>
47e866f279Sdrochner #include <search.h>
48e866f279Sdrochner
4967d513ddSdsl typedef int (*cmp_fn_t)(const void *, const void *);
5067d513ddSdsl static void *linear_base(const void *, void *, size_t *, size_t,
5167d513ddSdsl cmp_fn_t, int);
52e866f279Sdrochner
53e866f279Sdrochner void *
lsearch(const void * key,void * base,size_t * nelp,size_t width,cmp_fn_t compar)54*9e66e6d7Sabs lsearch(const void *key, void *base, size_t *nelp, size_t width,
55*9e66e6d7Sabs cmp_fn_t compar)
56e866f279Sdrochner {
57e866f279Sdrochner
58e866f279Sdrochner _DIAGASSERT(key != NULL);
59e866f279Sdrochner _DIAGASSERT(base != NULL);
60e866f279Sdrochner _DIAGASSERT(compar != NULL);
61e866f279Sdrochner
62e866f279Sdrochner return(linear_base(key, base, nelp, width, compar, 1));
63e866f279Sdrochner }
64e866f279Sdrochner
65e866f279Sdrochner void *
lfind(const void * key,const void * base,size_t * nelp,size_t width,cmp_fn_t compar)66*9e66e6d7Sabs lfind(const void *key, const void *base, size_t *nelp, size_t width,
67*9e66e6d7Sabs cmp_fn_t compar)
68e866f279Sdrochner {
69e866f279Sdrochner
70e866f279Sdrochner _DIAGASSERT(key != NULL);
71e866f279Sdrochner _DIAGASSERT(base != NULL);
72e866f279Sdrochner _DIAGASSERT(compar != NULL);
73e866f279Sdrochner
74ecef4b3dSdrochner return(linear_base(key, __UNCONST(base), nelp, width, compar, 0));
75e866f279Sdrochner }
76e866f279Sdrochner
77e866f279Sdrochner static void *
linear_base(const void * key,void * base,size_t * nelp,size_t width,cmp_fn_t compar,int add_flag)7844551472Smatt linear_base(const void *key, void *base, size_t *nelp, size_t width,
7944551472Smatt cmp_fn_t compar, int add_flag)
80e866f279Sdrochner {
81fce61e52Schristos char *element, *end;
82e866f279Sdrochner
83e866f279Sdrochner _DIAGASSERT(key != NULL);
84e866f279Sdrochner _DIAGASSERT(base != NULL);
85e866f279Sdrochner _DIAGASSERT(compar != NULL);
86e866f279Sdrochner
87fce61e52Schristos end = (char *)base + *nelp * width;
88fce61e52Schristos for (element = (char *)base; element < end; element += width)
89e866f279Sdrochner if (!compar(element, key)) /* key found */
90fce61e52Schristos return element;
91e866f279Sdrochner
92e866f279Sdrochner if (!add_flag) /* key not found */
93e866f279Sdrochner return(NULL);
94e866f279Sdrochner
95e866f279Sdrochner /*
96e866f279Sdrochner * The UNIX System User's Manual, 1986 edition claims that
97e866f279Sdrochner * a NULL pointer is returned by lsearch with errno set
98e866f279Sdrochner * appropriately, if there is not enough room in the table
99e866f279Sdrochner * to add a new item. This can't be done as none of these
100e866f279Sdrochner * routines have any method of determining the size of the
101e866f279Sdrochner * table. This comment isn't in the 1986-87 System V
102e866f279Sdrochner * manual.
103e866f279Sdrochner */
104e866f279Sdrochner ++*nelp;
105fce61e52Schristos memcpy(end, key, width);
106fce61e52Schristos return end;
107e866f279Sdrochner }
108