From bc328a435bb135d767798374d3653072255f0c48 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 31 Aug 2023 14:38:19 -0400 Subject: [PATCH] libroot: Import a lot of changes to stdlib, string code from FreeBSD. Includes licensing clause removal. Also deleted 2 files that are not used in the build and are not referenced anywhere else. --- src/system/libroot/posix/stdlib/bsearch.c | 40 ++- src/system/libroot/posix/stdlib/heapsort.c | 50 ++-- src/system/libroot/posix/stdlib/merge.c | 254 +++++++++---------- src/system/libroot/posix/stdlib/qsort.c | 200 ++++++++------- src/system/libroot/posix/stdlib/radixsort.c | 105 ++++---- src/system/libroot/posix/stdlib/strtol.c | 37 ++- src/system/libroot/posix/stdlib/strtoll.c | 50 ++-- src/system/libroot/posix/stdlib/strtoq.c | 153 ----------- src/system/libroot/posix/stdlib/strtoul.c | 37 ++- src/system/libroot/posix/stdlib/strtoull.c | 45 ++-- src/system/libroot/posix/stdlib/strtouq.c | 114 --------- src/system/libroot/posix/string/strcasecmp.c | 38 +-- src/system/libroot/posix/string/strcasestr.c | 18 +- 13 files changed, 443 insertions(+), 698 deletions(-) delete mode 100644 src/system/libroot/posix/stdlib/strtoq.c delete mode 100644 src/system/libroot/posix/stdlib/strtouq.c diff --git a/src/system/libroot/posix/stdlib/bsearch.c b/src/system/libroot/posix/stdlib/bsearch.c index 9c51bd1f26..cc533bdcfe 100644 --- a/src/system/libroot/posix/stdlib/bsearch.c +++ b/src/system/libroot/posix/stdlib/bsearch.c @@ -1,4 +1,6 @@ -/* +/*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * @@ -10,11 +12,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -34,10 +32,11 @@ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ - -//#include +#include #include +#define COMPAR(x,y) compar(x, y) + /* * Perform a binary search. * @@ -46,7 +45,7 @@ static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; * is odd, moving left simply involves halving lim: e.g., when lim * is 5 we look at item 2, so we change lim to 2 so that we will * look at items 0 & 1. If lim is even, the same applies. If lim - * is odd, moving right again involes halving lim, this time moving + * is odd, moving right again involves halving lim, this time moving * the base up one item past p: e.g., when lim is 5 we change base * to item 3 and make lim 2 so that we will look at items 3 and 4. * If lim is even, however, we have to shrink it by one before @@ -55,30 +54,23 @@ static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; * look at item 3. */ void * -bsearch( - void const *key, - void const *base0, - size_t nmemb, - size_t size, - int (*compar)(void const *, void const *) -) +bsearch(const void *key, const void *base0, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)) { - char const *base = base0; + const char *base = base0; size_t lim; int cmp; - void const *p; + const void *p; for (lim = nmemb; lim != 0; lim >>= 1) { p = base + (lim >> 1) * size; - cmp = (*compar)(key, p); - if (cmp == 0) { - return (void *)p; - } + cmp = COMPAR(key, p); + if (cmp == 0) + return ((void *)p); if (cmp > 0) { /* key > p: move right */ base = (char *)p + size; lim--; } /* else move left */ } - - return NULL; + return (NULL); } diff --git a/src/system/libroot/posix/stdlib/heapsort.c b/src/system/libroot/posix/stdlib/heapsort.c index 7b39361497..ace76faa58 100644 --- a/src/system/libroot/posix/stdlib/heapsort.c +++ b/src/system/libroot/posix/stdlib/heapsort.c @@ -1,6 +1,10 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2014 David T. Chisnall + * All rights reserved. * * This code is derived from software contributed to Berkeley by * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. @@ -13,11 +17,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -39,9 +39,10 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include +#include #include -#include +#define COMPAR(x, y) compar(x, y) /* * Swap two areas of size number of bytes. Although qsort(3) permits random @@ -78,14 +79,14 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; */ #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ - par_i = child_i) { \ + par_i = child_i) { \ child = base + child_i * size; \ - if (child_i < nmemb && compar(child, child + size) < 0) { \ + if (child_i < nmemb && COMPAR(child, child + size) < 0) { \ child += size; \ ++child_i; \ } \ par = base + par_i * size; \ - if (compar(child, par) <= 0) \ + if (COMPAR(child, par) <= 0) \ break; \ SWAP(par, child, count, size, tmp); \ } \ @@ -95,7 +96,7 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; * Select the top of the heap and 'heapify'. Since by far the most expensive * action is the call to the compar function, a considerable optimization * in the average case can be achieved due to the fact that k, the displaced - * elememt, is ususally quite small, so it would be preferable to first + * elememt, is usually quite small, so it would be preferable to first * heapify, always maintaining the invariant that the larger child is copied * over its parent's record. * @@ -111,7 +112,7 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ child = base + child_i * size; \ - if (child_i < nmemb && compar(child, child + size) < 0) { \ + if (child_i < nmemb && COMPAR(child, child + size) < 0) { \ child += size; \ ++child_i; \ } \ @@ -123,7 +124,7 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; par_i = child_i / 2; \ child = base + child_i * size; \ par = base + par_i * size; \ - if (child_i == 1 || compar(k, par) < 0) { \ + if (child_i == 1 || COMPAR(k, par) < 0) { \ COPY(child, k, count, size, tmp1, tmp2); \ break; \ } \ @@ -139,32 +140,23 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93"; * only advantage over quicksort is that it requires little additional memory. */ int -heapsort(void *vbase, size_t nmemb, size_t size, int (*compar)(void const *, void const *)) +heapsort(void *vbase, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)) { - size_t cnt; - size_t i; - size_t j; - size_t l; - char tmp; - char *tmp1; - char *tmp2; - char *base; - char *k; - char *p; - char *t; + size_t cnt, i, j, l; + char tmp, *tmp1, *tmp2; + char *base, *k, *p, *t; - if (nmemb <= 1) { + if (nmemb <= 1) return (0); - } if (!size) { -// __set_errno(EINVAL); + errno = EINVAL; return (-1); } - if ((k = malloc(size)) == NULL) { + if ((k = malloc(size)) == NULL) return (-1); - } /* * Items are numbered from 1 to nmemb, so offset from size bytes diff --git a/src/system/libroot/posix/stdlib/merge.c b/src/system/libroot/posix/stdlib/merge.c index 2b60c1a0a5..6ef2cfc7c3 100644 --- a/src/system/libroot/posix/stdlib/merge.c +++ b/src/system/libroot/posix/stdlib/merge.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * @@ -13,11 +15,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -53,15 +51,17 @@ static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; */ #include +#include #include #include #include -#include +typedef int (*cmp_t)(const void *, const void *); +#define CMP(x, y) cmp(x, y) -static void setup(u_char *, u_char *, size_t, size_t, int (*)()); -static void insertionsort(u_char *, size_t, size_t, int (*)()); +static void setup(u_char *, u_char *, size_t, size_t, cmp_t); +static void insertionsort(u_char *, size_t, size_t, cmp_t); #define ISIZE sizeof(int) #define PSIZE sizeof(u_char *) @@ -89,47 +89,29 @@ static void insertionsort(u_char *, size_t, size_t, int (*)()); * boundaries. */ /* Assumption: PSIZE is a power of 2. */ -#define EVAL(p) (u_char **) \ - ((u_char *)0 + \ - (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) +#define roundup2(x, y) (((x) + ((y) - 1)) & (~((y) - 1))) +#define EVAL(p) (u_char **)roundup2((uintptr_t)p, PSIZE) /* * Arguments are as for qsort. */ int -mergesort(void *base, size_t nmemb, size_t size, int (*cmp)(void const *, void const *)) +mergesort(void *base, size_t nmemb, size_t size, cmp_t cmp) { size_t i; int sense; int big, iflag; - u_char *f1; - u_char *f2; - u_char *t; - u_char *b; - u_char *tp2; - u_char *q; - u_char *l1; - u_char *l2; - u_char *list2; - u_char *list1; - u_char *p2; - u_char *p; - u_char *last; - u_char **p1; + u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; + u_char *list2, *list1, *p2, *p, *last, **p1; if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ -// __set_errno(EINVAL); + errno = EINVAL; return (-1); } - if (nmemb == 0) { + if (nmemb == 0) return (0); - } - /* - * XXX - * Stupid subtraction for the Cray. - */ iflag = 0; if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) iflag = 1; @@ -142,99 +124,99 @@ mergesort(void *base, size_t nmemb, size_t size, int (*cmp)(void const *, void c last = list2 + nmemb * size; i = big = 0; while (*EVAL(list2) != last) { - l2 = list1; - p1 = EVAL(list1); - for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { - p2 = *EVAL(p2); - f1 = l2; - f2 = l1 = list1 + (p2 - list2); - if (p2 != last) - p2 = *EVAL(p2); - l2 = list1 + (p2 - list2); - while (f1 < l1 && f2 < l2) { - if ((*cmp)(f1, f2) <= 0) { - q = f2; - b = f1, t = l1; - sense = -1; - } else { - q = f1; - b = f2, t = l2; - sense = 0; - } - if (!big) { /* here i = 0 */ - while ((b += size) < t && cmp(q, b) >sense) - if (++i == 6) { - big = 1; - goto EXPONENTIAL; - } - } else { + l2 = list1; + p1 = EVAL(list1); + for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { + p2 = *EVAL(p2); + f1 = l2; + f2 = l1 = list1 + (p2 - list2); + if (p2 != last) + p2 = *EVAL(p2); + l2 = list1 + (p2 - list2); + while (f1 < l1 && f2 < l2) { + if (CMP(f1, f2) <= 0) { + q = f2; + b = f1, t = l1; + sense = -1; + } else { + q = f1; + b = f2, t = l2; + sense = 0; + } + if (!big) { /* here i = 0 */ + while ((b += size) < t && CMP(q, b) >sense) + if (++i == 6) { + big = 1; + goto EXPONENTIAL; + } + } else { EXPONENTIAL: for (i = size; ; i <<= 1) - if ((p = (b + i)) >= t) { - if ((p = t - size) > b && - (*cmp)(q, p) <= sense) - t = p; - else - b = p; - break; - } else if ((*cmp)(q, p) <= sense) { - t = p; - if (i == size) - big = 0; - goto FASTCASE; - } else - b = p; + if ((p = (b + i)) >= t) { + if ((p = t - size) > b && + CMP(q, p) <= sense) + t = p; + else + b = p; + break; + } else if (CMP(q, p) <= sense) { + t = p; + if (i == size) + big = 0; + goto FASTCASE; + } else + b = p; while (t > b+size) { - i = (((t - b) / size) >> 1) * size; - if ((*cmp)(q, p = b + i) <= sense) - t = p; - else - b = p; - } - goto COPY; + i = (((t - b) / size) >> 1) * size; + if (CMP(q, p = b + i) <= sense) + t = p; + else + b = p; + } + goto COPY; FASTCASE: while (i > size) - if ((*cmp)(q, - p = b + (i >>= 1)) <= sense) - t = p; - else - b = p; + if (CMP(q, + p = b + (i >>= 1)) <= sense) + t = p; + else + b = p; COPY: b = t; - } - i = size; - if (q == f1) { - if (iflag) { - ICOPY_LIST(f2, tp2, b); - ICOPY_ELT(f1, tp2, i); - } else { - CCOPY_LIST(f2, tp2, b); - CCOPY_ELT(f1, tp2, i); - } - } else { - if (iflag) { - ICOPY_LIST(f1, tp2, b); - ICOPY_ELT(f2, tp2, i); - } else { - CCOPY_LIST(f1, tp2, b); - CCOPY_ELT(f2, tp2, i); - } - } - } - if (f2 < l2) { - if (iflag) - ICOPY_LIST(f2, tp2, l2); - else - CCOPY_LIST(f2, tp2, l2); - } else if (f1 < l1) { - if (iflag) - ICOPY_LIST(f1, tp2, l1); - else - CCOPY_LIST(f1, tp2, l1); - } - *p1 = l2; - } - tp2 = list1; /* swap list1, list2 */ - list1 = list2; - list2 = tp2; - last = list2 + nmemb*size; + } + i = size; + if (q == f1) { + if (iflag) { + ICOPY_LIST(f2, tp2, b); + ICOPY_ELT(f1, tp2, i); + } else { + CCOPY_LIST(f2, tp2, b); + CCOPY_ELT(f1, tp2, i); + } + } else { + if (iflag) { + ICOPY_LIST(f1, tp2, b); + ICOPY_ELT(f2, tp2, i); + } else { + CCOPY_LIST(f1, tp2, b); + CCOPY_ELT(f2, tp2, i); + } + } + } + if (f2 < l2) { + if (iflag) + ICOPY_LIST(f2, tp2, l2); + else + CCOPY_LIST(f2, tp2, l2); + } else if (f1 < l1) { + if (iflag) + ICOPY_LIST(f1, tp2, l1); + else + CCOPY_LIST(f1, tp2, l1); + } + *p1 = l2; + } + tp2 = list1; /* swap list1, list2 */ + list1 = list2; + list2 = tp2; + last = list2 + nmemb*size; } if (base == list2) { memmove(list2, list1, nmemb*size); @@ -271,7 +253,7 @@ COPY: b = t; */ static void -setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(void const *, void const *)) +setup(u_char *list1, u_char *list2, size_t n, size_t size, cmp_t cmp) { int i, length, size2, tmp, sense; u_char *f1, *f2, *s, *l2, *last, *p2; @@ -294,12 +276,12 @@ setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(void const #ifdef NATURAL p2 = list2; f1 = list1; - sense = (cmp(f1, f1 + size) > 0); + sense = (CMP(f1, f1 + size) > 0); for (; f1 < last; sense = !sense) { length = 2; /* Find pairs with same sense. */ for (f2 = f1 + size2; f2 < last; f2 += size2) { - if ((cmp(f2, f2+ size) > 0) != sense) + if ((CMP(f2, f2+ size) > 0) != sense) break; length += 2; } @@ -312,7 +294,7 @@ setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(void const } else { /* Natural merge */ l2 = f2; for (f2 = f1 + size2; f2 < l2; f2 += size2) { - if ((cmp(f2-size, f2) > 0) != sense) { + if ((CMP(f2-size, f2) > 0) != sense) { p2 = *EVAL(p2) = f2 - list1 + list2; if (sense > 0) reverse(f1, f2-size); @@ -322,7 +304,7 @@ setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(void const if (sense > 0) reverse (f1, f2-size); f1 = f2; - if (f2 < last || cmp(f2 - size, f2) > 0) + if (f2 < last || CMP(f2 - size, f2) > 0) p2 = *EVAL(p2) = f2 - list1 + list2; else p2 = *EVAL(p2) = list2 + n*size; @@ -331,7 +313,7 @@ setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(void const #else /* pairwise merge only. */ for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { p2 = *EVAL(p2) = p2 + size2; - if (cmp (f1, f1 + size) > 0) + if (CMP (f1, f1 + size) > 0) swap(f1, f1 + size); } #endif /* NATURAL */ @@ -341,23 +323,17 @@ setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(void const * This is to avoid out-of-bounds addresses in sorting the * last 4 elements. */ -static -void -insertionsort(u_char *a, size_t n,size_t size, int (*cmp)(const void *, const void *)) +static void +insertionsort(u_char *a, size_t n, size_t size, cmp_t cmp) { - u_char *ai; - u_char *s; - u_char *t; - u_char *u; - u_char tmp; + u_char *ai, *s, *t, *u, tmp; int i; - for (ai = a+size; --n >= 1; ai += size) { + for (ai = a+size; --n >= 1; ai += size) for (t = ai; t > a; t -= size) { u = t - size; - if (cmp(u, t) <= 0) + if (CMP(u, t) <= 0) break; swap(u, t); } - } } diff --git a/src/system/libroot/posix/stdlib/qsort.c b/src/system/libroot/posix/stdlib/qsort.c index 1efad9a1ac..86e27cbbd0 100644 --- a/src/system/libroot/posix/stdlib/qsort.c +++ b/src/system/libroot/posix/stdlib/qsort.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * @@ -10,11 +12,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,85 +29,72 @@ * SUCH DAMAGE. */ +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)qsort.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ +#include +#include #include +#include -#define min(a, b) (a) < (b) ? a : b - - -typedef int cmp_t(void const *, void const *); -static inline char *med3(char *, char *, char *, cmp_t *); -static inline void swapfunc(char *, char *, int, int); +typedef int cmp_t(const void *, const void *); +static inline char *med3(char *, char *, char *, cmp_t *, void *); +#define MIN(a, b) ((a) < (b) ? a : b) /* * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". */ -#define swapcode(TYPE, parmi, parmj, n) { \ - long i = (n) / sizeof (TYPE); \ - register TYPE *pi = (TYPE *) (parmi); \ - register TYPE *pj = (TYPE *) (parmj); \ - do { \ - register TYPE t = *pi; \ - *pi++ = *pj; \ - *pj++ = t; \ - } while (--i > 0); \ + +static inline void +swapfunc(char *a, char *b, size_t es) +{ + char t; + + do { + t = *a; + *a++ = *b; + *b++ = t; + } while (--es > 0); } -#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ - es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; +#define vecswap(a, b, n) \ + if ((n) > 0) swapfunc(a, b, n) -static inline -void -swapfunc(char *a, char *b, int n, int swaptype) +#define CMP(t, x, y) (cmp((x), (y))) + +static inline char * +med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk) { - if(swaptype <= 1) - swapcode(long, a, b, n) - else - swapcode(char, a, b, n) + return CMP(thunk, a, b) < 0 ? + (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) + :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); } -#define swap(a, b) \ - if (swaptype == 0) { \ - long t = *(long *)(a); \ - *(long *)(a) = *(long *)(b); \ - *(long *)(b) = t; \ - } else \ - swapfunc(a, b, es, swaptype) - -#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) - -static inline -char * -med3(char *a, char *b, char *c, cmp_t *cmp) +/* + * The actual qsort() implementation is static to avoid preemptible calls when + * recursing. Also give them different names for improved debugging. + */ +static void +local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) { - return cmp(a, b) < 0 ? - (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) - :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); -} - -void -qsort(void *a, size_t n, size_t es, cmp_t *cmp) -{ - char *pa; - char *pb; - char *pc; - char *pd; - char *pl; - char *pm; - char *pn; - int d; - int r; - int swaptype; + char *pa, *pb, *pc, *pd, *pl, *pm, *pn; + size_t d1, d2; + int cmp_result; int swap_cnt; -loop: SWAPINIT(a, es); + /* if there are less than 2 elements, then sorting is not needed */ + if (n < 2) + return; +loop: swap_cnt = 0; if (n < 7) { for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) - for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; - pl -= es) - swap(pl, pl - es); + for (pl = pm; + pl > (char *)a && CMP(thunk, pl - es, pl) > 0; + pl -= es) + swapfunc(pl, pl - es, es); return; } pm = (char *)a + (n / 2) * es; @@ -117,61 +102,92 @@ loop: SWAPINIT(a, es); pl = a; pn = (char *)a + (n - 1) * es; if (n > 40) { - d = (n / 8) * es; - pl = med3(pl, pl + d, pl + 2 * d, cmp); - pm = med3(pm - d, pm, pm + d, cmp); - pn = med3(pn - 2 * d, pn - d, pn, cmp); + size_t d = (n / 8) * es; + + pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk); + pm = med3(pm - d, pm, pm + d, cmp, thunk); + pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk); } - pm = med3(pl, pm, pn, cmp); + pm = med3(pl, pm, pn, cmp, thunk); } - swap(a, pm); + swapfunc(a, pm, es); pa = pb = (char *)a + es; pc = pd = (char *)a + (n - 1) * es; for (;;) { - while (pb <= pc && (r = cmp(pb, a)) <= 0) { - if (r == 0) { + while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) { + if (cmp_result == 0) { swap_cnt = 1; - swap(pa, pb); + swapfunc(pa, pb, es); pa += es; } pb += es; } - while (pb <= pc && (r = cmp(pc, a)) >= 0) { - if (r == 0) { + while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) { + if (cmp_result == 0) { swap_cnt = 1; - swap(pc, pd); + swapfunc(pc, pd, es); pd -= es; } pc -= es; } if (pb > pc) break; - swap(pb, pc); + swapfunc(pb, pc, es); swap_cnt = 1; pb += es; pc -= es; } if (swap_cnt == 0) { /* Switch to insertion sort */ for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) - for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; - pl -= es) - swap(pl, pl - es); + for (pl = pm; + pl > (char *)a && CMP(thunk, pl - es, pl) > 0; + pl -= es) + swapfunc(pl, pl - es, es); return; } pn = (char *)a + n * es; - r = min(pa - (char *)a, pb - pa); - vecswap(a, pb - r, r); - r = min((int)(pd - pc), (int)(pn - pd - es)); - vecswap(pb, pn - r, r); - if ((r = pb - pa) > (int)es) - qsort(a, r / es, es, cmp); - if ((r = pd - pc) > (int)es) { - /* Iterate rather than recurse to save stack space */ - a = pn - r; - n = r / es; - goto loop; + d1 = MIN(pa - (char *)a, pb - pa); + vecswap(a, pb - d1, d1); + /* + * Cast es to preserve signedness of right-hand side of MIN() + * expression, to avoid sign ambiguity in the implied comparison. es + * is safely within [0, SSIZE_MAX]. + */ + d1 = MIN(pd - pc, pn - pd - (ssize_t)es); + vecswap(pb, pn - d1, d1); + + d1 = pb - pa; + d2 = pd - pc; + if (d1 <= d2) { + /* Recurse on left partition, then iterate on right partition */ + if (d1 > es) { + local_qsort(a, d1 / es, es, cmp, thunk); + } + if (d2 > es) { + /* Iterate rather than recurse to save stack space */ + /* qsort(pn - d2, d2 / es, es, cmp); */ + a = pn - d2; + n = d2 / es; + goto loop; + } + } else { + /* Recurse on right partition, then iterate on left partition */ + if (d2 > es) { + local_qsort(pn - d2, d2 / es, es, cmp, thunk); + } + if (d1 > es) { + /* Iterate rather than recurse to save stack space */ + /* qsort(a, d1 / es, es, cmp); */ + n = d1 / es; + goto loop; + } } -/* qsort(pn - r, r / es, es, cmp);*/ +} + +void +qsort(void *a, size_t n, size_t es, cmp_t *cmp) +{ + local_qsort(a, n, es, cmp, NULL); } diff --git a/src/system/libroot/posix/stdlib/radixsort.c b/src/system/libroot/posix/stdlib/radixsort.c index d8f5d6dbc7..47bc4bd0e3 100644 --- a/src/system/libroot/posix/stdlib/radixsort.c +++ b/src/system/libroot/posix/stdlib/radixsort.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * @@ -13,11 +15,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -53,18 +51,19 @@ static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95"; #include #include +#include #include -#include - typedef struct { const u_char **sa; int sn, si; } stack; -static inline void simplesort(u_char const **, int, int, u_char const *, u_int); -static void r_sort_a(u_char const **, int, int, u_char const *, u_int); -static void r_sort_b(u_char const **, u_char const **, int, int, u_char const *, u_int); +static inline void simplesort +(const u_char **, int, int, const u_char *, u_int); +static void r_sort_a(const u_char **, int, int, const u_char *, u_int); +static void r_sort_b(const u_char **, const u_char **, int, int, + const u_char *, u_int); #define THRESHOLD 20 /* Divert to simplesort(). */ #define SIZE 512 /* Default stack size. */ @@ -82,17 +81,17 @@ static void r_sort_b(u_char const **, u_char const **, int, int, u_char const *, endch = tab[endch]; \ tr = tab; \ if (endch != 0 && endch != 255) { \ - /* __set_errno(EINVAL); */ \ + errno = EINVAL; \ return (-1); \ } \ } \ } int -radixsort(u_char const **a, int n, u_char const *tab, u_int endch) +radixsort(const u_char **a, int n, const u_char *tab, u_int endch) { - u_char const *tr; - u_int c; + const u_char *tr; + int c; u_char tr0[256]; SETUP; @@ -101,11 +100,10 @@ radixsort(u_char const **a, int n, u_char const *tab, u_int endch) } int -sradixsort(u_char const **a, int n, u_char const *tab, u_int endch) +sradixsort(const u_char **a, int n, const u_char *tab, u_int endch) { - u_char const *tr; - u_char const **ta; - u_int c; + const u_char *tr, **ta; + int c; u_char tr0[256]; SETUP; @@ -127,25 +125,14 @@ sradixsort(u_char const **a, int n, u_char const *tab, u_int endch) /* Unstable, in-place sort. */ static void -r_sort_a(u_char const **a, int n, int i, u_char const *tr, u_int endch) +r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch) { - static u_int count[256]; - static int nc; - static u_int bmin; - u_int c; - u_char const **ak; - u_char const *r; - stack s[SIZE]; - stack *sp; - stack *sp0; - stack *sp1; - stack temp; - u_int *cp; - u_int bigc; - u_char const **an; - u_char const *t; - u_char const **aj; - u_char const **top[256]; + static int count[256], nc, bmin; + int c; + const u_char **ak, *r; + stack s[SIZE], *sp, *sp0, *sp1, temp; + int *cp, bigc; + const u_char **an, *t, **aj, **top[256]; /* Set up stack. */ sp = s; @@ -175,6 +162,17 @@ r_sort_a(u_char const **a, int n, int i, u_char const *tr, u_int endch) } } + /* + * Special case: if all strings have the same + * character at position i, move on to the next + * character. + */ + if (nc == 1 && count[bmin] == n) { + push(a, n, i+1); + nc = count[bmin] = 0; + continue; + } + /* * Set top[]; push incompletely sorted bins onto stack. * top[] = pointers to last out-of-place element in bins. @@ -224,19 +222,16 @@ r_sort_a(u_char const **a, int n, int i, u_char const *tr, u_int endch) } /* Stable sort, requiring additional memory. */ -static -void -r_sort_b(u_char const **a, u_char const **ta, int n, int i, u_char const *tr, u_int endch) +static void +r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr, + u_int endch) { - static int count[256]; - static u_int nc; - static u_int bmin; - u_int c; - u_char const **ak, **ai; + static int count[256], nc, bmin; + int c; + const u_char **ak, **ai; stack s[512], *sp, *sp0, *sp1, temp; - u_char const **top[256]; - int *cp; - u_int bigc; + const u_char **top[256]; + int *cp, bigc; sp = s; push(a, n, i); @@ -296,25 +291,21 @@ r_sort_b(u_char const **a, u_char const **ta, int n, int i, u_char const *tr, u_ } } -static inline -void -simplesort(u_char const **a, int n, int b, u_char const *tr, u_int endch) /* insertion sort */ +/* insertion sort */ +static inline void +simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch) { u_char ch; - u_char const **ak; - u_char const **ai; - u_char const *s; - u_char const *t; + const u_char **ak, **ai, *s, *t; - for (ak = a+1; --n >= 1; ak++) { + for (ak = a+1; --n >= 1; ak++) for (ai = ak; ai > a; ai--) { for (s = ai[0] + b, t = ai[-1] + b; - (ch = tr[*s]) != endch; s++, t++) + (ch = tr[*s]) != endch; s++, t++) if (ch != tr[*t]) break; if (ch >= tr[*t]) break; swap(ai[0], ai[-1], s); } - } } diff --git a/src/system/libroot/posix/stdlib/strtol.c b/src/system/libroot/posix/stdlib/strtol.c index ba005fee0d..d6b411d215 100644 --- a/src/system/libroot/posix/stdlib/strtol.c +++ b/src/system/libroot/posix/stdlib/strtol.c @@ -1,7 +1,14 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * + * Copyright (c) 2011 The FreeBSD Foundation + * + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -10,11 +17,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -37,9 +40,6 @@ #include #include -#include - - /* * Convert a string to a long integer. * @@ -74,11 +74,21 @@ strtol(const char * __restrict nptr, char ** __restrict endptr, int base) c = *s++; } if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { + c == '0' && (*s == 'x' || *s == 'X') && + ((s[1] >= '0' && s[1] <= '9') || + (s[1] >= 'A' && s[1] <= 'F') || + (s[1] >= 'a' && s[1] <= 'f'))) { c = s[1]; s += 2; base = 16; } + if ((base == 0 || base == 2) && + c == '0' && (*s == 'b' || *s == 'B') && + (s[1] >= '0' && s[1] <= '1')) { + c = s[1]; + s += 2; + base = 2; + } if (base == 0) base = c == '0' ? 8 : 10; acc = any = 0; @@ -103,7 +113,7 @@ strtol(const char * __restrict nptr, char ** __restrict endptr, int base) * overflow. */ cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX - : LONG_MAX; + : LONG_MAX; cutlim = cutoff % base; cutoff /= base; for ( ; ; c = *s++) { @@ -127,10 +137,10 @@ strtol(const char * __restrict nptr, char ** __restrict endptr, int base) } if (any < 0) { acc = neg ? LONG_MIN : LONG_MAX; - __set_errno(ERANGE); + errno = ERANGE; } else if (!any) { noconv: - __set_errno(EINVAL); + errno = EINVAL; } else if (neg) acc = -acc; if (endptr != NULL) @@ -139,6 +149,7 @@ noconv: } +#ifdef __HAIKU__ long __strtol_internal(const char *number, char **_end, int base, int group); long @@ -149,4 +160,4 @@ __strtol_internal(const char *number, char **_end, int base, int group) return strtol(number, _end, base); } - +#endif diff --git a/src/system/libroot/posix/stdlib/strtoll.c b/src/system/libroot/posix/stdlib/strtoll.c index 59d5cd72f8..056aac95b2 100644 --- a/src/system/libroot/posix/stdlib/strtoll.c +++ b/src/system/libroot/posix/stdlib/strtoll.c @@ -1,7 +1,14 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * + * Copyright (c) 2011 The FreeBSD Foundation + * + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -10,11 +17,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -37,16 +40,12 @@ #include #include -#include - - /* * Convert a string to a long long integer. * * Assumes that the upper and lower case * alphabets and digits are each contiguous. */ - long long strtoll(const char * __restrict nptr, char ** __restrict endptr, int base) { @@ -58,8 +57,9 @@ strtoll(const char * __restrict nptr, char ** __restrict endptr, int base) /* * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. + * If base is 0, allow 0b for binary, 0x for hex, and 0 for + * octal, else assume decimal; if base is already 2, allow + * 0b; if base is already 16, allow 0x. */ s = nptr; do { @@ -74,11 +74,21 @@ strtoll(const char * __restrict nptr, char ** __restrict endptr, int base) c = *s++; } if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { + c == '0' && (*s == 'x' || *s == 'X') && + ((s[1] >= '0' && s[1] <= '9') || + (s[1] >= 'A' && s[1] <= 'F') || + (s[1] >= 'a' && s[1] <= 'f'))) { c = s[1]; s += 2; base = 16; } + if ((base == 0 || base == 2) && + c == '0' && (*s == 'b' || *s == 'B') && + (s[1] >= '0' && s[1] <= '1')) { + c = s[1]; + s += 2; + base = 2; + } if (base == 0) base = c == '0' ? 8 : 10; acc = any = 0; @@ -103,8 +113,8 @@ strtoll(const char * __restrict nptr, char ** __restrict endptr, int base) * Set 'any' if any `digits' consumed; make it negative to indicate * overflow. */ - cutoff = neg ? (unsigned long long)-(LONGLONG_MIN + LONGLONG_MAX) + LONGLONG_MAX - : LONGLONG_MAX; + cutoff = neg ? (unsigned long long)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX + : LLONG_MAX; cutlim = cutoff % base; cutoff /= base; for ( ; ; c = *s++) { @@ -127,20 +137,20 @@ strtoll(const char * __restrict nptr, char ** __restrict endptr, int base) } } if (any < 0) { - acc = neg ? LONGLONG_MIN : LONGLONG_MAX; - __set_errno(ERANGE); + acc = neg ? LLONG_MIN : LLONG_MAX; + errno = ERANGE; } else if (!any) { noconv: - __set_errno(EINVAL); + errno = EINVAL; } else if (neg) acc = -acc; if (endptr != NULL) *endptr = (char *)(any ? s - 1 : nptr); - - return acc; + return (acc); } +#ifdef __HAIKU__ long long __strtoll_internal(const char *number, char **_end, int base, int group); long long @@ -151,4 +161,4 @@ __strtoll_internal(const char *number, char **_end, int base, int group) return strtoll(number, _end, base); } - +#endif diff --git a/src/system/libroot/posix/stdlib/strtoq.c b/src/system/libroot/posix/stdlib/strtoq.c deleted file mode 100644 index 075b6a3b95..0000000000 --- a/src/system/libroot/posix/stdlib/strtoq.c +++ /dev/null @@ -1,153 +0,0 @@ -/*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. - * - * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 - -#include -#include -//#include -#include - -#include - - -#define QUAD_MIN (-0x7fffffffffffffffLL - 1) -#define QUAD_MAX 0x7fffffffffffffffLL -/* - * Convert a string to a quad integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -int64 -strtoq(nptr, endptr, base) - const char *nptr; - char **endptr; - register int base; -{ - register const char *s; - register int64 acc, cutoff; - register int c; - register int neg, any, cutlim; - - /* - * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - /* - * Compute the cutoff value between legal numbers and illegal - * numbers. That is the largest legal value, divided by the - * base. An input number that is greater than this value, if - * followed by a legal input character, is too big. One that - * is equal to this value may be valid or not; the limit - * between valid and invalid numbers is then based on the last - * digit. For instance, if the range for quads is - * [-9223372036854775808..9223372036854775807] and the input base - * is 10, cutoff will be set to 922337203685477580 and cutlim to - * either 7 (neg==0) or 8 (neg==1), meaning that if we have - * accumulated a value > 922337203685477580, or equal but the - * next digit is > 7 (or 8), the number is too big, and we will - * return a range error. - * - * Set any if any `digits' consumed; make it negative to indicate - * overflow. - */ - cutoff = neg ? QUAD_MIN : QUAD_MAX; - cutlim = cutoff % base; - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = QUAD_MIN; - __set_errno(ERANGE); - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = QUAD_MAX; - __set_errno(ERANGE); - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} diff --git a/src/system/libroot/posix/stdlib/strtoul.c b/src/system/libroot/posix/stdlib/strtoul.c index 4194d8e71d..bcff53b2c9 100644 --- a/src/system/libroot/posix/stdlib/strtoul.c +++ b/src/system/libroot/posix/stdlib/strtoul.c @@ -1,7 +1,14 @@ -/* +/*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * + * Copyright (c) 2011 The FreeBSD Foundation + * + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -10,11 +17,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -37,9 +40,6 @@ #include #include -#include - - /* * Convert a string to an unsigned long integer. * @@ -72,11 +72,21 @@ strtoul(const char * __restrict nptr, char ** __restrict endptr, int base) c = *s++; } if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { + c == '0' && (*s == 'x' || *s == 'X') && + ((s[1] >= '0' && s[1] <= '9') || + (s[1] >= 'A' && s[1] <= 'F') || + (s[1] >= 'a' && s[1] <= 'f'))) { c = s[1]; s += 2; base = 16; } + if ((base == 0 || base == 2) && + c == '0' && (*s == 'b' || *s == 'B') && + (s[1] >= '0' && s[1] <= '1')) { + c = s[1]; + s += 2; + base = 2; + } if (base == 0) base = c == '0' ? 8 : 10; acc = any = 0; @@ -106,10 +116,10 @@ strtoul(const char * __restrict nptr, char ** __restrict endptr, int base) } if (any < 0) { acc = ULONG_MAX; - __set_errno(ERANGE); + errno = ERANGE; } else if (!any) { noconv: - __set_errno(EINVAL); + errno = EINVAL; } else if (neg) acc = -acc; if (endptr != NULL) @@ -118,6 +128,7 @@ noconv: } +#ifdef __HAIKU__ unsigned long __strtoul_internal(const char *number, char **_end, int base, int group); unsigned long @@ -128,4 +139,4 @@ __strtoul_internal(const char *number, char **_end, int base, int group) return strtoul(number, _end, base); } - +#endif diff --git a/src/system/libroot/posix/stdlib/strtoull.c b/src/system/libroot/posix/stdlib/strtoull.c index e36474d8ea..f42e832d9f 100644 --- a/src/system/libroot/posix/stdlib/strtoull.c +++ b/src/system/libroot/posix/stdlib/strtoull.c @@ -1,7 +1,14 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * + * Copyright (c) 2011 The FreeBSD Foundation + * + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -10,11 +17,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -37,16 +40,12 @@ #include #include -#include - - /* * Convert a string to an unsigned long long integer. * * Assumes that the upper and lower case * alphabets and digits are each contiguous. */ - unsigned long long strtoull(const char * __restrict nptr, char ** __restrict endptr, int base) { @@ -72,19 +71,29 @@ strtoull(const char * __restrict nptr, char ** __restrict endptr, int base) c = *s++; } if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { + c == '0' && (*s == 'x' || *s == 'X') && + ((s[1] >= '0' && s[1] <= '9') || + (s[1] >= 'A' && s[1] <= 'F') || + (s[1] >= 'a' && s[1] <= 'f'))) { c = s[1]; s += 2; base = 16; } + if ((base == 0 || base == 2) && + c == '0' && (*s == 'b' || *s == 'B') && + (s[1] >= '0' && s[1] <= '1')) { + c = s[1]; + s += 2; + base = 2; + } if (base == 0) base = c == '0' ? 8 : 10; acc = any = 0; if (base < 2 || base > 36) goto noconv; - cutoff = ULONGLONG_MAX / base; - cutlim = ULONGLONG_MAX % base; + cutoff = ULLONG_MAX / base; + cutlim = ULLONG_MAX % base; for ( ; ; c = *s++) { if (c >= '0' && c <= '9') c -= '0'; @@ -105,20 +114,20 @@ strtoull(const char * __restrict nptr, char ** __restrict endptr, int base) } } if (any < 0) { - acc = ULONGLONG_MAX; - __set_errno(ERANGE); + acc = ULLONG_MAX; + errno = ERANGE; } else if (!any) { noconv: - __set_errno(EINVAL); + errno = EINVAL; } else if (neg) acc = -acc; if (endptr != NULL) *endptr = (char *)(any ? s - 1 : nptr); - - return acc; + return (acc); } +#ifdef __HAIKU__ unsigned long long __strtoull_internal(const char *number, char **_end, int base, int group); unsigned long long @@ -129,4 +138,4 @@ __strtoull_internal(const char *number, char **_end, int base, int group) return strtoull(number, _end, base); } - +#endif diff --git a/src/system/libroot/posix/stdlib/strtouq.c b/src/system/libroot/posix/stdlib/strtouq.c deleted file mode 100644 index 2dfce976b5..0000000000 --- a/src/system/libroot/posix/stdlib/strtouq.c +++ /dev/null @@ -1,114 +0,0 @@ -/*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. - * - * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 - -#include -#include -//#include -#include - -#include - - -#define UQUAD_MAX 0xffffffffffffffffULL -/* - * Convert a string to an unsigned quad integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -uint64 -strtouq(nptr, endptr, base) - const char *nptr; - char **endptr; - register int base; -{ - register const char *s; - register uint64 acc, cutoff; - register int c; - register int neg, any, cutlim; - - /* - * See strtoq for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - cutoff = UQUAD_MAX / (uint64)base; - cutlim = UQUAD_MAX % (uint64)base; - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = UQUAD_MAX; - __set_errno(ERANGE); - } else { - any = 1; - acc *= (uint64)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} diff --git a/src/system/libroot/posix/string/strcasecmp.c b/src/system/libroot/posix/string/strcasecmp.c index f43b437959..56781e4b79 100644 --- a/src/system/libroot/posix/string/strcasecmp.c +++ b/src/system/libroot/posix/string/strcasecmp.c @@ -1,7 +1,14 @@ -/* +/*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1987, 1993 * The Regents of the University of California. All rights reserved. * + * Copyright (c) 2011 The FreeBSD Foundation + * + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -10,11 +17,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,7 +34,6 @@ * SUCH DAMAGE. */ - #include #include @@ -72,15 +74,14 @@ strncasecmp(const char *s1, const char *s2, size_t n) int strcasecmp_l(const char *s1, const char *s2, locale_t locale) { - const u_char *us1 = (const u_char *)s1; - const u_char *us2 = (const u_char *)s2; + const u_char + *us1 = (const u_char *)s1, + *us2 = (const u_char *)s2; - while (tolower_l(*us1, locale) == tolower_l(*us2++, locale)) { + while (tolower_l(*us1, locale) == tolower_l(*us2++, locale)) if (*us1++ == '\0') - return 0; - } - - return tolower_l(*us1, locale) - tolower_l(*--us2, locale); + return (0); + return (tolower_l(*us1, locale) - tolower_l(*--us2, locale)); } @@ -88,15 +89,16 @@ int strncasecmp_l(const char *s1, const char *s2, size_t n, locale_t locale) { if (n != 0) { - const u_char *us1 = (const u_char *)s1; - const u_char *us2 = (const u_char *)s2; + const u_char + *us1 = (const u_char *)s1, + *us2 = (const u_char *)s2; do { if (tolower_l(*us1, locale) != tolower_l(*us2++, locale)) - return tolower_l(*us1, locale) - tolower_l(*--us2, locale); + return (tolower_l(*us1, locale) - tolower_l(*--us2, locale)); if (*us1++ == '\0') break; } while (--n != 0); } - return 0; + return (0); } diff --git a/src/system/libroot/posix/string/strcasestr.c b/src/system/libroot/posix/string/strcasestr.c index d979e875e5..133b62da51 100644 --- a/src/system/libroot/posix/string/strcasestr.c +++ b/src/system/libroot/posix/string/strcasestr.c @@ -1,10 +1,17 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * + * Copyright (c) 2011 The FreeBSD Foundation + * + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -13,11 +20,7 @@ * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -55,11 +58,10 @@ strcasestr(const char *s, const char *find) do { do { if ((sc = *s++) == 0) - return NULL; + return (NULL); } while ((char)tolower((unsigned char)sc) != c); } while (strncasecmp(s, find, len) != 0); s--; } - return (char *)s; + return ((char *)s); } -