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