[9] | 1 | #include <lib.h>
|
---|
| 2 | #include <string.h>
|
---|
| 3 | /* lsearch(3) and lfind(3)
|
---|
| 4 | *
|
---|
| 5 | * Author: Terrence W. Holm Sep. 1988
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <stddef.h>
|
---|
| 9 |
|
---|
| 10 | _PROTOTYPE( char *lsearch, (char *key, char *base,
|
---|
| 11 | unsigned *count, unsigned width,
|
---|
| 12 | int (*keycmp)(const void *, const void *)));
|
---|
| 13 | _PROTOTYPE( char *lfind, (char *key, char *base,
|
---|
| 14 | unsigned *count, unsigned width,
|
---|
| 15 | int (*keycmp)(const void *, const void *)));
|
---|
| 16 |
|
---|
| 17 | char *lsearch(key, base, count, width, keycmp)
|
---|
| 18 | char *key;
|
---|
| 19 | char *base;
|
---|
| 20 | unsigned *count;
|
---|
| 21 | unsigned width;
|
---|
| 22 | _PROTOTYPE( int (*keycmp), (const void *, const void *));
|
---|
| 23 | {
|
---|
| 24 | char *entry;
|
---|
| 25 | char *last = base + *count * width;
|
---|
| 26 |
|
---|
| 27 | for (entry = base; entry < last; entry += width)
|
---|
| 28 | if (keycmp(key, entry) == 0) return(entry);
|
---|
| 29 |
|
---|
| 30 | bcopy(key, last, width);
|
---|
| 31 | *count += 1;
|
---|
| 32 | return(last);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | char *lfind(key, base, count, width, keycmp)
|
---|
| 37 | char *key;
|
---|
| 38 | char *base;
|
---|
| 39 | unsigned *count;
|
---|
| 40 | unsigned width;
|
---|
| 41 | _PROTOTYPE( int (*keycmp), (const void *, const void *));
|
---|
| 42 | {
|
---|
| 43 | char *entry;
|
---|
| 44 | char *last = base + *count * width;
|
---|
| 45 |
|
---|
| 46 | for (entry = base; entry < last; entry += width)
|
---|
| 47 | if (keycmp(key, entry) == 0) return(entry);
|
---|
| 48 |
|
---|
| 49 | return((char *)NULL);
|
---|
| 50 | }
|
---|