1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/types.h>
19#include <common.h>
20#include <exports.h>
21
22void qsort(void *base,
23 size_t nel,
24 size_t width,
25 int (*comp)(const void *, const void *))
26{
27 size_t wgap, i, j, k;
28 char tmp;
29
30 if ((nel > 1) && (width > 0)) {
31 assert(nel <= ((size_t)(-1)) / width);
32 wgap = 0;
33 do {
34 wgap = 3 * wgap + 1;
35 } while (wgap < (nel-1)/3);
36
37
38 wgap *= width;
39 nel *= width;
40 do {
41 i = wgap;
42 do {
43 j = i;
44 do {
45 register char *a;
46 register char *b;
47
48 j -= wgap;
49 a = j + ((char *)base);
50 b = a + wgap;
51 if ((*comp)(a, b) <= 0) {
52 break;
53 }
54 k = width;
55 do {
56 tmp = *a;
57 *a++ = *b;
58 *b++ = tmp;
59 } while (--k);
60 } while (j >= wgap);
61 i += width;
62 } while (i < nel);
63 wgap = (wgap - width)/3;
64 } while (wgap);
65 }
66}
67
68int strcmp_compar(const void *p1, const void *p2)
69{
70 return strcmp(*(const char **)p1, *(const char **)p2);
71}
72