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.
This commit is contained in:
Augustin Cavalier
2023-08-31 15:10:19 -04:00
parent a573f9e4be
commit bc328a435b
13 changed files with 443 additions and 698 deletions
+16 -24
View File
@@ -1,4 +1,6 @@
/* /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
* *
@@ -10,11 +12,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -34,10 +32,11 @@
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <stddef.h>
//#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#define COMPAR(x,y) compar(x, y)
/* /*
* Perform a binary search. * 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 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 * 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 * 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 * 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. * 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 * 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. * look at item 3.
*/ */
void * void *
bsearch( bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
void const *key, int (*compar)(const void *, const void *))
void const *base0,
size_t nmemb,
size_t size,
int (*compar)(void const *, void const *)
)
{ {
char const *base = base0; const char *base = base0;
size_t lim; size_t lim;
int cmp; int cmp;
void const *p; const void *p;
for (lim = nmemb; lim != 0; lim >>= 1) { for (lim = nmemb; lim != 0; lim >>= 1) {
p = base + (lim >> 1) * size; p = base + (lim >> 1) * size;
cmp = (*compar)(key, p); cmp = COMPAR(key, p);
if (cmp == 0) { if (cmp == 0)
return (void *)p; return ((void *)p);
}
if (cmp > 0) { /* key > p: move right */ if (cmp > 0) { /* key > p: move right */
base = (char *)p + size; base = (char *)p + size;
lim--; lim--;
} /* else move left */ } /* else move left */
} }
return (NULL);
return NULL;
} }
+21 -29
View File
@@ -1,6 +1,10 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1991, 1993 * Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved. * 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 * This code is derived from software contributed to Berkeley by
* Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
@@ -13,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * 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 */ #endif /* LIBC_SCCS and not lint */
#include <errno.h> #include <errno.h>
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno_private.h> #define COMPAR(x, y) compar(x, y)
/* /*
* Swap two areas of size number of bytes. Although qsort(3) permits random * 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) { \ #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
par_i = child_i) { \ par_i = child_i) { \
child = base + child_i * size; \ 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 += size; \
++child_i; \ ++child_i; \
} \ } \
par = base + par_i * size; \ par = base + par_i * size; \
if (compar(child, par) <= 0) \ if (COMPAR(child, par) <= 0) \
break; \ break; \
SWAP(par, child, count, size, tmp); \ 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 * 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 * 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 * 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 * heapify, always maintaining the invariant that the larger child is copied
* over its parent's record. * 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) { \ #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) { \ for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
child = base + child_i * size; \ 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 += size; \
++child_i; \ ++child_i; \
} \ } \
@@ -123,7 +124,7 @@ static char sccsid[] = "@(#)heapsort.c 8.1 (Berkeley) 6/4/93";
par_i = child_i / 2; \ par_i = child_i / 2; \
child = base + child_i * size; \ child = base + child_i * size; \
par = base + par_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); \ COPY(child, k, count, size, tmp1, tmp2); \
break; \ 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. * only advantage over quicksort is that it requires little additional memory.
*/ */
int 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 cnt, i, j, l;
size_t i; char tmp, *tmp1, *tmp2;
size_t j; char *base, *k, *p, *t;
size_t l;
char tmp;
char *tmp1;
char *tmp2;
char *base;
char *k;
char *p;
char *t;
if (nmemb <= 1) { if (nmemb <= 1)
return (0); return (0);
}
if (!size) { if (!size) {
// __set_errno(EINVAL); errno = EINVAL;
return (-1); return (-1);
} }
if ((k = malloc(size)) == NULL) { if ((k = malloc(size)) == NULL)
return (-1); return (-1);
}
/* /*
* Items are numbered from 1 to nmemb, so offset from size bytes * Items are numbered from 1 to nmemb, so offset from size bytes
+115 -139
View File
@@ -1,4 +1,6 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
* *
@@ -13,11 +15,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -53,15 +51,17 @@ static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <stdint.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno_private.h> 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 setup(u_char *, u_char *, size_t, size_t, cmp_t);
static void insertionsort(u_char *, size_t, size_t, int (*)()); static void insertionsort(u_char *, size_t, size_t, cmp_t);
#define ISIZE sizeof(int) #define ISIZE sizeof(int)
#define PSIZE sizeof(u_char *) #define PSIZE sizeof(u_char *)
@@ -89,47 +89,29 @@ static void insertionsort(u_char *, size_t, size_t, int (*)());
* boundaries. * boundaries.
*/ */
/* Assumption: PSIZE is a power of 2. */ /* Assumption: PSIZE is a power of 2. */
#define EVAL(p) (u_char **) \ #define roundup2(x, y) (((x) + ((y) - 1)) & (~((y) - 1)))
((u_char *)0 + \ #define EVAL(p) (u_char **)roundup2((uintptr_t)p, PSIZE)
(((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
/* /*
* Arguments are as for qsort. * Arguments are as for qsort.
*/ */
int 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; size_t i;
int sense; int sense;
int big, iflag; int big, iflag;
u_char *f1; u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
u_char *f2; u_char *list2, *list1, *p2, *p, *last, **p1;
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;
if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
// __set_errno(EINVAL); errno = EINVAL;
return (-1); return (-1);
} }
if (nmemb == 0) { if (nmemb == 0)
return (0); return (0);
}
/*
* XXX
* Stupid subtraction for the Cray.
*/
iflag = 0; iflag = 0;
if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
iflag = 1; 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; last = list2 + nmemb * size;
i = big = 0; i = big = 0;
while (*EVAL(list2) != last) { while (*EVAL(list2) != last) {
l2 = list1; l2 = list1;
p1 = EVAL(list1); p1 = EVAL(list1);
for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
p2 = *EVAL(p2); p2 = *EVAL(p2);
f1 = l2; f1 = l2;
f2 = l1 = list1 + (p2 - list2); f2 = l1 = list1 + (p2 - list2);
if (p2 != last) if (p2 != last)
p2 = *EVAL(p2); p2 = *EVAL(p2);
l2 = list1 + (p2 - list2); l2 = list1 + (p2 - list2);
while (f1 < l1 && f2 < l2) { while (f1 < l1 && f2 < l2) {
if ((*cmp)(f1, f2) <= 0) { if (CMP(f1, f2) <= 0) {
q = f2; q = f2;
b = f1, t = l1; b = f1, t = l1;
sense = -1; sense = -1;
} else { } else {
q = f1; q = f1;
b = f2, t = l2; b = f2, t = l2;
sense = 0; sense = 0;
} }
if (!big) { /* here i = 0 */ if (!big) { /* here i = 0 */
while ((b += size) < t && cmp(q, b) >sense) while ((b += size) < t && CMP(q, b) >sense)
if (++i == 6) { if (++i == 6) {
big = 1; big = 1;
goto EXPONENTIAL; goto EXPONENTIAL;
} }
} else { } else {
EXPONENTIAL: for (i = size; ; i <<= 1) EXPONENTIAL: for (i = size; ; i <<= 1)
if ((p = (b + i)) >= t) { if ((p = (b + i)) >= t) {
if ((p = t - size) > b && if ((p = t - size) > b &&
(*cmp)(q, p) <= sense) CMP(q, p) <= sense)
t = p; t = p;
else else
b = p; b = p;
break; break;
} else if ((*cmp)(q, p) <= sense) { } else if (CMP(q, p) <= sense) {
t = p; t = p;
if (i == size) if (i == size)
big = 0; big = 0;
goto FASTCASE; goto FASTCASE;
} else } else
b = p; b = p;
while (t > b+size) { while (t > b+size) {
i = (((t - b) / size) >> 1) * size; i = (((t - b) / size) >> 1) * size;
if ((*cmp)(q, p = b + i) <= sense) if (CMP(q, p = b + i) <= sense)
t = p; t = p;
else else
b = p; b = p;
} }
goto COPY; goto COPY;
FASTCASE: while (i > size) FASTCASE: while (i > size)
if ((*cmp)(q, if (CMP(q,
p = b + (i >>= 1)) <= sense) p = b + (i >>= 1)) <= sense)
t = p; t = p;
else else
b = p; b = p;
COPY: b = t; COPY: b = t;
} }
i = size; i = size;
if (q == f1) { if (q == f1) {
if (iflag) { if (iflag) {
ICOPY_LIST(f2, tp2, b); ICOPY_LIST(f2, tp2, b);
ICOPY_ELT(f1, tp2, i); ICOPY_ELT(f1, tp2, i);
} else { } else {
CCOPY_LIST(f2, tp2, b); CCOPY_LIST(f2, tp2, b);
CCOPY_ELT(f1, tp2, i); CCOPY_ELT(f1, tp2, i);
} }
} else { } else {
if (iflag) { if (iflag) {
ICOPY_LIST(f1, tp2, b); ICOPY_LIST(f1, tp2, b);
ICOPY_ELT(f2, tp2, i); ICOPY_ELT(f2, tp2, i);
} else { } else {
CCOPY_LIST(f1, tp2, b); CCOPY_LIST(f1, tp2, b);
CCOPY_ELT(f2, tp2, i); CCOPY_ELT(f2, tp2, i);
} }
} }
} }
if (f2 < l2) { if (f2 < l2) {
if (iflag) if (iflag)
ICOPY_LIST(f2, tp2, l2); ICOPY_LIST(f2, tp2, l2);
else else
CCOPY_LIST(f2, tp2, l2); CCOPY_LIST(f2, tp2, l2);
} else if (f1 < l1) { } else if (f1 < l1) {
if (iflag) if (iflag)
ICOPY_LIST(f1, tp2, l1); ICOPY_LIST(f1, tp2, l1);
else else
CCOPY_LIST(f1, tp2, l1); CCOPY_LIST(f1, tp2, l1);
} }
*p1 = l2; *p1 = l2;
} }
tp2 = list1; /* swap list1, list2 */ tp2 = list1; /* swap list1, list2 */
list1 = list2; list1 = list2;
list2 = tp2; list2 = tp2;
last = list2 + nmemb*size; last = list2 + nmemb*size;
} }
if (base == list2) { if (base == list2) {
memmove(list2, list1, nmemb*size); memmove(list2, list1, nmemb*size);
@@ -271,7 +253,7 @@ COPY: b = t;
*/ */
static static
void 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; int i, length, size2, tmp, sense;
u_char *f1, *f2, *s, *l2, *last, *p2; 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 #ifdef NATURAL
p2 = list2; p2 = list2;
f1 = list1; f1 = list1;
sense = (cmp(f1, f1 + size) > 0); sense = (CMP(f1, f1 + size) > 0);
for (; f1 < last; sense = !sense) { for (; f1 < last; sense = !sense) {
length = 2; length = 2;
/* Find pairs with same sense. */ /* Find pairs with same sense. */
for (f2 = f1 + size2; f2 < last; f2 += size2) { for (f2 = f1 + size2; f2 < last; f2 += size2) {
if ((cmp(f2, f2+ size) > 0) != sense) if ((CMP(f2, f2+ size) > 0) != sense)
break; break;
length += 2; 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 */ } else { /* Natural merge */
l2 = f2; l2 = f2;
for (f2 = f1 + size2; f2 < l2; f2 += size2) { 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; p2 = *EVAL(p2) = f2 - list1 + list2;
if (sense > 0) if (sense > 0)
reverse(f1, f2-size); 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) if (sense > 0)
reverse (f1, f2-size); reverse (f1, f2-size);
f1 = f2; f1 = f2;
if (f2 < last || cmp(f2 - size, f2) > 0) if (f2 < last || CMP(f2 - size, f2) > 0)
p2 = *EVAL(p2) = f2 - list1 + list2; p2 = *EVAL(p2) = f2 - list1 + list2;
else else
p2 = *EVAL(p2) = list2 + n*size; 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. */ #else /* pairwise merge only. */
for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
p2 = *EVAL(p2) = p2 + size2; p2 = *EVAL(p2) = p2 + size2;
if (cmp (f1, f1 + size) > 0) if (CMP (f1, f1 + size) > 0)
swap(f1, f1 + size); swap(f1, f1 + size);
} }
#endif /* NATURAL */ #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 * This is to avoid out-of-bounds addresses in sorting the
* last 4 elements. * last 4 elements.
*/ */
static static void
void insertionsort(u_char *a, size_t n, size_t size, cmp_t cmp)
insertionsort(u_char *a, size_t n,size_t size, int (*cmp)(const void *, const void *))
{ {
u_char *ai; u_char *ai, *s, *t, *u, tmp;
u_char *s;
u_char *t;
u_char *u;
u_char tmp;
int i; 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) { for (t = ai; t > a; t -= size) {
u = t - size; u = t - size;
if (cmp(u, t) <= 0) if (CMP(u, t) <= 0)
break; break;
swap(u, t); swap(u, t);
} }
}
} }
+108 -92
View File
@@ -1,4 +1,6 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
* *
@@ -10,11 +12,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -31,85 +29,72 @@
* SUCH DAMAGE. * 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 <errno.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define min(a, b) (a) < (b) ? a : b typedef int cmp_t(const void *, const void *);
static inline char *med3(char *, char *, char *, cmp_t *, void *);
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);
#define MIN(a, b) ((a) < (b) ? a : b)
/* /*
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*/ */
#define swapcode(TYPE, parmi, parmj, n) { \
long i = (n) / sizeof (TYPE); \ static inline void
register TYPE *pi = (TYPE *) (parmi); \ swapfunc(char *a, char *b, size_t es)
register TYPE *pj = (TYPE *) (parmj); \ {
do { \ char t;
register TYPE t = *pi; \
*pi++ = *pj; \ do {
*pj++ = t; \ t = *a;
} while (--i > 0); \ *a++ = *b;
*b++ = t;
} while (--es > 0);
} }
#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ #define vecswap(a, b, n) \
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; if ((n) > 0) swapfunc(a, b, n)
static inline #define CMP(t, x, y) (cmp((x), (y)))
void
swapfunc(char *a, char *b, int n, int swaptype) static inline char *
med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
{ {
if(swaptype <= 1) return CMP(thunk, a, b) < 0 ?
swapcode(long, a, b, n) (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
else :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
swapcode(char, a, b, n)
} }
#define swap(a, b) \ /*
if (swaptype == 0) { \ * The actual qsort() implementation is static to avoid preemptible calls when
long t = *(long *)(a); \ * recursing. Also give them different names for improved debugging.
*(long *)(a) = *(long *)(b); \ */
*(long *)(b) = t; \ static void
} else \ local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk)
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)
{ {
return cmp(a, b) < 0 ? char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
(cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) size_t d1, d2;
:(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); int cmp_result;
}
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;
int swap_cnt; 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; swap_cnt = 0;
if (n < 7) { if (n < 7) {
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; for (pl = pm;
pl -= es) pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
swap(pl, pl - es); pl -= es)
swapfunc(pl, pl - es, es);
return; return;
} }
pm = (char *)a + (n / 2) * es; pm = (char *)a + (n / 2) * es;
@@ -117,61 +102,92 @@ loop: SWAPINIT(a, es);
pl = a; pl = a;
pn = (char *)a + (n - 1) * es; pn = (char *)a + (n - 1) * es;
if (n > 40) { if (n > 40) {
d = (n / 8) * es; size_t d = (n / 8) * es;
pl = med3(pl, pl + d, pl + 2 * d, cmp);
pm = med3(pm - d, pm, pm + d, cmp); pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
pn = med3(pn - 2 * d, pn - d, pn, cmp); 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; pa = pb = (char *)a + es;
pc = pd = (char *)a + (n - 1) * es; pc = pd = (char *)a + (n - 1) * es;
for (;;) { for (;;) {
while (pb <= pc && (r = cmp(pb, a)) <= 0) { while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
if (r == 0) { if (cmp_result == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pa, pb); swapfunc(pa, pb, es);
pa += es; pa += es;
} }
pb += es; pb += es;
} }
while (pb <= pc && (r = cmp(pc, a)) >= 0) { while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
if (r == 0) { if (cmp_result == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pc, pd); swapfunc(pc, pd, es);
pd -= es; pd -= es;
} }
pc -= es; pc -= es;
} }
if (pb > pc) if (pb > pc)
break; break;
swap(pb, pc); swapfunc(pb, pc, es);
swap_cnt = 1; swap_cnt = 1;
pb += es; pb += es;
pc -= es; pc -= es;
} }
if (swap_cnt == 0) { /* Switch to insertion sort */ if (swap_cnt == 0) { /* Switch to insertion sort */
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; for (pl = pm;
pl -= es) pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
swap(pl, pl - es); pl -= es)
swapfunc(pl, pl - es, es);
return; return;
} }
pn = (char *)a + n * es; pn = (char *)a + n * es;
r = min(pa - (char *)a, pb - pa); d1 = MIN(pa - (char *)a, pb - pa);
vecswap(a, pb - r, r); vecswap(a, pb - d1, d1);
r = min((int)(pd - pc), (int)(pn - pd - es)); /*
vecswap(pb, pn - r, r); * Cast es to preserve signedness of right-hand side of MIN()
if ((r = pb - pa) > (int)es) * expression, to avoid sign ambiguity in the implied comparison. es
qsort(a, r / es, es, cmp); * is safely within [0, SSIZE_MAX].
if ((r = pd - pc) > (int)es) { */
/* Iterate rather than recurse to save stack space */ d1 = MIN(pd - pc, pn - pd - (ssize_t)es);
a = pn - r; vecswap(pb, pn - d1, d1);
n = r / es;
goto loop; 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);
} }
+48 -57
View File
@@ -1,4 +1,6 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
* *
@@ -13,11 +15,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -53,18 +51,19 @@ static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h>
#include <errno.h> #include <errno.h>
#include <errno_private.h>
typedef struct { typedef struct {
const u_char **sa; const u_char **sa;
int sn, si; int sn, si;
} stack; } stack;
static inline void simplesort(u_char const **, int, int, u_char const *, u_int); static inline void simplesort
static void r_sort_a(u_char const **, int, int, u_char const *, u_int); (const u_char **, int, int, const u_char *, u_int);
static void r_sort_b(u_char const **, u_char const **, int, int, u_char const *, 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 THRESHOLD 20 /* Divert to simplesort(). */
#define SIZE 512 /* Default stack size. */ #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]; \ endch = tab[endch]; \
tr = tab; \ tr = tab; \
if (endch != 0 && endch != 255) { \ if (endch != 0 && endch != 255) { \
/* __set_errno(EINVAL); */ \ errno = EINVAL; \
return (-1); \ return (-1); \
} \ } \
} \ } \
} }
int 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; const u_char *tr;
u_int c; int c;
u_char tr0[256]; u_char tr0[256];
SETUP; SETUP;
@@ -101,11 +100,10 @@ radixsort(u_char const **a, int n, u_char const *tab, u_int endch)
} }
int 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; const u_char *tr, **ta;
u_char const **ta; int c;
u_int c;
u_char tr0[256]; u_char tr0[256];
SETUP; SETUP;
@@ -127,25 +125,14 @@ sradixsort(u_char const **a, int n, u_char const *tab, u_int endch)
/* Unstable, in-place sort. */ /* Unstable, in-place sort. */
static void 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 count[256], nc, bmin;
static int nc; int c;
static u_int bmin; const u_char **ak, *r;
u_int c; stack s[SIZE], *sp, *sp0, *sp1, temp;
u_char const **ak; int *cp, bigc;
u_char const *r; const u_char **an, *t, **aj, **top[256];
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];
/* Set up stack. */ /* Set up stack. */
sp = s; 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. * Set top[]; push incompletely sorted bins onto stack.
* top[] = pointers to last out-of-place element in bins. * 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. */ /* Stable sort, requiring additional memory. */
static static void
void r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
r_sort_b(u_char const **a, u_char const **ta, int n, int i, u_char const *tr, u_int endch) u_int endch)
{ {
static int count[256]; static int count[256], nc, bmin;
static u_int nc; int c;
static u_int bmin; const u_char **ak, **ai;
u_int c;
u_char const **ak, **ai;
stack s[512], *sp, *sp0, *sp1, temp; stack s[512], *sp, *sp0, *sp1, temp;
u_char const **top[256]; const u_char **top[256];
int *cp; int *cp, bigc;
u_int bigc;
sp = s; sp = s;
push(a, n, i); 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 /* insertion sort */
void static inline void
simplesort(u_char const **a, int n, int b, u_char const *tr, u_int endch) /* insertion sort */ simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
{ {
u_char ch; u_char ch;
u_char const **ak; const u_char **ak, **ai, *s, *t;
u_char const **ai;
u_char const *s;
u_char const *t;
for (ak = a+1; --n >= 1; ak++) { for (ak = a+1; --n >= 1; ak++)
for (ai = ak; ai > a; ai--) { for (ai = ak; ai > a; ai--) {
for (s = ai[0] + b, t = ai[-1] + b; 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]) if (ch != tr[*t])
break; break;
if (ch >= tr[*t]) if (ch >= tr[*t])
break; break;
swap(ai[0], ai[-1], s); swap(ai[0], ai[-1], s);
} }
}
} }
+24 -13
View File
@@ -1,7 +1,14 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@@ -10,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -37,9 +40,6 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno_private.h>
/* /*
* Convert a string to a long integer. * Convert a string to a long integer.
* *
@@ -74,11 +74,21 @@ strtol(const char * __restrict nptr, char ** __restrict endptr, int base)
c = *s++; c = *s++;
} }
if ((base == 0 || base == 16) && 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]; c = s[1];
s += 2; s += 2;
base = 16; 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) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
acc = any = 0; acc = any = 0;
@@ -103,7 +113,7 @@ strtol(const char * __restrict nptr, char ** __restrict endptr, int base)
* overflow. * overflow.
*/ */
cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX
: LONG_MAX; : LONG_MAX;
cutlim = cutoff % base; cutlim = cutoff % base;
cutoff /= base; cutoff /= base;
for ( ; ; c = *s++) { for ( ; ; c = *s++) {
@@ -127,10 +137,10 @@ strtol(const char * __restrict nptr, char ** __restrict endptr, int base)
} }
if (any < 0) { if (any < 0) {
acc = neg ? LONG_MIN : LONG_MAX; acc = neg ? LONG_MIN : LONG_MAX;
__set_errno(ERANGE); errno = ERANGE;
} else if (!any) { } else if (!any) {
noconv: noconv:
__set_errno(EINVAL); errno = EINVAL;
} else if (neg) } else if (neg)
acc = -acc; acc = -acc;
if (endptr != NULL) if (endptr != NULL)
@@ -139,6 +149,7 @@ noconv:
} }
#ifdef __HAIKU__
long __strtol_internal(const char *number, char **_end, int base, int group); long __strtol_internal(const char *number, char **_end, int base, int group);
long long
@@ -149,4 +160,4 @@ __strtol_internal(const char *number, char **_end, int base, int group)
return strtol(number, _end, base); return strtol(number, _end, base);
} }
#endif
+30 -20
View File
@@ -1,7 +1,14 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@@ -10,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -37,16 +40,12 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno_private.h>
/* /*
* Convert a string to a long long integer. * Convert a string to a long long integer.
* *
* Assumes that the upper and lower case * Assumes that the upper and lower case
* alphabets and digits are each contiguous. * alphabets and digits are each contiguous.
*/ */
long long long long
strtoll(const char * __restrict nptr, char ** __restrict endptr, int base) 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. * Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else * If base is 0, allow 0b for binary, 0x for hex, and 0 for
* assume decimal; if base is already 16, allow 0x. * octal, else assume decimal; if base is already 2, allow
* 0b; if base is already 16, allow 0x.
*/ */
s = nptr; s = nptr;
do { do {
@@ -74,11 +74,21 @@ strtoll(const char * __restrict nptr, char ** __restrict endptr, int base)
c = *s++; c = *s++;
} }
if ((base == 0 || base == 16) && 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]; c = s[1];
s += 2; s += 2;
base = 16; 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) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
acc = any = 0; 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 * Set 'any' if any `digits' consumed; make it negative to indicate
* overflow. * overflow.
*/ */
cutoff = neg ? (unsigned long long)-(LONGLONG_MIN + LONGLONG_MAX) + LONGLONG_MAX cutoff = neg ? (unsigned long long)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX
: LONGLONG_MAX; : LLONG_MAX;
cutlim = cutoff % base; cutlim = cutoff % base;
cutoff /= base; cutoff /= base;
for ( ; ; c = *s++) { for ( ; ; c = *s++) {
@@ -127,20 +137,20 @@ strtoll(const char * __restrict nptr, char ** __restrict endptr, int base)
} }
} }
if (any < 0) { if (any < 0) {
acc = neg ? LONGLONG_MIN : LONGLONG_MAX; acc = neg ? LLONG_MIN : LLONG_MAX;
__set_errno(ERANGE); errno = ERANGE;
} else if (!any) { } else if (!any) {
noconv: noconv:
__set_errno(EINVAL); errno = EINVAL;
} else if (neg) } else if (neg)
acc = -acc; acc = -acc;
if (endptr != NULL) if (endptr != NULL)
*endptr = (char *)(any ? s - 1 : nptr); *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 __strtoll_internal(const char *number, char **_end, int base, int group);
long long long long
@@ -151,4 +161,4 @@ __strtoll_internal(const char *number, char **_end, int base, int group)
return strtoll(number, _end, base); return strtoll(number, _end, base);
} }
#endif
-153
View File
@@ -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 <sys/types.h>
#include <ctype.h>
#include <errno.h>
//#include <limits.h>
#include <stdlib.h>
#include <errno_private.h>
#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);
}
+24 -13
View File
@@ -1,7 +1,14 @@
/* /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@@ -10,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -37,9 +40,6 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno_private.h>
/* /*
* Convert a string to an unsigned long integer. * Convert a string to an unsigned long integer.
* *
@@ -72,11 +72,21 @@ strtoul(const char * __restrict nptr, char ** __restrict endptr, int base)
c = *s++; c = *s++;
} }
if ((base == 0 || base == 16) && 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]; c = s[1];
s += 2; s += 2;
base = 16; 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) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
acc = any = 0; acc = any = 0;
@@ -106,10 +116,10 @@ strtoul(const char * __restrict nptr, char ** __restrict endptr, int base)
} }
if (any < 0) { if (any < 0) {
acc = ULONG_MAX; acc = ULONG_MAX;
__set_errno(ERANGE); errno = ERANGE;
} else if (!any) { } else if (!any) {
noconv: noconv:
__set_errno(EINVAL); errno = EINVAL;
} else if (neg) } else if (neg)
acc = -acc; acc = -acc;
if (endptr != NULL) 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 __strtoul_internal(const char *number, char **_end, int base, int group);
unsigned long unsigned long
@@ -128,4 +139,4 @@ __strtoul_internal(const char *number, char **_end, int base, int group)
return strtoul(number, _end, base); return strtoul(number, _end, base);
} }
#endif
+27 -18
View File
@@ -1,7 +1,14 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@@ -10,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -37,16 +40,12 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno_private.h>
/* /*
* Convert a string to an unsigned long long integer. * Convert a string to an unsigned long long integer.
* *
* Assumes that the upper and lower case * Assumes that the upper and lower case
* alphabets and digits are each contiguous. * alphabets and digits are each contiguous.
*/ */
unsigned long long unsigned long long
strtoull(const char * __restrict nptr, char ** __restrict endptr, int base) 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++; c = *s++;
} }
if ((base == 0 || base == 16) && 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]; c = s[1];
s += 2; s += 2;
base = 16; 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) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
acc = any = 0; acc = any = 0;
if (base < 2 || base > 36) if (base < 2 || base > 36)
goto noconv; goto noconv;
cutoff = ULONGLONG_MAX / base; cutoff = ULLONG_MAX / base;
cutlim = ULONGLONG_MAX % base; cutlim = ULLONG_MAX % base;
for ( ; ; c = *s++) { for ( ; ; c = *s++) {
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
c -= '0'; c -= '0';
@@ -105,20 +114,20 @@ strtoull(const char * __restrict nptr, char ** __restrict endptr, int base)
} }
} }
if (any < 0) { if (any < 0) {
acc = ULONGLONG_MAX; acc = ULLONG_MAX;
__set_errno(ERANGE); errno = ERANGE;
} else if (!any) { } else if (!any) {
noconv: noconv:
__set_errno(EINVAL); errno = EINVAL;
} else if (neg) } else if (neg)
acc = -acc; acc = -acc;
if (endptr != NULL) if (endptr != NULL)
*endptr = (char *)(any ? s - 1 : nptr); *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 __strtoull_internal(const char *number, char **_end, int base, int group);
unsigned long long unsigned long long
@@ -129,4 +138,4 @@ __strtoull_internal(const char *number, char **_end, int base, int group)
return strtoull(number, _end, base); return strtoull(number, _end, base);
} }
#endif
-114
View File
@@ -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 <sys/types.h>
#include <ctype.h>
#include <errno.h>
//#include <limits.h>
#include <stdlib.h>
#include <errno_private.h>
#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);
}
+20 -18
View File
@@ -1,7 +1,14 @@
/* /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1987, 1993 * Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@@ -10,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -31,7 +34,6 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <ctype.h> #include <ctype.h>
#include <strings.h> #include <strings.h>
@@ -72,15 +74,14 @@ strncasecmp(const char *s1, const char *s2, size_t n)
int int
strcasecmp_l(const char *s1, const char *s2, locale_t locale) strcasecmp_l(const char *s1, const char *s2, locale_t locale)
{ {
const u_char *us1 = (const u_char *)s1; const u_char
const u_char *us2 = (const u_char *)s2; *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') if (*us1++ == '\0')
return 0; return (0);
} return (tolower_l(*us1, locale) - tolower_l(*--us2, locale));
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) strncasecmp_l(const char *s1, const char *s2, size_t n, locale_t locale)
{ {
if (n != 0) { if (n != 0) {
const u_char *us1 = (const u_char *)s1; const u_char
const u_char *us2 = (const u_char *)s2; *us1 = (const u_char *)s1,
*us2 = (const u_char *)s2;
do { do {
if (tolower_l(*us1, locale) != tolower_l(*us2++, locale)) 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') if (*us1++ == '\0')
break; break;
} while (--n != 0); } while (--n != 0);
} }
return 0; return (0);
} }
+10 -8
View File
@@ -1,10 +1,17 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
* *
* This code is derived from software contributed to Berkeley by * This code is derived from software contributed to Berkeley by
* Chris Torek. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@@ -13,11 +20,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. Neither the name of the University nor the names of its contributors
* 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 * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@@ -55,11 +58,10 @@ strcasestr(const char *s, const char *find)
do { do {
do { do {
if ((sc = *s++) == 0) if ((sc = *s++) == 0)
return NULL; return (NULL);
} while ((char)tolower((unsigned char)sc) != c); } while ((char)tolower((unsigned char)sc) != c);
} while (strncasecmp(s, find, len) != 0); } while (strncasecmp(s, find, len) != 0);
s--; s--;
} }
return (char *)s; return ((char *)s);
} }