diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-04-28 11:34:12 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-04-28 11:34:12 -0400 |
| commit | 27f637904876fac0f8e69b6829591ea0a7689bb2 (patch) | |
| tree | 13b44eb7ddb36b78c9cd68776b750880dbba7cfa /util.c | |
| parent | 67787e9e2a6a580400cea0f925b7a5f3d9222ef7 (diff) | |
| download | volute-27f637904876fac0f8e69b6829591ea0a7689bb2.zip | |
move lsearch() into util.c
Diffstat (limited to 'util.c')
| -rw-r--r-- | util.c | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -9,3 +9,20 @@ free_arr(void **arr, int n) { } free(arr); } + +/* lsearch linearly searches base[0]...base[n-1] for an item that matches *key. + * The function cmp must return zero if its first argument (the search key) + * equals its second (a table entry), non-zero if not equal. + * Returns the index of the first occurrence of key in base, or -1 if not present. */ +int +lsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp)(const void *keyval, const void *datum)) { + size_t i; + + for (i = 0; i < n; i++) { + if (cmp(key, base) == 0) { + return i; + } + base = (char *) base + size; + } + return -1; +} |