1LSEARCH(3P)                POSIX Programmer's Manual               LSEARCH(3P)
2
3
4

PROLOG

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

NAME

12       lsearch, lfind — linear search and update
13

SYNOPSIS

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

DESCRIPTION

23       The lsearch() function shall linearly search the  table  and  return  a
24       pointer  into  the  table for the matching entry. If the entry does not
25       occur, it shall be added at the end of  the  table.  The  key  argument
26       points to the entry to be sought in the table. The base argument points
27       to the first element in the table. The width argument is the size of an
28       element in bytes. The nelp argument points to an integer containing the
29       current number of elements in the table.  The  integer  to  which  nelp
30       points  shall  be  incremented  if the entry is added to the table. The
31       compar argument points to a comparison function which  the  application
32       shall  supply (for example, strcmp()).  It is called with two arguments
33       that point to the elements being compared. The application shall ensure
34       that  the  function  returns  0 if the elements are equal, and non-zero
35       otherwise.
36
37       The lfind() function shall be equivalent to lsearch(), except  that  if
38       the  entry is not found, it is not added to the table.  Instead, a null
39       pointer is returned.
40

RETURN VALUE

42       If the searched for entry is found, both lsearch()  and  lfind()  shall
43       return  a pointer to it. Otherwise, lfind() shall return a null pointer
44       and lsearch() shall return a pointer to the newly added element.
45
46       Both functions shall return a null pointer in case of error.
47

ERRORS

49       No errors are defined.
50
51       The following sections are informative.
52

EXAMPLES

54   Storing Strings in a Table
55       This fragment reads in less than or equal to TABSIZE strings of  length
56       less  than  or  equal to ELSIZE and stores them in a table, eliminating
57       duplicates.
58
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
80           #include <search.h>
81           #include <string.h>
82           ...
83           char line[ELSIZE], tab[TABSIZE][ELSIZE];
84           size_t nel = 0;
85           char *findline;
86           void *entry;
87
88           findline = "This is a test.\n";
89
90           entry = lfind(findline, tab, &nel, ELSIZE, (
91               int (*)(const void *, const void *)) strcmp);
92

APPLICATION USAGE

94       The comparison function need not compare every byte, so arbitrary  data
95       may  be  contained in the elements in addition to the values being com‐
96       pared.
97
98       Undefined results can occur if there is not enough room in the table to
99       add a new item.
100

RATIONALE

102       None.
103

FUTURE DIRECTIONS

105       None.
106

SEE ALSO

108       hcreate(), tdelete()
109
110       The Base Definitions volume of POSIX.1‐2017, <search.h>
111
113       Portions  of  this text are reprinted and reproduced in electronic form
114       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
115       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
116       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
117       Electrical  and  Electronics Engineers, Inc and The Open Group.  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.opengroup.org/unix/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                  2017                          LSEARCH(3P)
Impressum