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