summaryrefslogtreecommitdiff
path: root/src/stdlib/bsearch_s.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-16 15:55:19 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-16 15:55:19 -0400
commite223e635cc53fa11e473cdbc864eb69a3da5f290 (patch)
treed481ca6fb0b7f4184f3ec259a6f9c37d76e56a26 /src/stdlib/bsearch_s.c
parent700fbd205a1a428677876d322606b9a354221892 (diff)
add skeleton of symbols from C11 LIB_EXT1
Diffstat (limited to 'src/stdlib/bsearch_s.c')
-rw-r--r--src/stdlib/bsearch_s.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/stdlib/bsearch_s.c b/src/stdlib/bsearch_s.c
new file mode 100644
index 00000000..12a19c25
--- /dev/null
+++ b/src/stdlib/bsearch_s.c
@@ -0,0 +1,49 @@
+#include <stddef.h>
+
+/** binary search **/
+void *bsearch_s(const void * key, const void * base,
+ rsize_t nmemb, rsize_t size,
+ int (*compar)(const void *x, const void *y, void * context),
+ void *context)
+{
+ __C_EXT(1, 201112L);
+ /* TODO: testing */
+ void *ret = NULL;
+ size_t i = nmemb / 2;
+ unsigned int trip = 1;
+
+ while (ret == NULL) {
+ int comp = compar(key, base+i);
+ if (comp == 0) {
+ return (void*)base+i;
+ } else if (comp > 0) {
+ i -= (nmemb >> trip);
+ } else {
+ i += (nmemb >> trip);
+ }
+ }
+ return NULL;
+}
+
+/***
+The fn(bsearch) function performs a binary search for arg(key) in the array
+arg(base), which contains arg(nmemb) members of arg(size) bytes each.
+
+The search is performed by calling arg(compar) with the first argument of
+arg(key), and the second being an element from the array at arg(base). The
+function must return less than 0 if arg(key) is less than the other element,
+0 if they are equal, and greater than 0 if arg(key) is greater than the other
+element.
+***/
+
+/* UNSPECIFIED: which element is matched if two elements are equal */
+/* UNDEFINED: the array at arg(base) is not sorted */
+/* IMPLEMENTATION: - */
+/* LOCALE: - */
+
+/* RETURN(NULL): no match was found */
+/* RETURN: a pointer to the matching element */
+
+/*
+CEXT1(201112)
+*/