lsearch.c 955 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * This file lifted in toto from 'Dlibs' on the atari ST (RdeBath)
  3. *
  4. *
  5. * Dale Schumacher 399 Beacon Ave.
  6. * (alias: Dalnefre') St. Paul, MN 55104
  7. * dal@syntel.UUCP United States of America
  8. * "It's not reality that's important, but how you perceive things."
  9. */
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <search.h>
  13. void *lfind(const void *key, const void *base, size_t *nmemb,
  14. size_t size, int (*compar)(const void *, const void *))
  15. {
  16. register int n = *nmemb;
  17. while (n--) {
  18. if ((*compar) (base, key) == 0)
  19. return ((void*)base);
  20. base += size;
  21. }
  22. return (NULL);
  23. }
  24. void *lsearch(const void *key, void *base, size_t *nmemb,
  25. size_t size, int (*compar)(const void *, const void *))
  26. {
  27. register char *p;
  28. if ((p = lfind(key, base, nmemb, size, compar)) == NULL) {
  29. p = memcpy((base + (size * (*nmemb))), key, size);
  30. ++(*nmemb);
  31. }
  32. return (p);
  33. }