about summary refs log tree commit diff
path: root/src/search/lsearch.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/search/lsearch.c')
-rw-r--r--src/search/lsearch.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/search/lsearch.c b/src/search/lsearch.c
new file mode 100644
index 00000000..63f31922
--- /dev/null
+++ b/src/search/lsearch.c
@@ -0,0 +1,31 @@
+#include <search.h>
+#include <string.h>
+
+void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
+	int (*compar)(const void *, const void *))
+{
+	char (*p)[width] = base;
+	size_t n = *nelp;
+	size_t i;
+
+	for (i = 0; i < n; i++)
+		if (compar(p[i], key) == 0)
+			return p[i];
+	*nelp = n+1;
+	return memcpy(p[n], key, width);
+}
+
+void *lfind(const void *key, const void *base, size_t *nelp,
+	size_t width, int (*compar)(const void *, const void *))
+{
+	char (*p)[width] = (void *)base;
+	size_t n = *nelp;
+	size_t i;
+
+	for (i = 0; i < n; i++)
+		if (compar(p[i], key) == 0)
+			return p[i];
+	return 0;
+}
+
+