Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ lsearch(S) — Xenix 2.3.4g

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

bsearch(S)

hsearch(S)

qsort(S)

tsearch(S)



     LSEARCH(S)               XENIX System V                LSEARCH(S)



     Name
          lsearch, lfind - Performs linear search and update.

     Syntax
          #include <stdio.h>
          #include <search.h>
          char *lsearch (key, base, nelp, width, compar)
          char *key;
          char *base;
          unsigned *nelp;
          unsigned width;
          int (*compar)();

          char *lfind (key, base, nelp, width, compar)
          char *key;
          char *base;
          unsigned *nelp;
          unsigned width;
          int (*compar) ();

     Description
          lsearch is a linear search routine generalized from Knuth
          (6.1) Algorithm Q.  It returns a pointer into a table
          indicating the location at which a datum may be found.  If
          the item does not occur, it is added at the end of the
          table.  The first argument is a pointer to the datum to be
          located in the table.  The second argument is a pointer to
          the base of the table.  The third argument is the address of
          an integer containing the number of items in the table.  It
          is incremented if the item is added to the table.  The
          fourth argument is the width of an element in bytes.  The
          last argument is the name of the comparison routine.  It is
          called with two arguments which are pointers to the elements
          being compared.  The routine must return zero if the items
          are equal, and nonzero otherwise.

          lfind is the same as lsearch except that if the datum is not
          found, it is not added to the table.

     Example
          This fragment of code will read _ TABSIZE strings of length
          _ ELSIZE and store them in a table, eliminating duplicates:

               #include <stdio.h>
               #include <search.h>

               #define TABSIZE 50
               #define ELSIZE 120

               char line[ELSIZE], tab[TABSIZE][ELSIZE], *lsearch( );
               unsigned nel = 0;
               int strcmp( );



     Page 1                                           (printed 8/7/87)





     LSEARCH(S)               XENIX System V                LSEARCH(S)



               while (fgets(line, ELSIZE, stdin) != NULL &&
                  nel < TABSIZE)
                    (void) lsearch(line, (char *)tab, &nel,
                         ELSIZE, strcmp);

     See Also
          bsearch(S), hsearch(S), qsort(S), tsearch(S)

     Diagnostics
          If the datum searched for is found, both lsearch and lfind
          return a pointer to it.  Otherwise, lfind returns NULL and
          lsearch returns a pointer to the newly added element.

     Notes
          The pointers to the key and the element at the base of the
          table should be of type pointer-to-element, and cast to type
          pointer-to-character.

          The comparison function need not compare every byte, so
          arbitrary data may be contained in the elements in addition
          to the values being compared.

          Although declared as type pointer-to-character, the value
          returned should be cast into type pointer-to-element

          Unpredictable events can occur if there is not enough room
          in the table to add a new item.




























     Page 2                                           (printed 8/7/87)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026