'\"macro stdmacro
.if n .pH g3c.lsearch @(#)lsearch	40.17 of 10/27/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} lsearch 3C "C Programming Language Utilities" "\&"
.if \nX=1 .ds x} lsearch 3C "C Programming Language Utilities"
.if \nX=2 .ds x} lsearch 3C "" "\&"
.if \nX=3 .ds x} lsearch "" "" "\&"
.TH \*(x}
.SH NAME
\f4lsearch\f1, \f4lfind\f1 \- linear search and update
.SH SYNOPSIS
.nf
\f4#include <search.h>\f1
.PP
\f4void \(**lsearch (const void \(**key, void \(** base, size_t \(**nelp,
    size_t width, int (\(**compar) (const void \(**, const void \(**));\fP
.PP
\f4void \(**lfind (const void \(**key, const void \(**base, size_t \(**nelp,
    size_t width, int (\(**compar)(const void \(**, const void \(**));\fP
.fi
.SH DESCRIPTION
\f4lsearch\fP
is a linear search routine generalized from Knuth (6.1) Algorithm S.
It returns a pointer into a table indicating where
a datum may be found.
If the datum does not occur, it is added
at the end of the table.
.I key\^
points to the datum to be sought in the table.
.I base\^
points to the first element in the table.
.I nelp\^
points to an integer containing the current number of 
elements in the table.
The integer is incremented if the datum is added to the table.
.I\^ width 
is the size of an element in bytes. 
.I compar\^
is a pointer to the comparison function that the user must supply
(\f4strcmp\f1,
for example).
It is called with two arguments that point
to the elements being compared.
The function must return zero if the elements
are equal and non-zero otherwise.
.PP
\f4lfind\fP
is the same as 
\f4lsearch\fP
except that if the datum is not found, it is not added
to the table. 
Instead, a 
null pointer is returned.
.SH NOTES
The pointers to the key and the element
at the base of the table may be
pointers to any type.
.PP
The comparison function need not compare every byte,
so arbitrary data may be contained in the elements
in addition to the values being compared.
.PP
The value returned should be cast into type pointer-to-element.
.SH EXAMPLE
This program will read in less than \f4TABSIZE\fP 
strings of length less than \f4ELSIZE\fP
and store them in a table, eliminating duplicates,
and then will print each entry.
.PP
.RS
.nf
.ss 18
\f4#include <search.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define TABSIZE 50
#define ELSIZE 120

main()
{
	char line[ELSIZE];	/\(** buffer to hold input string \(**/
	char tab[TABSIZE][ELSIZE];	/\(** table of strings \(**/
	size_t nel = 0;		/\(** number of entries in tab \(**/
	int i;

	while (fgets(line, ELSIZE, stdin) != NULL &&
		nel < TABSIZE)
		(void) lsearch(line, tab, &nel, ELSIZE, mycmp);
	for( i = 0; i < nel; i++ )
		(void)fputs(tab[i], stdout);
	return 0;
}\f1
.fi
.RE
.SH SEE ALSO
\f4bsearch\fP(3C), \f4hsearch\fP(3C), \f4string\fP(3C), \f4tsearch\fP(3C).
.SH NOTES
If the searched-for datum is found, both 
\f4lsearch\fP
and 
\f4lfind\fP
return a pointer
to it.
Otherwise, 
\f4lfind\fP
returns \f4NULL\fP and 
\f4lsearch\fP
returns a pointer to the newly
added element.
.PP
Undefined results can occur if there is not enough room in the table to
add a new item.
.\"	@(#)lsearch.3c	6.3 of 10/20/83
.Ee
