1b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes/*	$OpenBSD: lsearch.c,v 1.5 2014/07/18 04:16:09 matthew Exp $	*/
2b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes
37f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes/*
47f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * Copyright (c) 1989, 1993
57f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *	The Regents of the University of California.  All rights reserved.
67f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *
77f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * This code is derived from software contributed to Berkeley by
87f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * Roger L. Snyder.
97f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *
107f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * Redistribution and use in source and binary forms, with or without
117f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * modification, are permitted provided that the following conditions
127f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * are met:
137f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * 1. Redistributions of source code must retain the above copyright
147f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    notice, this list of conditions and the following disclaimer.
157f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
167f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
177f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    documentation and/or other materials provided with the distribution.
187f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * 3. Neither the name of the University nor the names of its contributors
197f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    may be used to endorse or promote products derived from this software
207f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *    without specific prior written permission.
217f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes *
227f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
237f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
247f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
257f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
267f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
277f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
287f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
297f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
307f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
317f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
327f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes * SUCH DAMAGE.
337f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes */
347f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
357f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <sys/types.h>
367f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <string.h>
377f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes#include <search.h>
387f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
397f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughestypedef int (*cmp_fn_t)(const void *, const void *);
40b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughesstatic void *linear_base(const void *, const void *, size_t *, size_t,
41b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes    cmp_fn_t, int);
427f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
437f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesvoid *
447f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hugheslsearch(const void *key, void *base, size_t *nelp, size_t width,
45b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes    	cmp_fn_t compar)
467f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes{
477f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
487f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	return(linear_base(key, base, nelp, width, compar, 1));
497f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes}
507f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
517f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesvoid *
527f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hugheslfind(const void *key, const void *base, size_t *nelp, size_t width,
53b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	cmp_fn_t compar)
547f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes{
55b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	return(linear_base(key, base, nelp, width, compar, 0));
567f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes}
577f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
587f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughesstatic void *
59b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hugheslinear_base(const void *key, const void *base, size_t *nelp, size_t width,
607f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	cmp_fn_t compar, int add_flag)
617f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes{
62b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	const char *element, *end;
637f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
64b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	end = (const char *)base + *nelp * width;
65b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	for (element = base; element < end; element += width)
66b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes		if (!compar(key, element))		/* key found */
67b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes			return((void *)element);
687f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
697f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	if (!add_flag)					/* key not found */
707f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes		return(NULL);
717f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes
727f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	/*
737f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * The UNIX System User's Manual, 1986 edition claims that
747f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * a NULL pointer is returned by lsearch with errno set
757f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * appropriately, if there is not enough room in the table
767f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * to add a new item.  This can't be done as none of these
777f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * routines have any method of determining the size of the
787f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * table.  This comment isn't in the 1986-87 System V
797f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 * manual.
807f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	 */
817f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes	++*nelp;
82b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	memcpy((void *)end, key, width);
83b902641d7303d2ea24c10f6d6e7ff49e7ee75611Elliott Hughes	return((void *)end);
847f3a272ae327c647db4caeaea2a3c7af50bd73b5Elliott Hughes}
85