lsearch.c revision 7f3a272ae327c647db4caeaea2a3c7af50bd73b5
17f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes/*
27f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * Copyright (c) 1989, 1993
37f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *	The Regents of the University of California.  All rights reserved.
47f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *
57f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * This code is derived from software contributed to Berkeley by
67f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * Roger L. Snyder.
77f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *
87f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * Redistribution and use in source and binary forms, with or without
97f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * modification, are permitted provided that the following conditions
107f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * are met:
117f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * 1. Redistributions of source code must retain the above copyright
127f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    notice, this list of conditions and the following disclaimer.
137f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
147f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
157f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    documentation and/or other materials provided with the distribution.
167f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * 3. Neither the name of the University nor the names of its contributors
177f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    may be used to endorse or promote products derived from this software
187f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    without specific prior written permission.
197f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *
207f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
217f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
227f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
237f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
247f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
257f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
267f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
277f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
287f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
297f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
307f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * SUCH DAMAGE.
317f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes */
327f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
337f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <sys/cdefs.h>
347f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#if defined(LIBC_SCCS) && !defined(lint)
357f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#if 0
367f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesstatic char sccsid[] = "@(#)lsearch.c	8.1 (Berkeley) 6/4/93";
377f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#else
387f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes__RCSID("$NetBSD: lsearch.c,v 1.7 2012/06/25 22:32:45 abs Exp $");
397f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#endif
407f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#endif /* LIBC_SCCS and not lint */
417f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
427f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <sys/types.h>
437f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
447f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <assert.h>
457f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <errno.h>
467f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <string.h>
477f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <search.h>
487f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
497f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughestypedef int (*cmp_fn_t)(const void *, const void *);
507f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesstatic void *linear_base(const void *, void *, size_t *, size_t,
517f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes			     cmp_fn_t, int);
527f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
537f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesvoid *
547f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hugheslsearch(const void *key, void *base, size_t *nelp, size_t width,
557f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes    cmp_fn_t compar)
567f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes{
577f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
587f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(key != NULL);
597f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(base != NULL);
607f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(compar != NULL);
617f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
627f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	return(linear_base(key, base, nelp, width, compar, 1));
637f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes}
647f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
657f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesvoid *
667f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hugheslfind(const void *key, const void *base, size_t *nelp, size_t width,
677f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes    cmp_fn_t compar)
687f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes{
697f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
707f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(key != NULL);
717f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(base != NULL);
727f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(compar != NULL);
737f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
747f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	return(linear_base(key, __UNCONST(base), nelp, width, compar, 0));
757f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes}
767f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
777f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesstatic void *
787f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hugheslinear_base(const void *key, void *base, size_t *nelp, size_t width,
797f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	cmp_fn_t compar, int add_flag)
807f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes{
817f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	char *element, *end;
827f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
837f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(key != NULL);
847f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(base != NULL);
857f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	_DIAGASSERT(compar != NULL);
867f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
877f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	end = (char *)base + *nelp * width;
887f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	for (element = (char *)base; element < end; element += width)
897f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes		if (!compar(element, key))		/* key found */
907f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes			return element;
917f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
927f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	if (!add_flag)					/* key not found */
937f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes		return(NULL);
947f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
957f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	/*
967f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * The UNIX System User's Manual, 1986 edition claims that
977f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * a NULL pointer is returned by lsearch with errno set
987f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * appropriately, if there is not enough room in the table
997f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * to add a new item.  This can't be done as none of these
1007f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * routines have any method of determining the size of the
1017f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * table.  This comment isn't in the 1986-87 System V
1027f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * manual.
1037f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 */
1047f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	++*nelp;
1057f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	memcpy(end, key, width);
1067f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	return end;
1077f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes}
108