xref: /openbsd-src/lib/libc/stdlib/lsearch.c (revision 0674b9825d2b89985e4947e13f7ff95932dbab72)
1*0674b982Scheloha /*	$OpenBSD: lsearch.c,v 1.7 2021/12/08 22:06:28 cheloha Exp $	*/
248f0b646Sjsg 
3964aabfcSmillert /*
4964aabfcSmillert  * Copyright (c) 1989, 1993
5964aabfcSmillert  *	The Regents of the University of California.  All rights reserved.
6964aabfcSmillert  *
7964aabfcSmillert  * This code is derived from software contributed to Berkeley by
8964aabfcSmillert  * Roger L. Snyder.
9964aabfcSmillert  *
10964aabfcSmillert  * Redistribution and use in source and binary forms, with or without
11964aabfcSmillert  * modification, are permitted provided that the following conditions
12964aabfcSmillert  * are met:
13964aabfcSmillert  * 1. Redistributions of source code must retain the above copyright
14964aabfcSmillert  *    notice, this list of conditions and the following disclaimer.
15964aabfcSmillert  * 2. Redistributions in binary form must reproduce the above copyright
16964aabfcSmillert  *    notice, this list of conditions and the following disclaimer in the
17964aabfcSmillert  *    documentation and/or other materials provided with the distribution.
186580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
19964aabfcSmillert  *    may be used to endorse or promote products derived from this software
20964aabfcSmillert  *    without specific prior written permission.
21964aabfcSmillert  *
22964aabfcSmillert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23964aabfcSmillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24964aabfcSmillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25964aabfcSmillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26964aabfcSmillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27964aabfcSmillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28964aabfcSmillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29964aabfcSmillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30964aabfcSmillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31964aabfcSmillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32964aabfcSmillert  * SUCH DAMAGE.
33964aabfcSmillert  */
34964aabfcSmillert 
35964aabfcSmillert #include <sys/types.h>
36964aabfcSmillert #include <string.h>
37964aabfcSmillert #include <search.h>
38964aabfcSmillert 
39964aabfcSmillert typedef int (*cmp_fn_t)(const void *, const void *);
40964aabfcSmillert 
41964aabfcSmillert void *
lsearch(const void * key,void * base,size_t * nelp,size_t width,cmp_fn_t compar)42177a7427Smatthew lsearch(const void *key, void *base, size_t *nelp, size_t width,
43964aabfcSmillert     	cmp_fn_t compar)
44964aabfcSmillert {
45*0674b982Scheloha 	void *element = lfind(key, base, nelp, width, compar);
46964aabfcSmillert 
47*0674b982Scheloha 	/*
48*0674b982Scheloha 	 * Use memmove(3) to ensure the key is copied cleanly into the
49*0674b982Scheloha 	 * array, even if the key overlaps with the end of the array.
50*0674b982Scheloha 	 */
51*0674b982Scheloha 	if (element == NULL) {
52*0674b982Scheloha 		element = memmove((char *)base + *nelp * width, key, width);
53*0674b982Scheloha 		*nelp += 1;
54*0674b982Scheloha 	}
55*0674b982Scheloha 	return element;
56964aabfcSmillert }
57964aabfcSmillert 
58964aabfcSmillert void *
lfind(const void * key,const void * base,size_t * nelp,size_t width,cmp_fn_t compar)59964aabfcSmillert lfind(const void *key, const void *base, size_t *nelp, size_t width,
60964aabfcSmillert 	cmp_fn_t compar)
61964aabfcSmillert {
62964aabfcSmillert 	const char *element, *end;
63964aabfcSmillert 
64964aabfcSmillert 	end = (const char *)base + *nelp * width;
65964aabfcSmillert 	for (element = base; element < end; element += width)
66964aabfcSmillert 		if (!compar(key, element))		/* key found */
67964aabfcSmillert 			return((void *)element);
68*0674b982Scheloha 	return NULL;
69964aabfcSmillert }
70*0674b982Scheloha DEF_WEAK(lfind);
71