1 #include <search.h> 2 #include <string.h> 3 4 void *lsearch(const void *key, void *base, size_t *nelp, size_t width, 5 int (*compar)(const void *, const void *)) 6 { 7 char (*p)[width] = base; 8 size_t n = *nelp; 9 size_t i; 10 11 for (i = 0; i < n; i++) 12 if (compar(key, p[i]) == 0) 13 return p[i]; 14 *nelp = n+1; 15 return memcpy(p[n], key, width); 16 } 17 18 void *lfind(const void *key, const void *base, size_t *nelp, 19 size_t width, int (*compar)(const void *, const void *)) 20 { 21 char (*p)[width] = (void *)base; 22 size_t n = *nelp; 23 size_t i; 24 25 for (i = 0; i < n; i++) 26 if (compar(key, p[i]) == 0) 27 return p[i]; 28 return 0; 29 } 30 31 32