Home | History | Annotate | Line # | Download | only in stdlib
lsearch.c revision 1.1
      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.1  drochner __RCSID("$NetBSD: lsearch.c,v 1.1 2005/07/06 14:43:24 drochner 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.1  drochner typedef int (*cmp_fn_t) __P((const void *, const void *));
     50  1.1  drochner static void *linear_base __P((const void *, const void *, size_t *, size_t,
     51  1.1  drochner 			     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.1  drochner 	const void *key, *base;
     56  1.1  drochner 	size_t *nelp, width;
     57  1.1  drochner 	cmp_fn_t compar;
     58  1.1  drochner {
     59  1.1  drochner 
     60  1.1  drochner 	_DIAGASSERT(key != NULL);
     61  1.1  drochner 	_DIAGASSERT(base != NULL);
     62  1.1  drochner 	_DIAGASSERT(compar != NULL);
     63  1.1  drochner 
     64  1.1  drochner 	return(linear_base(key, base, nelp, width, compar, 1));
     65  1.1  drochner }
     66  1.1  drochner 
     67  1.1  drochner void *
     68  1.1  drochner lfind(key, base, nelp, width, compar)
     69  1.1  drochner 	const void *key, *base;
     70  1.1  drochner 	size_t *nelp, width;
     71  1.1  drochner 	cmp_fn_t compar;
     72  1.1  drochner {
     73  1.1  drochner 
     74  1.1  drochner 	_DIAGASSERT(key != NULL);
     75  1.1  drochner 	_DIAGASSERT(base != NULL);
     76  1.1  drochner 	_DIAGASSERT(compar != NULL);
     77  1.1  drochner 
     78  1.1  drochner 	return(linear_base(key, base, nelp, width, compar, 0));
     79  1.1  drochner }
     80  1.1  drochner 
     81  1.1  drochner static void *
     82  1.1  drochner linear_base(key, base, nelp, width, compar, add_flag)
     83  1.1  drochner 	const void *key, *base;
     84  1.1  drochner 	size_t *nelp, width;
     85  1.1  drochner 	cmp_fn_t compar;
     86  1.1  drochner 	int add_flag;
     87  1.1  drochner {
     88  1.1  drochner 	char *element, *end;
     89  1.1  drochner 
     90  1.1  drochner 	_DIAGASSERT(key != NULL);
     91  1.1  drochner 	_DIAGASSERT(base != NULL);
     92  1.1  drochner 	_DIAGASSERT(compar != NULL);
     93  1.1  drochner 
     94  1.1  drochner 	/* LINTED const castaway */
     95  1.1  drochner 	end = (char *)base + *nelp * width;
     96  1.1  drochner 	/* LINTED const castaway */
     97  1.1  drochner 	for (element = (char *)base; element < end; element += width)
     98  1.1  drochner 		if (!compar(element, key))		/* key found */
     99  1.1  drochner 			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.1  drochner 	memcpy(end, key, width);
    115  1.1  drochner 	return end;
    116  1.1  drochner }
    117