Serenity Operating System
at portability 126 lines 5.3 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27/*- 28 * Copyright (c) 1980, 1983, 1990 The Regents of the University of California. 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions 33 * are met: 34 * 1. Redistributions of source code must retain the above copyright 35 * notice, this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 3. All advertising materials mentioning features or use of this software 40 * must display the following acknowledgement: 41 * This product includes software developed by the University of 42 * California, Berkeley and its contributors. 43 * 4. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60#if defined(LIBC_SCCS) && !defined(lint) 61static char sccsid[] = "@(#)qsort.c 5.9 (Berkeley) 2/23/91"; 62#endif /* LIBC_SCCS and not lint */ 63 64#include <stdlib.h> 65#include <sys/types.h> 66 67static void insertion_sort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*)); 68static void insertion_sort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg); 69 70void qsort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*)) 71{ 72 if (nmemb <= 1) 73 return; 74 75 insertion_sort(bot, nmemb, size, compar); 76} 77 78void qsort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg) 79{ 80 if (nmemb <= 1) 81 return; 82 83 insertion_sort_r(bot, nmemb, size, compar, arg); 84} 85 86void insertion_sort(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*)) 87{ 88 int cnt; 89 unsigned char ch; 90 char *s1, *s2, *t1, *t2, *top; 91 top = (char*)bot + nmemb * size; 92 for (t1 = (char*)bot + size; t1 < top;) { 93 for (t2 = t1; (t2 -= size) >= bot && compar(t1, t2) < 0;) 94 ; 95 if (t1 != (t2 += size)) { 96 for (cnt = size; cnt--; ++t1) { 97 ch = *t1; 98 for (s1 = s2 = t1; (s2 -= size) >= t2; s1 = s2) 99 *s1 = *s2; 100 *s1 = ch; 101 } 102 } else 103 t1 += size; 104 } 105} 106 107void insertion_sort_r(void* bot, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void* arg) 108{ 109 int cnt; 110 unsigned char ch; 111 char *s1, *s2, *t1, *t2, *top; 112 top = (char*)bot + nmemb * size; 113 for (t1 = (char*)bot + size; t1 < top;) { 114 for (t2 = t1; (t2 -= size) >= bot && compar(t1, t2, arg) < 0;) 115 ; 116 if (t1 != (t2 += size)) { 117 for (cnt = size; cnt--; ++t1) { 118 ch = *t1; 119 for (s1 = s2 = t1; (s2 -= size) >= t2; s1 = s2) 120 *s1 = *s2; 121 *s1 = ch; 122 } 123 } else 124 t1 += size; 125 } 126}