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

NAME

6       lsearch, lfind - linear search and update
7

SYNOPSIS

9       #include <search.h>
10
11       void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
12              int (*compar)(const void *, const void *));
13       void *lfind(const void *key, const void *base, size_t *nelp,
14              size_t width, int (*compar)(const void *, const void *));
15
16

DESCRIPTION

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

RETURN VALUE

37       If  the  searched  for entry is found, both lsearch() and lfind() shall
38       return a pointer to it. Otherwise, lfind() shall return a null  pointer
39       and lsearch() shall return a pointer to the newly added element.
40
41       Both functions shall return a null pointer in case of error.
42

ERRORS

44       No errors are defined.
45
46       The following sections are informative.
47

EXAMPLES

49   Storing Strings in a Table
50       This  fragment reads in less than or equal to TABSIZE strings of length
51       less than or equal to ELSIZE and stores them in  a  table,  eliminating
52       duplicates.
53
54
55              #include <stdio.h>
56              #include <string.h>
57              #include <search.h>
58
59
60              #define TABSIZE 50
61              #define ELSIZE 120
62
63
64              ...
65                  char line[ELSIZE], tab[TABSIZE][ELSIZE];
66                  size_t nel = 0;
67                  ...
68                  while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
69                      (void) lsearch(line, tab, &nel,
70                          ELSIZE, (int (*)(const void *, const void *)) strcmp);
71                  ...
72
73   Finding a Matching Entry
74       The following example finds any line that reads "This is a test."  .
75
76
77              #include <search.h>
78              #include <string.h>
79              ...
80              char line[ELSIZE], tab[TABSIZE][ELSIZE];
81              size_t nel = 0;
82              char *findline;
83              void *entry;
84
85
86              findline = "This is a test.\n";
87
88
89              entry = lfind(findline, tab, &nel, ELSIZE, (
90                  int (*)(const void *, const void *)) strcmp);
91

APPLICATION USAGE

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

RATIONALE

101       None.
102

FUTURE DIRECTIONS

104       None.
105

SEE ALSO

107       hcreate()   ,   tsearch()   ,   the   Base   Definitions   volume    of
108       IEEE Std 1003.1-2001, <search.h>
109
111       Portions  of  this text are reprinted and reproduced in electronic form
112       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
113       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
114       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
115       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
116       event of any discrepancy between this version and the original IEEE and
117       The  Open Group Standard, the original IEEE and The Open Group Standard
118       is the referee document. The original Standard can be  obtained  online
119       at http://www.opengroup.org/unix/online.html .
120
121
122
123IEEE/The Open Group                  2003                           LSEARCH(P)
Impressum