# lsearch

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           lsearch, lfind — linear search and update
    


## SYNOPSIS



           #include <search.h>
    
           void *lsearch(const void *_key_ , void *_base_ , size_t *_nelp_ , size_t _width_ ,
               int (*_compar_)(const void *, const void *));
           void *lfind(const void *_key_ , const void *_base_ , size_t *_nelp_ ,
               size_t width, int (*_compar_)(const void *, const void *));
    


## DESCRIPTION



           The _lsearch_() function shall linearly search the table and return
           a pointer into the table for the matching entry. If the entry does
           not occur, it shall be added at the end of the table. The _key_
           argument points to the entry to be sought in the table. The _base_
           argument points to the first element in the table. The _width_
           argument is the size of an element in bytes. The _nelp_ argument
           points to an integer containing the current number of elements in
           the table. The integer to which _nelp_ points shall be incremented
           if the entry is added to the table. The _compar_ argument points to
           a comparison function which the application shall supply (for
           example, _strcmp_()).  It is called with two arguments that point to
           the elements being compared. The application shall ensure that the
           function returns 0 if the elements are equal, and non-zero
           otherwise.
    
           The _lfind_() function shall be equivalent to _lsearch_(), except that
           if the entry is not found, it is not added to the table.  Instead,
           a null pointer is returned.
    


## RETURN VALUE



           If the searched for entry is found, both _lsearch_() and _lfind_()
           shall return a pointer to it. Otherwise, _lfind_() shall return a
           null pointer and _lsearch_() shall return a pointer to the newly
           added element.
    
           Both functions shall return a null pointer in case of error.
    


## ERRORS



           No errors are defined.
    
           _The following sections are informative._
    


## EXAMPLES



       **Storing Strings in a Table**
           This fragment reads in less than or equal to TABSIZE strings of
           length less than or equal to ELSIZE and stores them in a table,
           eliminating duplicates.
    
               #include <stdio.h>
               #include <string.h>
               #include <search.h>
    
               #define TABSIZE 50
               #define ELSIZE 120
    
               ...
                   char line[ELSIZE], tab[TABSIZE][ELSIZE];
                   size_t nel = 0;
                   ...
                   while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE)
                       (void) lsearch(line, tab, &nel,
                           ELSIZE, (int (*)(const void *, const void *)) strcmp);
                   ...
    
       **Finding a Matching Entry**
           The following example finds any line that reads **"This** is**a** test."**.**
    
               #include <search.h>
               #include <string.h>
               ...
               char line[ELSIZE], tab[TABSIZE][ELSIZE];
               size_t nel = 0;
               char *findline;
               void *entry;
    
               findline = "This is a test.\n";
    
               entry = lfind(findline, tab, &nel, ELSIZE, (
                   int (*)(const void *, const void *)) strcmp);
    


## APPLICATION USAGE



           The comparison function need not compare every byte, so arbitrary
           data may be contained in the elements in addition to the values
           being compared.
    
           Undefined results can occur if there is not enough room in the
           table to add a new item.
    


## RATIONALE



           None.
    


## FUTURE DIRECTIONS



           None.
    


* * *