1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
'\" et
.TH LSEARCH "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
lsearch,
lfind
\(em linear search and update
.SH SYNOPSIS
.LP
.nf
#include <search.h>
.P
void *lsearch(const void *\fIkey\fP, void *\fIbase\fP, size_t *\fInelp\fP, size_t \fIwidth\fP,
int (*\fIcompar\fP)(const void *, const void *));
void *lfind(const void *\fIkey\fP, const void *\fIbase\fP, size_t *\fInelp\fP,
size_t width, int (*\fIcompar\fP)(const void *, const void *));
.fi
.SH DESCRIPTION
The
\fIlsearch\fR()
function shall linearly search the table and return a pointer into the
table for the matching entry. If the entry does not occur, it shall be
added at the end of the table. The
.IR key
argument points to the entry to be sought in the table. The
.IR base
argument points to the first element in the table. The
.IR width
argument is the size of an element in bytes. The
.IR nelp
argument points to an integer containing the current number of elements
in the table. The integer to which
.IR nelp
points shall be incremented if the entry is added to the table. The
.IR compar
argument points to a comparison function which the application shall
supply (for example,
\fIstrcmp\fR()).
It is called with two arguments that point to the elements being
compared. The application shall ensure that the function returns 0
if the elements are equal, and non-zero otherwise.
.P
The
\fIlfind\fR()
function shall be equivalent to
\fIlsearch\fR(),
except that if the entry is not found, it is not added to the table.
Instead, a null pointer is returned.
.SH "RETURN VALUE"
If the searched for entry is found, both
\fIlsearch\fR()
and
\fIlfind\fR()
shall return a pointer to it. Otherwise,
\fIlfind\fR()
shall return a null pointer and
\fIlsearch\fR()
shall return a pointer to the newly added element.
.P
Both functions shall return a null pointer in case of error.
.SH ERRORS
No errors are defined.
.LP
.IR "The following sections are informative."
.SH "EXAMPLES"
.SS "Storing Strings in a Table"
.P
This fragment reads in less than or equal to TABSIZE
strings of length less than or equal to ELSIZE and stores them in a
table, eliminating duplicates.
.sp
.RS 4
.nf
#include <stdio.h>
#include <string.h>
#include <search.h>
.P
#define TABSIZE 50
#define ELSIZE 120
.P
\&...
char line[ELSIZE], tab[TABSIZE][ELSIZE];
size_t nel = 0;
...
while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
(void) lsearch(line, tab, &nel,
ELSIZE, (int (*)(const void *, const void *)) strcmp);
...
.fi
.P
.RE
.SS "Finding a Matching Entry"
.P
The following example finds any line that reads
.BR \(dqThis is a test.\(dq .
.sp
.RS 4
.nf
#include <search.h>
#include <string.h>
\&...
char line[ELSIZE], tab[TABSIZE][ELSIZE];
size_t nel = 0;
char *findline;
void *entry;
.P
findline = "This is a test.\en";
.P
entry = lfind(findline, tab, &nel, ELSIZE, (
int (*)(const void *, const void *)) strcmp);
.fi
.P
.RE
.SH "APPLICATION USAGE"
The comparison function need not compare every byte, so arbitrary
data may be contained in the elements in addition to the values
being compared.
.P
Undefined results can occur if there is not enough room in the table to
add a new item.
.SH RATIONALE
None.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIhcreate\fR\^(\|)",
.IR "\fItdelete\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "\fB<search.h>\fP"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|