Home | History | Annotate | Line # | Download | only in stdlib
lsearch.c revision 1.5.4.2
      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.4.2      yamt __RCSID("$NetBSD: lsearch.c,v 1.5.4.2 2012/10/30 18:58:59 yamt 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.5.4.2      yamt lsearch(const void *key, void *base, size_t *nelp, size_t width,
     55  1.5.4.2      yamt     cmp_fn_t compar)
     56      1.1  drochner {
     57      1.1  drochner 
     58      1.1  drochner 	_DIAGASSERT(key != NULL);
     59      1.1  drochner 	_DIAGASSERT(base != NULL);
     60      1.1  drochner 	_DIAGASSERT(compar != NULL);
     61      1.1  drochner 
     62      1.1  drochner 	return(linear_base(key, base, nelp, width, compar, 1));
     63      1.1  drochner }
     64      1.1  drochner 
     65      1.1  drochner void *
     66  1.5.4.2      yamt lfind(const void *key, const void *base, size_t *nelp, size_t width,
     67  1.5.4.2      yamt     cmp_fn_t compar)
     68      1.1  drochner {
     69      1.1  drochner 
     70      1.1  drochner 	_DIAGASSERT(key != NULL);
     71      1.1  drochner 	_DIAGASSERT(base != NULL);
     72      1.1  drochner 	_DIAGASSERT(compar != NULL);
     73      1.1  drochner 
     74      1.2  drochner 	return(linear_base(key, __UNCONST(base), nelp, width, compar, 0));
     75      1.1  drochner }
     76      1.1  drochner 
     77      1.1  drochner static void *
     78  1.5.4.1      yamt linear_base(const void *key, void *base, size_t *nelp, size_t width,
     79  1.5.4.1      yamt 	cmp_fn_t compar, int add_flag)
     80      1.1  drochner {
     81      1.4  christos 	char *element, *end;
     82      1.1  drochner 
     83      1.1  drochner 	_DIAGASSERT(key != NULL);
     84      1.1  drochner 	_DIAGASSERT(base != NULL);
     85      1.1  drochner 	_DIAGASSERT(compar != NULL);
     86      1.1  drochner 
     87      1.4  christos 	end = (char *)base + *nelp * width;
     88      1.4  christos 	for (element = (char *)base; element < end; element += width)
     89      1.1  drochner 		if (!compar(element, key))		/* key found */
     90      1.4  christos 			return element;
     91      1.1  drochner 
     92      1.1  drochner 	if (!add_flag)					/* key not found */
     93      1.1  drochner 		return(NULL);
     94      1.1  drochner 
     95      1.1  drochner 	/*
     96      1.1  drochner 	 * The UNIX System User's Manual, 1986 edition claims that
     97      1.1  drochner 	 * a NULL pointer is returned by lsearch with errno set
     98      1.1  drochner 	 * appropriately, if there is not enough room in the table
     99      1.1  drochner 	 * to add a new item.  This can't be done as none of these
    100      1.1  drochner 	 * routines have any method of determining the size of the
    101      1.1  drochner 	 * table.  This comment isn't in the 1986-87 System V
    102      1.1  drochner 	 * manual.
    103      1.1  drochner 	 */
    104      1.1  drochner 	++*nelp;
    105      1.4  christos 	memcpy(end, key, width);
    106      1.4  christos 	return end;
    107      1.1  drochner }
    108