139298Sbostic /*
2*61241Sbostic * Copyright (c) 1989, 1993
3*61241Sbostic * The Regents of the University of California. All rights reserved.
439298Sbostic *
539298Sbostic * This code is derived from software contributed to Berkeley by
639298Sbostic * Roger L. Snyder.
739298Sbostic *
842629Sbostic * %sccs.include.redist.c%
939298Sbostic */
1039298Sbostic
1139298Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61241Sbostic static char sccsid[] = "@(#)lsearch.c 8.1 (Berkeley) 06/04/93";
1339298Sbostic #endif /* LIBC_SCCS and not lint */
1439298Sbostic
1539298Sbostic #include <sys/types.h>
1639298Sbostic #include <unistd.h>
1739298Sbostic
1854675Storek static char *linear_base();
1939298Sbostic
2039298Sbostic char *
lsearch(key,base,nelp,width,compar)2139298Sbostic lsearch(key, base, nelp, width, compar)
2239298Sbostic char *key, *base;
2339298Sbostic u_int *nelp, width;
2439298Sbostic int (*compar)();
2539298Sbostic {
2639298Sbostic return(linear_base(key, base, nelp, width, compar, 1));
2739298Sbostic }
2839298Sbostic
2939298Sbostic char *
lfind(key,base,nelp,width,compar)3039298Sbostic lfind(key, base, nelp, width, compar)
3139298Sbostic char *key, *base;
3239298Sbostic u_int *nelp, width;
3339298Sbostic int (*compar)();
3439298Sbostic {
3539298Sbostic return(linear_base(key, base, nelp, width, compar, 0));
3639298Sbostic }
3739298Sbostic
3839298Sbostic static char *
linear_base(key,base,nelp,width,compar,add_flag)3939298Sbostic linear_base(key, base, nelp, width, compar, add_flag)
4039298Sbostic char *key, *base;
4139298Sbostic u_int *nelp, width;
4239298Sbostic int (*compar)(), add_flag;
4339298Sbostic {
4439298Sbostic register char *element, *end;
4539298Sbostic
4639298Sbostic end = base + *nelp * width;
4739298Sbostic for (element = base; element < end; element += width)
4839298Sbostic if (!compar(element, key)) /* key found */
4939298Sbostic return(element);
5039298Sbostic
5139298Sbostic if (!add_flag) /* key not found */
5239298Sbostic return(NULL);
5339298Sbostic
5439298Sbostic /*
5539298Sbostic * The UNIX System User's Manual, 1986 edition claims that
5639298Sbostic * a NULL pointer is returned by lsearch with errno set
5739298Sbostic * appropriately, if there is not enough room in the table
5839298Sbostic * to add a new item. This can't be done as none of these
5939298Sbostic * routines have any method of determining the size of the
6039298Sbostic * table. This comment was isn't in the 1986-87 System V
6139298Sbostic * manual.
6239298Sbostic */
6339298Sbostic ++*nelp;
6439298Sbostic bcopy(key, end, (int)width);
6539298Sbostic return(end);
6639298Sbostic }
67