[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 3/3] libc: bsearch



+ lib: bsearch()

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 src/include/stdlib.h      |    2 +
 src/lib/stdlibc/bsearch.c |   46 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)
 create mode 100644 src/lib/stdlibc/bsearch.c

diff --git a/src/include/stdlib.h b/src/include/stdlib.h
index 6ef072a..77ae060 100644
--- a/src/include/stdlib.h
+++ b/src/include/stdlib.h
@@ -68,6 +68,8 @@ int putenv(const char* str);
 void unsetenv(const char* name);
 
 void qsort(void *base, size_t num, size_t size, int (*comparator)(const void *, const void *));
+void *bsearch(const void *key, const void *base, size_t nel,
+    size_t width, int (*compar)(const void *, const void *));
 
 char* mktemp(char* templatename);
 
diff --git a/src/lib/stdlibc/bsearch.c b/src/lib/stdlibc/bsearch.c
new file mode 100644
index 0000000..366090b
--- /dev/null
+++ b/src/lib/stdlibc/bsearch.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Kevin Wolf.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+
+void *bsearch(const void *key, const void *base, size_t nel,
+    size_t width, int (*compar)(const void *, const void *))
+{
+    size_t i;
+    const void* obj;
+
+    for (i = 0; i < nel; i++) {
+        obj = base + (width * i);
+        if (compar(key, obj) == 0) {
+            return (void*) obj;
+        }
+    }
+
+    return NULL;
+}
+
-- 
1.6.0.2