home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / dlibssrc / lsearch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-14  |  911 b   |  41 lines

  1. #include <stdio.h>
  2.  
  3. char *lfind(key, base, num, size, cmp)
  4. register char *key;
  5. register char *base;
  6. register int num;
  7. register int size;
  8. register int (*cmp)();
  9. /*
  10.  *    Like lsearch(), but do not add elements which are not found.
  11.  */
  12. {
  13.     while(num--) {
  14.         if((cmp)(base, key) == 0)
  15.             return(base);
  16.         base += size;
  17.     }
  18.     return(NULL);
  19. }
  20.  
  21. char *lsearch(key, base, num, size, cmp)
  22. char *key;
  23. char *base;
  24. int num;
  25. int size;
  26. int (*cmp)();
  27. /*
  28.  *    Perform a linear search for <key> on the data at <base>. The
  29.  *    <num>, <size> and <cmp> parameters are like the corresponding
  30.  *    parameters to qsort().  A pointer to the first matching element
  31.  *    is returned for success, or NULL for failure.  If <key> is not
  32.  *    found, it will be added to the end of the array.
  33.  */
  34. {
  35.     register char *p;
  36.  
  37.     if(p = lfind(key, base, num, size, cmp))
  38.         return(p);
  39.     blkcpy((base + (size * num)), key, size);
  40. }
  41.