summaryrefslogtreecommitdiff
path: root/libc/stdlib/stdlib.c
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2011-11-07 09:24:30 +0100
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2011-11-07 09:24:30 +0100
commit515d54433138596e81267237542bd9168b8cc787 (patch)
treef2d6745268b788cc38d1b67deb32f71916b10fec /libc/stdlib/stdlib.c
parent84144d142cb9f041dd18285cb2ad9dbe6619b0a2 (diff)
stdlib: add qsort_r
GNU extension like qsort but takes a 3 parameter comparision function. Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Diffstat (limited to 'libc/stdlib/stdlib.c')
-rw-r--r--libc/stdlib/stdlib.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/libc/stdlib/stdlib.c b/libc/stdlib/stdlib.c
index cd9a0cac6..9e8c347fd 100644
--- a/libc/stdlib/stdlib.c
+++ b/libc/stdlib/stdlib.c
@@ -784,7 +784,7 @@ void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
#endif
/**********************************************************************/
-#ifdef L_qsort
+#ifdef L_qsort_r
/* This code is derived from a public domain shell sort routine by
* Ray Gardner and found in Bob Stout's snippets collection. The
@@ -794,10 +794,11 @@ void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
* calculation, as well as to reduce the generated code size with
* bcc and gcc. */
-void qsort(void *base,
+void qsort_r(void *base,
size_t nel,
size_t width,
- int (*comp)(const void *, const void *))
+ __compar_d_fn_t comp,
+ void *arg)
{
size_t wgap, i, j, k;
char tmp;
@@ -823,7 +824,7 @@ void qsort(void *base,
j -= wgap;
a = j + ((char *)base);
b = a + wgap;
- if ((*comp)(a, b) <= 0) {
+ if ((*comp)(a, b, arg) <= 0) {
break;
}
k = width;
@@ -839,7 +840,7 @@ void qsort(void *base,
} while (wgap);
}
}
-libc_hidden_def(qsort)
+libc_hidden_def(qsort_r)
/* ---------- original snippets version below ---------- */
@@ -886,6 +887,18 @@ void ssort(void *base,
#endif
#endif
+
+#ifdef L_qsort
+void qsort(void *base,
+ size_t nel,
+ size_t width,
+ __compar_fn_t comp)
+{
+ return qsort_r (base, nel, width, (__compar_d_fn_t) comp, NULL);
+}
+libc_hidden_def(qsort)
+#endif
+
/**********************************************************************/
#ifdef L__stdlib_mb_cur_max