1LSEARCH(3P) POSIX Programmer's Manual LSEARCH(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 lsearch, lfind — linear search and update
14
16 #include <search.h>
17
18 void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
19 int (*compar)(const void *, const void *));
20 void *lfind(const void *key, const void *base, size_t *nelp,
21 size_t width, int (*compar)(const void *, const void *));
22
24 The lsearch() function shall linearly search the table and return a
25 pointer into the table for the matching entry. If the entry does not
26 occur, it shall be added at the end of the table. The key argument
27 points to the entry to be sought in the table. The base argument points
28 to the first element in the table. The width argument is the size of an
29 element in bytes. The nelp argument points to an integer containing the
30 current number of elements in the table. The integer to which nelp
31 points shall be incremented if the entry is added to the table. The
32 compar argument points to a comparison function which the application
33 shall supply (for example, strcmp()). It is called with two arguments
34 that point to the elements being compared. The application shall ensure
35 that the function returns 0 if the elements are equal, and non-zero
36 otherwise.
37
38 The lfind() function shall be equivalent to lsearch(), except that if
39 the entry is not found, it is not added to the table. Instead, a null
40 pointer is returned.
41
43 If the searched for entry is found, both lsearch() and lfind() shall
44 return a pointer to it. Otherwise, lfind() shall return a null pointer
45 and lsearch() shall return a pointer to the newly added element.
46
47 Both functions shall return a null pointer in case of error.
48
50 No errors are defined.
51
52 The following sections are informative.
53
55 Storing Strings in a Table
56 This fragment reads in less than or equal to TABSIZE strings of length
57 less than or equal to ELSIZE and stores them in a table, eliminating
58 duplicates.
59
60 #include <stdio.h>
61 #include <string.h>
62 #include <search.h>
63
64 #define TABSIZE 50
65 #define ELSIZE 120
66
67 ...
68 char line[ELSIZE], tab[TABSIZE][ELSIZE];
69 size_t nel = 0;
70 ...
71 while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
72 (void) lsearch(line, tab, &nel,
73 ELSIZE, (int (*)(const void *, const void *)) strcmp);
74 ...
75
76 Finding a Matching Entry
77 The following example finds any line that reads "Thisisatest.".
78
79 #include <search.h>
80 #include <string.h>
81 ...
82 char line[ELSIZE], tab[TABSIZE][ELSIZE];
83 size_t nel = 0;
84 char *findline;
85 void *entry;
86
87 findline = "This is a test.\n";
88
89 entry = lfind(findline, tab, &nel, ELSIZE, (
90 int (*)(const void *, const void *)) strcmp);
91
93 The comparison function need not compare every byte, so arbitrary data
94 may be contained in the elements in addition to the values being com‐
95 pared.
96
97 Undefined results can occur if there is not enough room in the table to
98 add a new item.
99
101 None.
102
104 None.
105
107 hcreate(), tdelete()
108
109 The Base Definitions volume of POSIX.1‐2008, <search.h>
110
112 Portions of this text are reprinted and reproduced in electronic form
113 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
114 -- Portable Operating System Interface (POSIX), The Open Group Base
115 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
116 cal and Electronics Engineers, Inc and The Open Group. (This is
117 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
118 event of any discrepancy between this version and the original IEEE and
119 The Open Group Standard, the original IEEE and The Open Group Standard
120 is the referee document. The original Standard can be obtained online
121 at http://www.unix.org/online.html .
122
123 Any typographical or formatting errors that appear in this page are
124 most likely to have been introduced during the conversion of the source
125 files to man page format. To report such errors, see https://www.ker‐
126 nel.org/doc/man-pages/reporting_bugs.html .
127
128
129
130IEEE/The Open Group 2013 LSEARCH(3P)