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
12 lsearch, lfind - linear search and update
13
15 #include <search.h>
16
17 void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
18 int (*compar)(const void *, const void *));
19 void *lfind(const void *key, const void *base, size_t *nelp,
20 size_t width, int (*compar)(const void *, const void *));
21
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
61 #include <stdio.h>
62 #include <string.h>
63 #include <search.h>
64
65
66 #define TABSIZE 50
67 #define ELSIZE 120
68
69
70 ...
71 char line[ELSIZE], tab[TABSIZE][ELSIZE];
72 size_t nel = 0;
73 ...
74 while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
75 (void) lsearch(line, tab, &nel,
76 ELSIZE, (int (*)(const void *, const void *)) strcmp);
77 ...
78
79 Finding a Matching Entry
80 The following example finds any line that reads "This is a test." .
81
82
83 #include <search.h>
84 #include <string.h>
85 ...
86 char line[ELSIZE], tab[TABSIZE][ELSIZE];
87 size_t nel = 0;
88 char *findline;
89 void *entry;
90
91
92 findline = "This is a test.\n";
93
94
95 entry = lfind(findline, tab, &nel, ELSIZE, (
96 int (*)(const void *, const void *)) strcmp);
97
99 The comparison function need not compare every byte, so arbitrary data
100 may be contained in the elements in addition to the values being com‐
101 pared.
102
103 Undefined results can occur if there is not enough room in the table to
104 add a new item.
105
107 None.
108
110 None.
111
113 hcreate(), tsearch(), the Base Definitions volume of
114 IEEE Std 1003.1-2001, <search.h>
115
117 Portions of this text are reprinted and reproduced in electronic form
118 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
119 -- Portable Operating System Interface (POSIX), The Open Group Base
120 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
121 Electrical and Electronics Engineers, Inc and The Open Group. In the
122 event of any discrepancy between this version and the original IEEE and
123 The Open Group Standard, the original IEEE and The Open Group Standard
124 is the referee document. The original Standard can be obtained online
125 at http://www.opengroup.org/unix/online.html .
126
127
128
129IEEE/The Open Group 2003 LSEARCH(3P)