home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / misc / lsearch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-20  |  930 b   |  31 lines

  1. /* $Id: lsearch.c,v 1.1 1994/05/21 13:23:03 chris Exp $ */
  2.  
  3. #include <ansidecl.h>
  4. #include <search.h>
  5. #include <memory.h>
  6.  
  7. /* Perform a linear search for KEY in BASE which has *NMEMB elements
  8.    of SIZE bytes each.  The comparisons are done by (*COMPAR)(), which
  9.    must return zero if the elements being compared are equal, and
  10.    non-zero otherwise. 
  11.    If the datum is not found, it will be inserted at the end of the
  12.    table. */
  13. PTR
  14. DEFUN(lsearch, (key, base, pnmemb, size, compar),
  15.       register CONST PTR key AND CONST PTR base AND
  16.       register size_t *nmemb AND register size_t size AND
  17.       register int EXFUN((*compar), (CONST PTR, CONST PTR)))
  18. {
  19.   register size_t i;
  20.   register CONST PTR p;
  21.  
  22.   for(i = 0, p = base; i < *nmemb; 
  23.              p = (PTR) (((CONST char *) p) + size), i++)
  24.       if ( !((*compar)(key, p)))
  25.         return (PTR) p;
  26.  
  27.   (*nmemb)++;
  28.   memmove ((PTR) p, (PTR) key, size);
  29.   return (PTR) p;
  30. }
  31.