summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2003-05-08 23:21:36 +0000
committermillert <>2003-05-08 23:21:36 +0000
commit904702870b8643c3707b8b8747e4c6814f2afa54 (patch)
tree3d12ac094717e8b222ea3871e71bdd988a8cc79a
parent3512822e2f5923ed1823cf94b42172157fddd7e3 (diff)
downloadopenbsd-904702870b8643c3707b8b8747e4c6814f2afa54.tar.gz
openbsd-904702870b8643c3707b8b8747e4c6814f2afa54.tar.bz2
openbsd-904702870b8643c3707b8b8747e4c6814f2afa54.zip
move lsearch(3) and insque(3) from libcompat -> libc; they are now POSIX
-rw-r--r--src/lib/libc/stdlib/Makefile.inc15
-rw-r--r--src/lib/libc/stdlib/lsearch.3107
-rw-r--r--src/lib/libc/stdlib/lsearch.c90
3 files changed, 205 insertions, 7 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index 98c13c8c8e..d2034982fe 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -5,10 +5,10 @@
5 5
6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ 6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
7 calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt.c getopt_long.c \ 7 calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt.c getopt_long.c \
8 getsubopt.c heapsort.c l64a.c malloc.c merge.c multibyte.c putenv.c \ 8 getsubopt.c heapsort.c l64a.c lsearch.c malloc.c merge.c multibyte.c \
9 qsort.c radixsort.c rand.c random.c realpath.c setenv.c strtod.c \ 9 putenv.c qsort.c radixsort.c rand.c random.c realpath.c setenv.c \
10 strtol.c strtoll.c strtoul.c strtoull.c system.c tfind.c tsearch.c \ 10 strtod.c strtol.c strtoll.c strtoul.c strtoull.c system.c tfind.c \
11 _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ 11 tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \
12 mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c 12 mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c
13 13
14.if (${MACHINE_ARCH} == "m68k") 14.if (${MACHINE_ARCH} == "m68k")
@@ -34,13 +34,14 @@ SRCS+= abs.c div.c labs.c ldiv.c
34 34
35MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ 35MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
36 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ 36 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
37 getsubopt.3 labs.3 ldiv.3 malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 \ 37 getsubopt.3 labs.3 ldiv.3 lsearch.3 malloc.3 memory.3 qabs.3 qdiv.3 \
38 radixsort.3 rand48.3 rand.3 random.3 realpath.3 strtod.3 strtol.3 \ 38 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 strtod.3 \
39 strtoul.3 system.3 tsearch.3 39 strtol.3 strtoul.3 system.3 tsearch.3
40 40
41MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 41MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3
42MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 42MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3
43MLINKS+=getopt_long.3 getopt_long_only.3 43MLINKS+=getopt_long.3 getopt_long_only.3
44MLINKS+=lsearch.3 lfind.3
44MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 45MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3
45MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5 46MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5
46MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 47MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
diff --git a/src/lib/libc/stdlib/lsearch.3 b/src/lib/libc/stdlib/lsearch.3
new file mode 100644
index 0000000000..3e17d131e4
--- /dev/null
+++ b/src/lib/libc/stdlib/lsearch.3
@@ -0,0 +1,107 @@
1.\" Copyright (c) 1989, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)lsearch.3 8.1 (Berkeley) 6/4/93
33.\"
34.Dd June 4, 1993
35.Dt LSEARCH 3
36.Os
37.Sh NAME
38.Nm lsearch ,
39.Nm lfind
40.Nd linear searching routines
41.Sh SYNOPSIS
42.Ft char *
43.Fn lsearch "const void *key" "const void *base" "size_t *nelp" "size_t width" "int (*compar)(void *, void *)"
44.Ft char *
45.Fn lfind "const void *key" "const void *base" "size_t *nelp" "size_t width" "int (*compar)(void *, void *)"
46.Sh DESCRIPTION
47The functions
48.Fn lsearch ,
49and
50.Fn lfind
51provide basic linear searching functionality.
52.Pp
53.Fa base
54is the pointer to the beginning of an array.
55The argument
56.Fa nelp
57is the current number of elements in the array, where each element
58is
59.Fa width
60bytes long.
61The
62.Fa compar
63function
64is a comparison routine which is used to compare two elements.
65It takes two arguments which point to the
66.Fa key
67object and to an array member, in that order, and must return an integer
68less than, equivalent to, or greater than zero if the
69.Fa key
70object is considered, respectively, to be less than, equal to, or greater
71than the array member.
72.Pp
73The
74.Fn lsearch
75and
76.Fn lfind
77functions
78return a pointer into the array referenced by
79.Fa base
80where
81.Fa key
82is located.
83If
84.Fa key
85does not exist,
86.Fn lfind
87will return a null pointer and
88.Fn lsearch
89will add it to the array.
90When an element is added to the array by
91.Fn lsearch
92the location referenced by the argument
93.Fa nelp
94is incremented by one.
95.Sh SEE ALSO
96.Xr bsearch 3 ,
97.Xr db 3
98.Sh STANDARDS
99The
100.Fn lsearch
101and
102.Fn lfind
103functions conform to the
104.St -p1003.1-01
105and
106.St -xpg4.3 .
107specifications.
diff --git a/src/lib/libc/stdlib/lsearch.c b/src/lib/libc/stdlib/lsearch.c
new file mode 100644
index 0000000000..ba0aad3cfc
--- /dev/null
+++ b/src/lib/libc/stdlib/lsearch.c
@@ -0,0 +1,90 @@
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. 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, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)lsearch.c 8.1 (Berkeley) 6/4/93";
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/types.h>
42#include <string.h>
43#include <search.h>
44
45typedef int (*cmp_fn_t)(const void *, const void *);
46static void *linear_base(const void *, const void *, size_t *, size_t,
47 cmp_fn_t, int);
48
49void *
50lsearch(const void *key, const void *base, size_t *nelp, size_t width,
51 cmp_fn_t compar)
52{
53
54 return(linear_base(key, base, nelp, width, compar, 1));
55}
56
57void *
58lfind(const void *key, const void *base, size_t *nelp, size_t width,
59 cmp_fn_t compar)
60{
61 return(linear_base(key, base, nelp, width, compar, 0));
62}
63
64static void *
65linear_base(const void *key, const void *base, size_t *nelp, size_t width,
66 cmp_fn_t compar, int add_flag)
67{
68 const char *element, *end;
69
70 end = (const char *)base + *nelp * width;
71 for (element = base; element < end; element += width)
72 if (!compar(key, element)) /* key found */
73 return((void *)element);
74
75 if (!add_flag) /* key not found */
76 return(NULL);
77
78 /*
79 * The UNIX System User's Manual, 1986 edition claims that
80 * a NULL pointer is returned by lsearch with errno set
81 * appropriately, if there is not enough room in the table
82 * to add a new item. This can't be done as none of these
83 * routines have any method of determining the size of the
84 * table. This comment isn't in the 1986-87 System V
85 * manual.
86 */
87 ++*nelp;
88 memcpy((void *)end, key, width);
89 return((void *)end);
90}