xref: /minix3/lib/libc/stdlib/lsearch.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
12fe8fb19SBen Gras /*
22fe8fb19SBen Gras  * Copyright (c) 1989, 1993
32fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
42fe8fb19SBen Gras  *
52fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
62fe8fb19SBen Gras  * Roger L. Snyder.
72fe8fb19SBen Gras  *
82fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras  * are met:
112fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
172fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
182fe8fb19SBen Gras  *    without specific prior written permission.
192fe8fb19SBen Gras  *
202fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
212fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
242fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302fe8fb19SBen Gras  * SUCH DAMAGE.
312fe8fb19SBen Gras  */
322fe8fb19SBen Gras 
332fe8fb19SBen Gras #include <sys/cdefs.h>
342fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
352fe8fb19SBen Gras #if 0
362fe8fb19SBen Gras static char sccsid[] = "@(#)lsearch.c	8.1 (Berkeley) 6/4/93";
372fe8fb19SBen Gras #else
38*f14fb602SLionel Sambuc __RCSID("$NetBSD: lsearch.c,v 1.7 2012/06/25 22:32:45 abs Exp $");
392fe8fb19SBen Gras #endif
402fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
412fe8fb19SBen Gras 
422fe8fb19SBen Gras #include <sys/types.h>
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <assert.h>
452fe8fb19SBen Gras #include <errno.h>
462fe8fb19SBen Gras #include <string.h>
472fe8fb19SBen Gras #include <search.h>
482fe8fb19SBen Gras 
49*f14fb602SLionel Sambuc typedef int (*cmp_fn_t)(const void *, const void *);
50*f14fb602SLionel Sambuc static void *linear_base(const void *, void *, size_t *, size_t,
51*f14fb602SLionel Sambuc 			     cmp_fn_t, int);
522fe8fb19SBen Gras 
532fe8fb19SBen Gras void *
lsearch(const void * key,void * base,size_t * nelp,size_t width,cmp_fn_t compar)54*f14fb602SLionel Sambuc lsearch(const void *key, void *base, size_t *nelp, size_t width,
55*f14fb602SLionel Sambuc     cmp_fn_t compar)
562fe8fb19SBen Gras {
572fe8fb19SBen Gras 
582fe8fb19SBen Gras 	_DIAGASSERT(key != NULL);
592fe8fb19SBen Gras 	_DIAGASSERT(base != NULL);
602fe8fb19SBen Gras 	_DIAGASSERT(compar != NULL);
612fe8fb19SBen Gras 
622fe8fb19SBen Gras 	return(linear_base(key, base, nelp, width, compar, 1));
632fe8fb19SBen Gras }
642fe8fb19SBen Gras 
652fe8fb19SBen Gras void *
lfind(const void * key,const void * base,size_t * nelp,size_t width,cmp_fn_t compar)66*f14fb602SLionel Sambuc lfind(const void *key, const void *base, size_t *nelp, size_t width,
67*f14fb602SLionel Sambuc     cmp_fn_t compar)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras 
702fe8fb19SBen Gras 	_DIAGASSERT(key != NULL);
712fe8fb19SBen Gras 	_DIAGASSERT(base != NULL);
722fe8fb19SBen Gras 	_DIAGASSERT(compar != NULL);
732fe8fb19SBen Gras 
742fe8fb19SBen Gras 	return(linear_base(key, __UNCONST(base), nelp, width, compar, 0));
752fe8fb19SBen Gras }
762fe8fb19SBen Gras 
772fe8fb19SBen Gras static void *
linear_base(const void * key,void * base,size_t * nelp,size_t width,cmp_fn_t compar,int add_flag)78*f14fb602SLionel Sambuc linear_base(const void *key, void *base, size_t *nelp, size_t width,
79*f14fb602SLionel Sambuc 	cmp_fn_t compar, int add_flag)
802fe8fb19SBen Gras {
812fe8fb19SBen Gras 	char *element, *end;
822fe8fb19SBen Gras 
832fe8fb19SBen Gras 	_DIAGASSERT(key != NULL);
842fe8fb19SBen Gras 	_DIAGASSERT(base != NULL);
852fe8fb19SBen Gras 	_DIAGASSERT(compar != NULL);
862fe8fb19SBen Gras 
872fe8fb19SBen Gras 	end = (char *)base + *nelp * width;
882fe8fb19SBen Gras 	for (element = (char *)base; element < end; element += width)
892fe8fb19SBen Gras 		if (!compar(element, key))		/* key found */
902fe8fb19SBen Gras 			return element;
912fe8fb19SBen Gras 
922fe8fb19SBen Gras 	if (!add_flag)					/* key not found */
932fe8fb19SBen Gras 		return(NULL);
942fe8fb19SBen Gras 
952fe8fb19SBen Gras 	/*
962fe8fb19SBen Gras 	 * The UNIX System User's Manual, 1986 edition claims that
972fe8fb19SBen Gras 	 * a NULL pointer is returned by lsearch with errno set
982fe8fb19SBen Gras 	 * appropriately, if there is not enough room in the table
992fe8fb19SBen Gras 	 * to add a new item.  This can't be done as none of these
1002fe8fb19SBen Gras 	 * routines have any method of determining the size of the
1012fe8fb19SBen Gras 	 * table.  This comment isn't in the 1986-87 System V
1022fe8fb19SBen Gras 	 * manual.
1032fe8fb19SBen Gras 	 */
1042fe8fb19SBen Gras 	++*nelp;
1052fe8fb19SBen Gras 	memcpy(end, key, width);
1062fe8fb19SBen Gras 	return end;
1072fe8fb19SBen Gras }
108