date and time created 90/05/03 17:47:27 by bostic
[unix-history] / usr / src / lib / libcompat / 4.3 / lsearch.c
CommitLineData
d2601b5f
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Roger L. Snyder.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21#if defined(LIBC_SCCS) && !defined(lint)
8190f343 22static char sccsid[] = "@(#)lsearch.c 5.2 (Berkeley) %G%";
d2601b5f
KB
23#endif /* LIBC_SCCS and not lint */
24
25#include <sys/types.h>
26#include <unistd.h>
27
28char *linear_base();
29
30char *
31lsearch(key, base, nelp, width, compar)
32 char *key, *base;
33 u_int *nelp, width;
34 int (*compar)();
35{
36 return(linear_base(key, base, nelp, width, compar, 1));
37}
38
39char *
40lfind(key, base, nelp, width, compar)
41 char *key, *base;
42 u_int *nelp, width;
43 int (*compar)();
44{
45 return(linear_base(key, base, nelp, width, compar, 0));
46}
47
48static char *
49linear_base(key, base, nelp, width, compar, add_flag)
50 char *key, *base;
51 u_int *nelp, width;
52 int (*compar)(), add_flag;
53{
54 register char *element, *end;
55
56 end = base + *nelp * width;
57 for (element = base; element < end; element += width)
58 if (!compar(element, key)) /* key found */
59 return(element);
60
61 if (!add_flag) /* key not found */
62 return(NULL);
63
64 /*
65 * The UNIX System User's Manual, 1986 edition claims that
66 * a NULL pointer is returned by lsearch with errno set
67 * appropriately, if there is not enough room in the table
68 * to add a new item. This can't be done as none of these
69 * routines have any method of determining the size of the
70 * table. This comment was isn't in the 1986-87 System V
71 * manual.
72 */
73 ++*nelp;
74 bcopy(key, end, (int)width);
75 return(end);
76}