diff options
author | Travis Geiselbrecht <geist@foobox.com> | 2015-11-17 14:22:03 -0800 |
---|---|---|
committer | Travis Geiselbrecht <geist@foobox.com> | 2015-11-17 14:22:03 -0800 |
commit | a2f02251e5785ec6693f995198dd711840ab2de0 (patch) | |
tree | 0bcf5416128b1cb1dde12cda4d0fbda32ad0497c /lib/libc | |
parent | 527c0dbc54227f36207173d97050d1fcae61692e (diff) | |
download | common-a2f02251e5785ec6693f995198dd711840ab2de0.tar.gz |
[lib][libc] mass reformat with space indents
Ran files through ./script/codestyle.space
Diffstat (limited to 'lib/libc')
47 files changed, 1436 insertions, 1427 deletions
diff --git a/lib/libc/atexit.c b/lib/libc/atexit.c index 85d7bdf5..9be083f6 100644 --- a/lib/libc/atexit.c +++ b/lib/libc/atexit.c @@ -24,6 +24,6 @@ /* nulled out atexit. static object constructors call this */ int atexit(void (*func)(void)) { - return 0; + return 0; } diff --git a/lib/libc/atoi.c b/lib/libc/atoi.c index 71dfd0f6..d641e00d 100644 --- a/lib/libc/atoi.c +++ b/lib/libc/atoi.c @@ -33,158 +33,159 @@ static int hexval(char c) { - if (c >= '0' && c <= '9') - return c - '0'; - else if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - else if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - - return 0; + if (c >= '0' && c <= '9') + return c - '0'; + else if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + + return 0; } int atoi(const char *num) { #if !LONG_IS_INT - // XXX fail + // XXX fail #else - return atol(num); + return atol(num); #endif } unsigned int atoui(const char *num) { #if !LONG_IS_INT - // XXX fail + // XXX fail #else - return atoul(num); + return atoul(num); #endif } long atol(const char *num) { - long value = 0; - int neg = 0; - - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - if (num[0] == '-') { - neg = 1; - num++; - } - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - - if (neg) - value = -value; - - return value; + long value = 0; + int neg = 0; + + if (num[0] == '0' && num[1] == 'x') { + // hex + num += 2; + while (*num && isxdigit(*num)) + value = value * 16 + hexval(*num++); + } else { + // decimal + if (num[0] == '-') { + neg = 1; + num++; + } + while (*num && isdigit(*num)) + value = value * 10 + *num++ - '0'; + } + + if (neg) + value = -value; + + return value; } unsigned long atoul(const char *num) { - unsigned long value = 0; - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - - return value; + unsigned long value = 0; + if (num[0] == '0' && num[1] == 'x') { + // hex + num += 2; + while (*num && isxdigit(*num)) + value = value * 16 + hexval(*num++); + } else { + // decimal + while (*num && isdigit(*num)) + value = value * 10 + *num++ - '0'; + } + + return value; } unsigned long long atoull(const char *num) { - unsigned long long value = 0; - if (num[0] == '0' && num[1] == 'x') { - // hex - num += 2; - while (*num && isxdigit(*num)) - value = value * 16 + hexval(*num++); - } else { - // decimal - while (*num && isdigit(*num)) - value = value * 10 + *num++ - '0'; - } - - return value; + unsigned long long value = 0; + if (num[0] == '0' && num[1] == 'x') { + // hex + num += 2; + while (*num && isxdigit(*num)) + value = value * 16 + hexval(*num++); + } else { + // decimal + while (*num && isdigit(*num)) + value = value * 10 + *num++ - '0'; + } + + return value; } -unsigned long strtoul(const char *nptr, char **endptr, int base) { - int neg = 0; - unsigned long ret = 0; - - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - return 0; - } - - while (isspace(*nptr)) { - nptr++; - } - - if (*nptr == '+') { - nptr++; - } else if (*nptr == '-') { - neg = 1; - nptr++; - } - - if ((base == 0 || base == 16) && nptr[0] == '0' && nptr[1] == 'x') { - base = 16; - nptr += 2; - } else if (base == 0 && nptr[0] == '0') { - base = 8; - nptr++; - } else if (base == 0) { - base = 10; - } - - for (;;) { - char c = *nptr; - int v = -1; - unsigned long new_ret; - - if (c >= 'A' && c <= 'Z') { - v = c - 'A' + 10; - } else if (c >= 'a' && c <= 'z') { - v = c - 'a' + 10; - } else if (c >= '0' && c <= '9') { - v = c - '0'; - } - - if (v < 0 || v >= base) { - *endptr = (char *) nptr; - break; - } - - new_ret = ret * base; - if (new_ret / base != ret || - new_ret + v < new_ret || - ret == ULONG_MAX) { - ret = ULONG_MAX; - errno = ERANGE; - } else { - ret = new_ret + v; - } - - nptr++; - } - - if (neg && ret != ULONG_MAX) { - ret = -ret; - } - - return ret; +unsigned long strtoul(const char *nptr, char **endptr, int base) +{ + int neg = 0; + unsigned long ret = 0; + + if (base < 0 || base == 1 || base > 36) { + errno = EINVAL; + return 0; + } + + while (isspace(*nptr)) { + nptr++; + } + + if (*nptr == '+') { + nptr++; + } else if (*nptr == '-') { + neg = 1; + nptr++; + } + + if ((base == 0 || base == 16) && nptr[0] == '0' && nptr[1] == 'x') { + base = 16; + nptr += 2; + } else if (base == 0 && nptr[0] == '0') { + base = 8; + nptr++; + } else if (base == 0) { + base = 10; + } + + for (;;) { + char c = *nptr; + int v = -1; + unsigned long new_ret; + + if (c >= 'A' && c <= 'Z') { + v = c - 'A' + 10; + } else if (c >= 'a' && c <= 'z') { + v = c - 'a' + 10; + } else if (c >= '0' && c <= '9') { + v = c - '0'; + } + + if (v < 0 || v >= base) { + *endptr = (char *) nptr; + break; + } + + new_ret = ret * base; + if (new_ret / base != ret || + new_ret + v < new_ret || + ret == ULONG_MAX) { + ret = ULONG_MAX; + errno = ERANGE; + } else { + ret = new_ret + v; + } + + nptr++; + } + + if (neg && ret != ULONG_MAX) { + ret = -ret; + } + + return ret; } diff --git a/lib/libc/bsearch.c b/lib/libc/bsearch.c index 53aafb87..a50c576e 100644 --- a/lib/libc/bsearch.c +++ b/lib/libc/bsearch.c @@ -15,30 +15,31 @@ #include <stdlib.h> void *bsearch(const void *key, const void *base, size_t num_elems, size_t size, - int (*compare)(const void *, const void *)) { - size_t low = 0, high = num_elems - 1; + int (*compare)(const void *, const void *)) +{ + size_t low = 0, high = num_elems - 1; - if (num_elems == 0) { - return NULL; - } + if (num_elems == 0) { + return NULL; + } - for (;;) { - size_t mid = low + ((high - low) / 2); - const void *mid_elem = ((unsigned char*) base) + mid*size; - int r = compare(key, mid_elem); + for (;;) { + size_t mid = low + ((high - low) / 2); + const void *mid_elem = ((unsigned char*) base) + mid*size; + int r = compare(key, mid_elem); - if (r < 0) { - if (mid == 0) { - return NULL; - } - high = mid - 1; - } else if (r > 0) { - low = mid + 1; - if (low < mid || low > high) { - return NULL; - } - } else { - return (void*) mid_elem; - } - } + if (r < 0) { + if (mid == 0) { + return NULL; + } + high = mid - 1; + } else if (r > 0) { + low = mid + 1; + if (low < mid || low > high) { + return NULL; + } + } else { + return (void*) mid_elem; + } + } } diff --git a/lib/libc/ctype.c b/lib/libc/ctype.c index 37b8cf89..e5600bc7 100644 --- a/lib/libc/ctype.c +++ b/lib/libc/ctype.c @@ -24,75 +24,75 @@ int isblank(int c) { - return (c == ' ' || c == '\t'); + return (c == ' ' || c == '\t'); } int isspace(int c) { - return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'); + return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'); } int islower(int c) { - return ((c >= 'a') && (c <= 'z')); + return ((c >= 'a') && (c <= 'z')); } int isupper(int c) { - return ((c >= 'A') && (c <= 'Z')); + return ((c >= 'A') && (c <= 'Z')); } int isdigit(int c) { - return ((c >= '0') && (c <= '9')); + return ((c >= '0') && (c <= '9')); } int isalpha(int c) { - return isupper(c) || islower(c); + return isupper(c) || islower(c); } int isalnum(int c) { - return isalpha(c) || isdigit(c); + return isalpha(c) || isdigit(c); } int isxdigit(int c) { - return isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F')); + return isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F')); } int isgraph(int c) { - return ((c > ' ') && (c < 0x7f)); + return ((c > ' ') && (c < 0x7f)); } int iscntrl(int c) { - return ((c < ' ') || (c == 0x7f)); + return ((c < ' ') || (c == 0x7f)); } int isprint(int c) { - return ((c >= 0x20) && (c < 0x7f)); + return ((c >= 0x20) && (c < 0x7f)); } int ispunct(int c) { - return isgraph(c) && (!isalnum(c)); + return isgraph(c) && (!isalnum(c)); } int tolower(int c) { - if ((c >= 'A') && (c <= 'Z')) - return c + ('a' - 'A'); - return c; + if ((c >= 'A') && (c <= 'Z')) + return c + ('a' - 'A'); + return c; } int toupper(int c) { - if ((c >= 'a') && (c <= 'z')) - return c + ('A' - 'a'); - return c; + if ((c >= 'a') && (c <= 'z')) + return c + ('A' - 'a'); + return c; } diff --git a/lib/libc/eabi.c b/lib/libc/eabi.c index f35f2983..b0b3617e 100644 --- a/lib/libc/eabi.c +++ b/lib/libc/eabi.c @@ -29,17 +29,17 @@ * so we stub them out here. */ _Unwind_Reason_Code __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp, _Unwind_Context *context) { - return _URC_FAILURE; + return _URC_FAILURE; } _Unwind_Reason_Code __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp, _Unwind_Context *context) { - return _URC_FAILURE; + return _URC_FAILURE; } _Unwind_Reason_Code __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp, _Unwind_Context *context) { - return _URC_FAILURE; + return _URC_FAILURE; } #endif diff --git a/lib/libc/printf.c b/lib/libc/printf.c index b81886f0..14e7cd1e 100644 --- a/lib/libc/printf.c +++ b/lib/libc/printf.c @@ -38,26 +38,26 @@ int sprintf(char *str, const char *fmt, ...) { - int err; + int err; - va_list ap; - va_start(ap, fmt); - err = vsprintf(str, fmt, ap); - va_end(ap); + va_list ap; + va_start(ap, fmt); + err = vsprintf(str, fmt, ap); + va_end(ap); - return err; + return err; } int snprintf(char *str, size_t len, const char *fmt, ...) { - int err; + int err; - va_list ap; - va_start(ap, fmt); - err = vsnprintf(str, len, fmt, ap); - va_end(ap); + va_list ap; + va_start(ap, fmt); + err = vsnprintf(str, len, fmt, ap); + va_end(ap); - return err; + return err; } @@ -78,36 +78,36 @@ int snprintf(char *str, size_t len, const char *fmt, ...) __NO_INLINE static char *longlong_to_string(char *buf, unsigned long long n, size_t len, uint flag, char *signchar) { - size_t pos = len; - int negative = 0; + size_t pos = len; + int negative = 0; - if ((flag & SIGNEDFLAG) && (long long)n < 0) { - negative = 1; - n = -n; - } + if ((flag & SIGNEDFLAG) && (long long)n < 0) { + negative = 1; + n = -n; + } - buf[--pos] = 0; + buf[--pos] = 0; - /* only do the math if the number is >= 10 */ - while (n >= 10) { - int digit = n % 10; + /* only do the math if the number is >= 10 */ + while (n >= 10) { + int digit = n % 10; - n /= 10; + n /= 10; - buf[--pos] = digit + '0'; - } - buf[--pos] = n + '0'; + buf[--pos] = digit + '0'; + } + buf[--pos] = n + '0'; - if (negative) - *signchar = '-'; - else if ((flag & SHOWSIGNFLAG)) - *signchar = '+'; - else if ((flag & BLANKPOSFLAG)) - *signchar = ' '; - else - *signchar = '\0'; + if (negative) + *signchar = '-'; + else if ((flag & SHOWSIGNFLAG)) + *signchar = '+'; + else if ((flag & BLANKPOSFLAG)) + *signchar = ' '; + else + *signchar = '\0'; - return &buf[pos]; + return &buf[pos]; } static const char hextable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; @@ -115,24 +115,24 @@ static const char hextable_caps[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8 __NO_INLINE static char *longlong_to_hexstring(char *buf, unsigned long long u, size_t len, uint flag) { - size_t pos = len; - const char *table = (flag & CAPSFLAG) ? hextable_caps : hextable; + size_t pos = len; + const char *table = (flag & CAPSFLAG) ? hextable_caps : hextable; - buf[--pos] = 0; - do { - unsigned int digit = u % 16; - u /= 16; + buf[--pos] = 0; + do { + unsigned int digit = u % 16; + u /= 16; - buf[--pos] = table[digit]; - } while (u != 0); + buf[--pos] = table[digit]; + } while (u != 0); - return &buf[pos]; + return &buf[pos]; } #if FLOAT_PRINTF union double_int { - double d; - uint64_t i; + double d; + uint64_t i; }; #define OUT(c) buf[pos++] = (c) @@ -141,209 +141,217 @@ union double_int { /* print up to a 4 digit exponent as string, with sign */ __NO_INLINE static size_t exponent_to_string(char *buf, int32_t exponent) { - size_t pos = 0; - - /* handle sign */ - if (exponent < 0) { - OUT('-'); - exponent = -exponent; - } else { - OUT('+'); - } - - /* see how far we need to bump into the string to print from the right */ - if (exponent >= 1000) pos += 4; - else if (exponent >= 100) pos += 3; - else if (exponent >= 10) pos += 2; - else pos++; - - /* print decimal string, from the right */ - uint i = pos; - do { - uint digit = (uint32_t)exponent % 10; - - buf[--i] = digit + '0'; - - exponent /= 10; - } while (exponent != 0); - - /* return number of characters printed */ - return pos; + size_t pos = 0; + + /* handle sign */ + if (exponent < 0) { + OUT('-'); + exponent = -exponent; + } else { + OUT('+'); + } + + /* see how far we need to bump into the string to print from the right */ + if (exponent >= 1000) pos += 4; + else if (exponent >= 100) pos += 3; + else if (exponent >= 10) pos += 2; + else pos++; + + /* print decimal string, from the right */ + uint i = pos; + do { + uint digit = (uint32_t)exponent % 10; + + buf[--i] = digit + '0'; + + exponent /= 10; + } while (exponent != 0); + + /* return number of characters printed */ + return pos; } __NO_INLINE static char *double_to_string(char *buf, size_t len, double d, uint flag) { - size_t pos = 0; - union double_int u = { d }; - - uint32_t exponent = (u.i >> 52) & 0x7ff; - uint64_t fraction = (u.i & ((1ULL << 52) - 1)); - bool neg = !!(u.i & (1ULL << 63)); - - /* start constructing the string */ - if (neg) { - OUT('-'); - d = -d; - } - - /* longest: - * 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000o - */ - - /* look for special cases */ - if (exponent == 0x7ff) { - if (fraction == 0) { - /* infinity */ - if (flag & CAPSFLAG) OUTSTR("INF"); else OUTSTR("inf"); - } else { - /* NaN */ - if (flag & CAPSFLAG) OUTSTR("NAN"); else OUTSTR("nan"); - } - } else if (exponent == 0) { - if (fraction == 0) { - /* zero */ - OUTSTR("0.000000"); - } else { - /* denormalized */ - /* XXX does not handle */ - if (flag & CAPSFLAG) OUTSTR("DEN"); else OUTSTR("den"); - } - } else { - /* see if it's in the range of floats we can easily print */ - int exponent_signed = exponent - 1023; - if (exponent_signed < -52 || exponent_signed > 52) { - OUTSTR("<range>"); - } else { - /* start by walking backwards through the string */ + size_t pos = 0; + union double_int u = { d }; + + uint32_t exponent = (u.i >> 52) & 0x7ff; + uint64_t fraction = (u.i & ((1ULL << 52) - 1)); + bool neg = !!(u.i & (1ULL << 63)); + + /* start constructing the string */ + if (neg) { + OUT('-'); + d = -d; + } + + /* longest: + * 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000o + */ + + /* look for special cases */ + if (exponent == 0x7ff) { + if (fraction == 0) { + /* infinity */ + if (flag & CAPSFLAG) OUTSTR("INF"); + else OUTSTR("inf"); + } else { + /* NaN */ + if (flag & CAPSFLAG) OUTSTR("NAN"); + else OUTSTR("nan"); + } + } else if (exponent == 0) { + if (fraction == 0) { + /* zero */ + OUTSTR("0.000000"); + } else { + /* denormalized */ + /* XXX does not handle */ + if (flag & CAPSFLAG) OUTSTR("DEN"); + else OUTSTR("den"); + } + } else { + /* see if it's in the range of floats we can easily print */ + int exponent_signed = exponent - 1023; + if (exponent_signed < -52 || exponent_signed > 52) { + OUTSTR("<range>"); + } else { + /* start by walking backwards through the string */ #define OUTREV(c) do { if (&buf[pos] == buf) goto done; else buf[--pos] = (c); } while (0) - pos = len; - OUTREV(0); + pos = len; + OUTREV(0); - /* reserve space for the fractional component first */ - for (int i = 0; i <= 6; i++) - OUTREV('0'); - size_t decimal_spot = pos; + /* reserve space for the fractional component first */ + for (int i = 0; i <= 6; i++) + OUTREV('0'); + size_t decimal_spot = pos; - /* print the integer portion */ - uint64_t u; - if (exponent_signed >= 0) { - u = fraction; - u |= (1ULL<<52); - u >>= (52 - exponent_signed); + /* print the integer portion */ + uint64_t u; + if (exponent_signed >= 0) { + u = fraction; + u |= (1ULL<<52); + u >>= (52 - exponent_signed); - char *s = longlong_to_string(buf, u, pos + 1, flag, &(char){0}); + char *s = longlong_to_string(buf, u, pos + 1, flag, &(char) {0}); - pos = s - buf; - } else { - /* exponent is negative */ - u = 0; - OUTREV('0'); - } + pos = s - buf; + } else { + /* exponent is negative */ + u = 0; + OUTREV('0'); + } - buf[decimal_spot] = '.'; + buf[decimal_spot] = '.'; - /* handle the fractional part */ - uint32_t frac = ((d - u) * 1000000) + .5; + /* handle the fractional part */ + uint32_t frac = ((d - u) * 1000000) + .5; - uint i = decimal_spot + 6 + 1; - while (frac != 0) { - uint digit = frac % 10; + uint i = decimal_spot + 6 + 1; + while (frac != 0) { + uint digit = frac % 10; - buf[--i] = digit + '0'; + buf[--i] = digit + '0'; - frac /= 10; - } + frac /= 10; + } - if (neg) - OUTREV('-'); + if (neg) + OUTREV('-'); done: - /* separate return path, since we've been walking backwards through the string */ - return &buf[pos]; - } + /* separate return path, since we've been walking backwards through the string */ + return &buf[pos]; + } #undef OUTREV - } + } - buf[pos] = 0; - return buf; + buf[pos] = 0; + return buf; } __NO_INLINE static char *double_to_hexstring(char *buf, size_t len, double d, uint flag) { - size_t pos = 0; - union double_int u = { d }; - - uint32_t exponent = (u.i >> 52) & 0x7ff; - uint64_t fraction = (u.i & ((1ULL << 52) - 1)); - bool neg = !!(u.i & (1ULL << 63)); - - /* start constructing the string */ - if (neg) { - OUT('-'); - } - - /* look for special cases */ - if (exponent == 0x7ff) { - if (fraction == 0) { - /* infinity */ - if (flag & CAPSFLAG) OUTSTR("INF"); else OUTSTR("inf"); - } else { - /* NaN */ - if (flag & CAPSFLAG) OUTSTR("NAN"); else OUTSTR("nan"); - } - } else if (exponent == 0) { - if (fraction == 0) { - /* zero */ - if (flag & CAPSFLAG) OUTSTR("0X0P+0"); else OUTSTR("0x0p+0"); - } else { - /* denormalized */ - /* XXX does not handle */ - if (flag & CAPSFLAG) OUTSTR("DEN"); else OUTSTR("den"); - } - } else { - /* regular normalized numbers: - * 0x1p+1 - * 0x1.0000000000001p+1 - * 0X1.FFFFFFFFFFFFFP+1023 - * 0x1.FFFFFFFFFFFFFP+1023 - */ - int exponent_signed = exponent - 1023; - - /* implicit 1. */ - if (flag & CAPSFLAG) OUTSTR("0X1"); else OUTSTR("0x1"); - - /* select the appropriate hex case table */ - const char *table = (flag & CAPSFLAG) ? hextable_caps : hextable; - - int zero_count = 0; - bool output_dot = false; - for (int i = 52 - 4; i >= 0; i -= 4) { - uint digit = (fraction >> i) & 0xf; - - if (digit == 0) { - zero_count++; - } else { - /* output a . the first time we output a char */ - if (!output_dot) { - OUT('.'); - output_dot = true; - } - /* if we have a non zero digit, see if we need to output a string of zeros */ - while (zero_count > 0) { - OUT('0'); - zero_count--; - } - buf[pos++] = table[digit]; - } - } - - /* handle the exponent */ - buf[pos++] = (flag & CAPSFLAG) ? 'P' : 'p'; - pos += exponent_to_string(&buf[pos], exponent_signed); - } - - buf[pos] = 0; - return buf; + size_t pos = 0; + union double_int u = { d }; + + uint32_t exponent = (u.i >> 52) & 0x7ff; + uint64_t fraction = (u.i & ((1ULL << 52) - 1)); + bool neg = !!(u.i & (1ULL << 63)); + + /* start constructing the string */ + if (neg) { + OUT('-'); + } + + /* look for special cases */ + if (exponent == 0x7ff) { + if (fraction == 0) { + /* infinity */ + if (flag & CAPSFLAG) OUTSTR("INF"); + else OUTSTR("inf"); + } else { + /* NaN */ + if (flag & CAPSFLAG) OUTSTR("NAN"); + else OUTSTR("nan"); + } + } else if (exponent == 0) { + if (fraction == 0) { + /* zero */ + if (flag & CAPSFLAG) OUTSTR("0X0P+0"); + else OUTSTR("0x0p+0"); + } else { + /* denormalized */ + /* XXX does not handle */ + if (flag & CAPSFLAG) OUTSTR("DEN"); + else OUTSTR("den"); + } + } else { + /* regular normalized numbers: + * 0x1p+1 + * 0x1.0000000000001p+1 + * 0X1.FFFFFFFFFFFFFP+1023 + * 0x1.FFFFFFFFFFFFFP+1023 + */ + int exponent_signed = exponent - 1023; + + /* implicit 1. */ + if (flag & CAPSFLAG) OUTSTR("0X1"); + else OUTSTR("0x1"); + + /* select the appropriate hex case table */ + const char *table = (flag & CAPSFLAG) ? hextable_caps : hextable; + + int zero_count = 0; + bool output_dot = false; + for (int i = 52 - 4; i >= 0; i -= 4) { + uint digit = (fraction >> i) & 0xf; + + if (digit == 0) { + zero_count++; + } else { + /* output a . the first time we output a char */ + if (!output_dot) { + OUT('.'); + output_dot = true; + } + /* if we have a non zero digit, see if we need to output a string of zeros */ + while (zero_count > 0) { + OUT('0'); + zero_count--; + } + buf[pos++] = table[digit]; + } + } + + /* handle the exponent */ + buf[pos++] = (flag & CAPSFLAG) ? 'P' : 'p'; + pos += exponent_to_string(&buf[pos], exponent_signed); + } + + buf[pos] = 0; + return buf; } #undef OUT @@ -353,280 +361,278 @@ __NO_INLINE static char *double_to_hexstring(char *buf, size_t len, double d, ui int vsprintf(char *str, const char *fmt, va_list ap) { - return vsnprintf(str, INT_MAX, fmt, ap); + return vsnprintf(str, INT_MAX, fmt, ap); } struct _output_args { - char *outstr; - size_t len; - size_t pos; + char *outstr; + size_t len; + size_t pos; }; static int _vsnprintf_output(const char *str, size_t len, void *state) { - struct _output_args *args = state; + struct _output_args *args = state; - size_t count = 0; - while (count < len) { - if (args->pos < args->len) { - args->outstr[args->pos++] = *str; - } + size_t count = 0; + while (count < len) { + if (args->pos < args->len) { + args->outstr[args->pos++] = *str; + } - str++; - count++; - } + str++; + count++; + } - return count; + return count; } int vsnprintf(char *str, size_t len, const char *fmt, va_list ap) { - struct _output_args args; - int wlen; - - args.outstr = str; - args.len = len; - args.pos = 0; - - wlen = _printf_engine(&_vsnprintf_output, (void *)&args, fmt, ap); - if (args.pos >= len) - str[len-1] = '\0'; - else - str[wlen] = '\0'; - return wlen; + struct _output_args args; + int wlen; + + args.outstr = str; + args.len = len; + args.pos = 0; + + wlen = _printf_engine(&_vsnprintf_output, (void *)&args, fmt, ap); + if (args.pos >= len) + str[len-1] = '\0'; + else + str[wlen] = '\0'; + return wlen; } int _printf_engine(_printf_engine_output_func out, void *state, const char *fmt, va_list ap) { - int err = 0; - char c; - unsigned char uc; - const char *s; - size_t string_len; - unsigned long long n; - void *ptr; - int flags; - unsigned int format_num; - char signchar; - size_t chars_written = 0; - char num_buffer[32]; + int err = 0; + char c; + unsigned char uc; + const char *s; + size_t string_len; + unsigned long long n; + void *ptr; + int flags; + unsigned int format_num; + char signchar; + size_t chars_written = 0; + char num_buffer[32]; #define OUTPUT_STRING(str, len) do { err = out(str, len, state); if (err < 0) { goto exit; } else { chars_written += err; } } while(0) #define OUTPUT_CHAR(c) do { char __temp[1] = { c }; OUTPUT_STRING(__temp, 1); } while (0) - for (;;) { - /* reset the format state */ - flags = 0; - format_num = 0; - signchar = '\0'; + for (;;) { + /* reset the format state */ + flags = 0; + format_num = 0; + signchar = '\0'; - /* handle regular chars that aren't format related */ - s = fmt; - string_len = 0; - while ((c = *fmt++) != 0) { - if (c == '%') - break; /* we saw a '%', break and start parsing format */ - string_len++; - } + /* handle regular chars that aren't format related */ + s = fmt; + string_len = 0; + while ((c = *fmt++) != 0) { + if (c == '%') + break; /* we saw a '%', break and start parsing format */ + string_len++; + } - /* output the string we've accumulated */ - OUTPUT_STRING(s, string_len); + /* output the string we've accumulated */ + OUTPUT_STRING(s, string_len); - /* make sure we haven't just hit the end of the string */ - if (c == 0) - break; + /* make sure we haven't just hit the end of the string */ + if (c == 0) + break; next_format: - /* grab the next format character */ - c = *fmt++; - if (c == 0) - break; - - switch (c) { - case '0'...'9': - if (c == '0' && format_num == 0) - flags |= LEADZEROFLAG; - format_num *= 10; - format_num += c - '0'; - goto next_format; - case '.': - /* XXX for now eat numeric formatting */ - goto next_format; - case '%': - OUTPUT_CHAR('%'); - break; - case 'c': - uc = va_arg(ap, unsigned int); - OUTPUT_CHAR(uc); - break; - case 's': - s = va_arg(ap, const char *); - if (s == 0) - s = "<null>"; - flags &= ~LEADZEROFLAG; /* doesn't make sense for strings */ - goto _output_string; - case '-': - flags |= LEFTFORMATFLAG; - goto next_format; - case '+': - flags |= SHOWSIGNFLAG; - goto next_format; - case ' ': - flags |= BLANKPOSFLAG; - goto next_format; - case '#': - flags |= ALTFLAG; - goto next_format; - case 'l': - if (flags & LONGFLAG) - flags |= LONGLONGFLAG; - flags |= LONGFLAG; - goto next_format; - case 'h': - if (flags & HALFFLAG) - flags |= HALFHALFFLAG; - flags |= HALFFLAG; - goto next_format; - case 'z': - flags |= SIZETFLAG; - goto next_format; - case 'j': - flags |= INTMAXFLAG; - goto next_format; - case 't': - flags |= PTRDIFFFLAG; - goto next_format; - case 'i': - case 'd': - n = (flags & LONGLONGFLAG) ? va_arg(ap, long long) : - (flags & LONGFLAG) ? va_arg(ap, long) : - (flags & HALFHALFFLAG) ? (signed char)va_arg(ap, int) : - (flags & HALFFLAG) ? (short)va_arg(ap, int) : - (flags & SIZETFLAG) ? va_arg(ap, ssize_t) : - (flags & INTMAXFLAG) ? va_arg(ap, intmax_t) : - (flags & PTRDIFFFLAG) ? va_arg(ap, ptrdiff_t) : - va_arg(ap, int); - flags |= SIGNEDFLAG; - s = longlong_to_string(num_buffer, n, sizeof(num_buffer), flags, &signchar); - goto _output_string; - case 'u': - n = (flags & LONGLONGFLAG) ? va_arg(ap, unsigned long long) : - (flags & LONGFLAG) ? va_arg(ap, unsigned long) : - (flags & HALFHALFFLAG) ? (unsigned char)va_arg(ap, unsigned int) : - (flags & HALFFLAG) ? (unsigned short)va_arg(ap, unsigned int) : - (flags & SIZETFLAG) ? va_arg(ap, size_t) : - (flags & INTMAXFLAG) ? va_arg(ap, uintmax_t) : - (flags & PTRDIFFFLAG) ? (uintptr_t)va_arg(ap, ptrdiff_t) : - va_arg(ap, unsigned int); - s = longlong_to_string(num_buffer, n, sizeof(num_buffer), flags, &signchar); - goto _output_string; - case 'p': - flags |= LONGFLAG | ALTFLAG; - goto hex; - case 'X': - flags |= CAPSFLAG; - /* fallthrough */ + /* grab the next format character */ + c = *fmt++; + if (c == 0) + break; + + switch (c) { + case '0'...'9': + if (c == '0' && format_num == 0) + flags |= LEADZEROFLAG; + format_num *= 10; + format_num += c - '0'; + goto next_format; + case '.': + /* XXX for now eat numeric formatting */ + goto next_format; + case '%': + OUTPUT_CHAR('%'); + break; + case 'c': + uc = va_arg(ap, unsigned int); + OUTPUT_CHAR(uc); + break; + case 's': + s = va_arg(ap, const char *); + if (s == 0) + s = "<null>"; + flags &= ~LEADZEROFLAG; /* doesn't make sense for strings */ + goto _output_string; + case '-': + flags |= LEFTFORMATFLAG; + goto next_format; + case '+': + flags |= SHOWSIGNFLAG; + goto next_format; + case ' ': + flags |= BLANKPOSFLAG; + goto next_format; + case '#': + flags |= ALTFLAG; + goto next_format; + case 'l': + if (flags & LONGFLAG) + flags |= LONGLONGFLAG; + flags |= LONGFLAG; + goto next_format; + case 'h': + if (flags & HALFFLAG) + flags |= HALFHALFFLAG; + flags |= HALFFLAG; + goto next_format; + case 'z': + flags |= SIZETFLAG; + goto next_format; + case 'j': + flags |= INTMAXFLAG; + goto next_format; + case 't': + flags |= PTRDIFFFLAG; + goto next_format; + case 'i': + case 'd': + n = (flags & LONGLONGFLAG) ? va_arg(ap, long long) : + (flags & LONGFLAG) ? va_arg(ap, long) : + (flags & HALFHALFFLAG) ? (signed char)va_arg(ap, int) : + (flags & HALFFLAG) ? (short)va_arg(ap, int) : + (flags & SIZETFLAG) ? va_arg(ap, ssize_t) : + (flags & INTMAXFLAG) ? va_arg(ap, intmax_t) : + (flags & PTRDIFFFLAG) ? va_arg(ap, ptrdiff_t) : + va_arg(ap, int); + flags |= SIGNEDFLAG; + s = longlong_to_string(num_buffer, n, sizeof(num_buffer), flags, &signchar); + goto _output_string; + case 'u': + n = (flags & LONGLONGFLAG) ? va_arg(ap, unsigned long long) : + (flags & LONGFLAG) ? va_arg(ap, unsigned long) : + (flags & HALFHALFFLAG) ? (unsigned char)va_arg(ap, unsigned int) : + (flags & HALFFLAG) ? (unsigned short)va_arg(ap, unsigned int) : + (flags & SIZETFLAG) ? va_arg(ap, size_t) : + (flags & INTMAXFLAG) ? va_arg(ap, uintmax_t) : + (flags & PTRDIFFFLAG) ? (uintptr_t)va_arg(ap, ptrdiff_t) : + va_arg(ap, unsigned int); + s = longlong_to_string(num_buffer, n, sizeof(num_buffer), flags, &signchar); + goto _output_string; + case 'p': + flags |= LONGFLAG | ALTFLAG; + goto hex; + case 'X': + flags |= CAPSFLAG; + /* fallthrough */ hex: - case 'x': - n = (flags & LONGLONGFLAG) ? va_arg(ap, unsigned long long) : - (flags & LONGFLAG) ? va_arg(ap, unsigned long) : - (flags & HALFHALFFLAG) ? (unsigned char)va_arg(ap, unsigned int) : - (flags & HALFFLAG) ? (unsigned short)va_arg(ap, unsigned int) : - (flags & SIZETFLAG) ? va_arg(ap, size_t) : - (flags & INTMAXFLAG) ? va_arg(ap, uintmax_t) : - (flags & PTRDIFFFLAG) ? (uintptr_t)va_arg(ap, ptrdiff_t) : - va_arg(ap, unsigned int); - s = longlong_to_hexstring(num_buffer, n, sizeof(num_buffer), flags); - if (flags & ALTFLAG) { - OUTPUT_CHAR('0'); - OUTPUT_CHAR((flags & CAPSFLAG) ? 'X': 'x'); - } - goto _output_string; - case 'n': - ptr = va_arg(ap, void *); - if (flags & LONGLONGFLAG) - *(long long *)ptr = chars_written; - else if (flags & LONGFLAG) - *(long *)ptr = chars_written; - else if (flags & HALFHALFFLAG) - *(signed char *)ptr = chars_written; - else if (flags & HALFFLAG) - *(short *)ptr = chars_written; - else if (flags & SIZETFLAG) - *(size_t *)ptr = chars_written; - else - *(int *)ptr = chars_written; - break; + case 'x': + n = (flags & LONGLONGFLAG) ? va_arg(ap, unsigned long long) : + (flags & LONGFLAG) ? va_arg(ap, unsigned long) : + (flags & HALFHALFFLAG) ? (unsigned char)va_arg(ap, unsigned int) : + (flags & HALFFLAG) ? (unsigned short)va_arg(ap, unsigned int) : + (flags & SIZETFLAG) ? va_arg(ap, size_t) : + (flags & INTMAXFLAG) ? va_arg(ap, uintmax_t) : + (flags & PTRDIFFFLAG) ? (uintptr_t)va_arg(ap, ptrdiff_t) : + va_arg(ap, unsigned int); + s = longlong_to_hexstring(num_buffer, n, sizeof(num_buffer), flags); + if (flags & ALTFLAG) { + OUTPUT_CHAR('0'); + OUTPUT_CHAR((flags & CAPSFLAG) ? 'X': 'x'); + } + goto _output_string; + case 'n': + ptr = va_arg(ap, void *); + if (flags & LONGLONGFLAG) + *(long long *)ptr = chars_written; + else if (flags & LONGFLAG) + *(long *)ptr = chars_written; + else if (flags & HALFHALFFLAG) + *(signed char *)ptr = chars_written; + else if (flags & HALFFLAG) + *(short *)ptr = chars_written; + else if (flags & SIZETFLAG) + *(size_t *)ptr = chars_written; + else + *(int *)ptr = chars_written; + break; #if FLOAT_PRINTF - case 'F': - flags |= CAPSFLAG; - /* fallthrough */ - case 'f': { - double d = va_arg(ap, double); - s = double_to_string(num_buffer, sizeof(num_buffer), d, flags); - goto _output_string; - } - case 'A': - flags |= CAPSFLAG; - /* fallthrough */ - case 'a': { - double d = va_arg(ap, double); - s = double_to_hexstring(num_buffer, sizeof(num_buffer), d, flags); - goto _output_string; - } + case 'F': + flags |= CAPSFLAG; + /* fallthrough */ + case 'f': { + double d = va_arg(ap, double); + s = double_to_string(num_buffer, sizeof(num_buffer), d, flags); + goto _output_string; + } + case 'A': + flags |= CAPSFLAG; + /* fallthrough */ + case 'a': { + double d = va_arg(ap, double); + s = double_to_hexstring(num_buffer, sizeof(num_buffer), d, flags); + goto _output_string; + } #endif - default: - OUTPUT_CHAR('%'); - OUTPUT_CHAR(c); - break; - } + default: + OUTPUT_CHAR('%'); + OUTPUT_CHAR(c); + break; + } - /* move on to the next field */ - continue; + /* move on to the next field */ + continue; - /* shared output code */ + /* shared output code */ _output_string: - string_len = strlen(s); - - if (flags & LEFTFORMATFLAG) { - /* left justify the text */ - OUTPUT_STRING(s, string_len); - uint written = err; - - /* pad to the right (if necessary) */ - for (; format_num > written; format_num--) - OUTPUT_CHAR(' '); - } else { - /* right justify the text (digits) */ - - /* if we're going to print a sign digit, - it'll chew up one byte of the format size */ - if (signchar != '\0' && format_num > 0) - format_num--; - - /* output the sign char before the leading zeros */ - if (flags & LEADZEROFLAG && signchar != '\0') - OUTPUT_CHAR(signchar); - - /* pad according to the format string */ - for (; format_num > string_len; format_num--) - OUTPUT_CHAR(flags & LEADZEROFLAG ? '0' : ' '); - - /* if not leading zeros, output the sign char just before the number */ - if (!(flags & LEADZEROFLAG) && signchar != '\0') - OUTPUT_CHAR(signchar); - - /* output the string */ - OUTPUT_STRING(s, string_len); - } - continue; - } + string_len = strlen(s); + + if (flags & LEFTFORMATFLAG) { + /* left justify the text */ + OUTPUT_STRING(s, string_len); + uint written = err; + + /* pad to the right (if necessary) */ + for (; format_num > written; format_num--) + OUTPUT_CHAR(' '); + } else { + /* right justify the text (digits) */ + + /* if we're going to print a sign digit, + it'll chew up one byte of the format size */ + if (signchar != '\0' && format_num > 0) + format_num--; + + /* output the sign char before the leading zeros */ + if (flags & LEADZEROFLAG && signchar != '\0') + OUTPUT_CHAR(signchar); + + /* pad according to the format string */ + for (; format_num > string_len; format_num--) + OUTPUT_CHAR(flags & LEADZEROFLAG ? '0' : ' '); + + /* if not leading zeros, output the sign char just before the number */ + if (!(flags & LEADZEROFLAG) && signchar != '\0') + OUTPUT_CHAR(signchar); + + /* output the string */ + OUTPUT_STRING(s, string_len); + } + continue; + } #undef OUTPUT_STRING #undef OUTPUT_CHAR exit: - return (err < 0) ? err : (int)chars_written; + return (err < 0) ? err : (int)chars_written; } - -// vim: set ts=4 sw=4 noexpandtab: diff --git a/lib/libc/pure_virtual.cpp b/lib/libc/pure_virtual.cpp index cace8059..5d3fc0a1 100644 --- a/lib/libc/pure_virtual.cpp +++ b/lib/libc/pure_virtual.cpp @@ -24,6 +24,6 @@ extern "C" void __cxa_pure_virtual(void) { - panic("pure virtual called\n"); + panic("pure virtual called\n"); } diff --git a/lib/libc/qsort.c b/lib/libc/qsort.c index f6fc8e1d..ddd1c45a 100644 --- a/lib/libc/qsort.c +++ b/lib/libc/qsort.c @@ -1,7 +1,7 @@ -/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ +/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ /*- * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * 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 @@ -31,131 +31,132 @@ #include <sys/types.h> #include <stdlib.h> -static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); -static __inline void swapfunc(char *, char *, int, int); +static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); +static __inline void swapfunc(char *, char *, int, int); -#define min(a, b) (a) < (b) ? a : b +#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); \ - TYPE *pi = (TYPE *) (parmi); \ - TYPE *pj = (TYPE *) (parmj); \ - do { \ - TYPE t = *pi; \ - *pi++ = *pj; \ - *pj++ = t; \ - } while (--i > 0); \ +#define swapcode(TYPE, parmi, parmj, n) { \ + long i = (n) / sizeof (TYPE); \ + TYPE *pi = (TYPE *) (parmi); \ + TYPE *pj = (TYPE *) (parmj); \ + do { \ + TYPE t = *pi; \ + *pi++ = *pj; \ + *pj++ = t; \ + } while (--i > 0); \ } #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ - es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; static __inline void swapfunc(char *a, char *b, int n, int swaptype) { - if (swaptype <= 1) - swapcode(long, a, b, n) - else - swapcode(char, a, b, n) -} + if (swaptype <= 1) + swapcode(long, a, b, n) + else + swapcode(char, a, b, n) + } -#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 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) +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) static __inline char * med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) { - 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 )); + 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 *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) { - char *pa, *pb, *pc, *pd, *pl, *pm, *pn; - int d, r, swaptype, swap_cnt; - char *a = aa; + char *pa, *pb, *pc, *pd, *pl, *pm, *pn; + int d, r, swaptype, swap_cnt; + char *a = aa; -loop: SWAPINIT(a, es); - 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); - return; - } - pm = (char *)a + (n / 2) * es; - if (n > 7) { - pl = (char *)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); - } - pm = med3(pl, pm, pn, cmp); - } - swap(a, pm); - pa = pb = (char *)a + es; +loop: + SWAPINIT(a, es); + 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); + return; + } + pm = (char *)a + (n / 2) * es; + if (n > 7) { + pl = (char *)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); + } + pm = med3(pl, pm, pn, cmp); + } + swap(a, pm); + pa = pb = (char *)a + es; - pc = pd = (char *)a + (n - 1) * es; - for (;;) { - while (pb <= pc && (r = cmp(pb, a)) <= 0) { - if (r == 0) { - swap_cnt = 1; - swap(pa, pb); - pa += es; - } - pb += es; - } - while (pb <= pc && (r = cmp(pc, a)) >= 0) { - if (r == 0) { - swap_cnt = 1; - swap(pc, pd); - pd -= es; - } - pc -= es; - } - if (pb > pc) - break; - swap(pb, pc); - 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); - return; - } + pc = pd = (char *)a + (n - 1) * es; + for (;;) { + while (pb <= pc && (r = cmp(pb, a)) <= 0) { + if (r == 0) { + swap_cnt = 1; + swap(pa, pb); + pa += es; + } + pb += es; + } + while (pb <= pc && (r = cmp(pc, a)) >= 0) { + if (r == 0) { + swap_cnt = 1; + swap(pc, pd); + pd -= es; + } + pc -= es; + } + if (pb > pc) + break; + swap(pb, pc); + 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); + return; + } - pn = (char *)a + n * es; - r = min(pa - (char *)a, pb - pa); - vecswap(a, pb - r, r); - r = min(pd - pc, pn - pd - (int)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; - } - /* qsort(pn - r, r / es, es, cmp); */ + pn = (char *)a + n * es; + r = min(pa - (char *)a, pb - pa); + vecswap(a, pb - r, r); + r = min(pd - pc, pn - pd - (int)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; + } + /* qsort(pn - r, r / es, es, cmp); */ } diff --git a/lib/libc/rand.c b/lib/libc/rand.c index c7d5a2d8..fe502c16 100644 --- a/lib/libc/rand.c +++ b/lib/libc/rand.c @@ -27,23 +27,23 @@ static unsigned int randseed = 12345; void srand(unsigned int seed) { - randseed = seed; + randseed = seed; } void rand_add_entropy(const void *buf, size_t len) { - if (len == 0) - return; + if (len == 0) + return; - uint32_t enp = 0; - for (size_t i = 0; i < len; i++) { - enp ^= ((enp << 8) | (enp >> 24)) ^ ((const uint8_t *)buf)[i]; - } + uint32_t enp = 0; + for (size_t i = 0; i < len; i++) { + enp ^= ((enp << 8) | (enp >> 24)) ^ ((const uint8_t *)buf)[i]; + } - randseed ^= enp; + randseed ^= enp; } int rand(void) { - return (randseed = randseed * 1664525 + 1013904223); + return (randseed = randseed * 1664525 + 1013904223); } diff --git a/lib/libc/stdio.c b/lib/libc/stdio.c index 51b4ec7f..2c96e941 100644 --- a/lib/libc/stdio.c +++ b/lib/libc/stdio.c @@ -28,61 +28,61 @@ int fputc(int c, FILE *fp) { - return fp->fputc(fp->ctx, c); + return fp->fputc(fp->ctx, c); } int putchar(int c) { - return fputc(c, stdout); + return fputc(c, stdout); } int puts(const char *str) { - int err = fputs(str, stdout); - if (err >= 0) - err = fputc('\n', stdout); - return err; + int err = fputs(str, stdout); + if (err >= 0) + err = fputc('\n', stdout); + return err; } int fputs(const char *s, FILE *fp) { - return fp->fputs(fp->ctx, s); + return fp->fputs(fp->ctx, s); } int getc(FILE *fp) { - return fp->fgetc(fp->ctx); + return fp->fgetc(fp->ctx); } int getchar(void) { - return getc(stdin); + return getc(stdin); } int vfprintf(FILE *fp, const char *fmt, va_list ap) { - return fp->vfprintf(fp->ctx, fmt, ap); + return fp->vfprintf(fp->ctx, fmt, ap); } int fprintf(FILE *fp, const char *fmt, ...) { - va_list ap; - int err; + va_list ap; + int err; - va_start(ap, fmt); - err = vfprintf(fp, fmt, ap); - va_end(ap); - return err; + va_start(ap, fmt); + err = vfprintf(fp, fmt, ap); + va_end(ap); + return err; } int _printf(const char *fmt, ...) { - va_list ap; - int err; + va_list ap; + int err; - va_start(ap, fmt); - err = vfprintf(stdout, fmt, ap); - va_end(ap); + va_start(ap, fmt); + err = vfprintf(stdout, fmt, ap); + va_end(ap); - return err; + return err; } diff --git a/lib/libc/string/arch/arm/arm/memcpy.S b/lib/libc/string/arch/arm/arm/memcpy.S index e899ab0b..efe46df4 100644 --- a/lib/libc/string/arch/arm/arm/memcpy.S +++ b/lib/libc/string/arch/arm/arm/memcpy.S @@ -28,151 +28,151 @@ /* void bcopy(const void *src, void *dest, size_t n); */ FUNCTION(bcopy) - // swap args for bcopy - mov r12, r0 - mov r0, r1 - mov r1, r12 + // swap args for bcopy + mov r12, r0 + mov r0, r1 + mov r1, r12 /* void *memcpy(void *dest, const void *src, size_t n); */ FUNCTION(memmove) FUNCTION(memcpy) - // check for zero length copy or the same pointer - cmp r2, #0 - cmpne r1, r0 - bxeq lr - - // save a few registers for use and the return code (input dst) - stmfd sp!, {r0, r4, r5, lr} - - // check for forwards overlap (src > dst, distance < len) - subs r3, r0, r1 - cmphi r2, r3 - bhi .L_forwardoverlap - - // check for a short copy len. - // 20 bytes is enough so that if a 16 byte alignment needs to happen there is at least a - // wordwise copy worth of work to be done. - cmp r2, #(16+4) - blo .L_bytewise - - // see if they are similarly aligned on 4 byte boundaries - eor r3, r0, r1 - tst r3, #3 - bne .L_bytewise // dissimilarly aligned, nothing we can do (for now) - - // check for 16 byte alignment on dst. - // this will also catch src being not 4 byte aligned, since it is similarly 4 byte - // aligned with dst at this point. - tst r0, #15 - bne .L_not16bytealigned - - // check to see if we have at least 32 bytes of data to copy. - // if not, just revert to wordwise copy - cmp r2, #32 - blo .L_wordwise + // check for zero length copy or the same pointer + cmp r2, #0 + cmpne r1, r0 + bxeq lr + + // save a few registers for use and the return code (input dst) + stmfd sp!, {r0, r4, r5, lr} + + // check for forwards overlap (src > dst, distance < len) + subs r3, r0, r1 + cmphi r2, r3 + bhi .L_forwardoverlap + + // check for a short copy len. + // 20 bytes is enough so that if a 16 byte alignment needs to happen there is at least a + // wordwise copy worth of work to be done. + cmp r2, #(16+4) + blo .L_bytewise + + // see if they are similarly aligned on 4 byte boundaries + eor r3, r0, r1 + tst r3, #3 + bne .L_bytewise // dissimilarly aligned, nothing we can do (for now) + + // check for 16 byte alignment on dst. + // this will also catch src being not 4 byte aligned, since it is similarly 4 byte + // aligned with dst at this point. + tst r0, #15 + bne .L_not16bytealigned + + // check to see if we have at least 32 bytes of data to copy. + // if not, just revert to wordwise copy + cmp r2, #32 + blo .L_wordwise .L_bigcopy: - // copy 32 bytes at a time. src & dst need to be at least 4 byte aligned, - // and we need at least 32 bytes remaining to copy + // copy 32 bytes at a time. src & dst need to be at least 4 byte aligned, + // and we need at least 32 bytes remaining to copy - // save r6-r7 for use in the big copy - stmfd sp!, {r6-r11} + // save r6-r7 for use in the big copy + stmfd sp!, {r6-r11} - sub r2, r2, #32 // subtract an extra 32 to the len so we can avoid an extra compare + sub r2, r2, #32 // subtract an extra 32 to the len so we can avoid an extra compare .L_bigcopy_loop: - pld [r1, #64] - ldmia r1!, {r4-r11} - subs r2, r2, #32 - stmia r0!, {r4-r11} - bhs .L_bigcopy_loop + pld [r1, #64] + ldmia r1!, {r4-r11} + subs r2, r2, #32 + stmia r0!, {r4-r11} + bhs .L_bigcopy_loop - // restore r6-r7 - ldmfd sp!, {r6-r11} + // restore r6-r7 + ldmfd sp!, {r6-r11} - // see if we are done - adds r2, r2, #32 - beq .L_done + // see if we are done + adds r2, r2, #32 + beq .L_done - // less then 4 bytes left? - cmp r2, #4 - blo .L_bytewise + // less then 4 bytes left? + cmp r2, #4 + blo .L_bytewise .L_wordwise: - // copy 4 bytes at a time. - // src & dst are guaranteed to be word aligned, and at least 4 bytes are left to copy. - subs r2, r2, #4 + // copy 4 bytes at a time. + // src & dst are guaranteed to be word aligned, and at least 4 bytes are left to copy. + subs r2, r2, #4 .L_wordwise_loop: - ldr r3, [r1], #4 - subs r2, r2, #4 - str r3, [r0], #4 - bhs .L_wordwise_loop + ldr r3, [r1], #4 + subs r2, r2, #4 + str r3, [r0], #4 + bhs .L_wordwise_loop - // correct the remaining len and test for completion - adds r2, r2, #4 - beq .L_done + // correct the remaining len and test for completion + adds r2, r2, #4 + beq .L_done .L_bytewise: - // simple bytewise copy - ldrb r3, [r1], #1 - subs r2, r2, #1 - strb r3, [r0], #1 - bhi .L_bytewise + // simple bytewise copy + ldrb r3, [r1], #1 + subs r2, r2, #1 + strb r3, [r0], #1 + bhi .L_bytewise .L_done: - // load dst for return and restore r4,r5 + // load dst for return and restore r4,r5 #if ARM_ARCH_LEVEL >= 5 - ldmfd sp!, {r0, r4, r5, pc} + ldmfd sp!, {r0, r4, r5, pc} #else - ldmfd sp!, {r0, r4, r5, lr} - bx lr + ldmfd sp!, {r0, r4, r5, lr} + bx lr #endif .L_not16bytealigned: - // dst is not 16 byte aligned, so we will copy up to 15 bytes to get it aligned. - // src is guaranteed to be similarly word aligned with dst. - - // set the condition flags based on the alignment. - lsl r12, r0, #28 - rsb r12, r12, #0 - msr CPSR_f, r12 // move into NZCV fields in CPSR - - // move as many bytes as necessary to get the dst aligned - ldrvsb r3, [r1], #1 // V set - ldrcsh r4, [r1], #2 // C set - ldreq r5, [r1], #4 // Z set - - strvsb r3, [r0], #1 - strcsh r4, [r0], #2 - streq r5, [r0], #4 - - ldmmiia r1!, {r3-r4} // N set - stmmiia r0!, {r3-r4} - - // fix the remaining len - sub r2, r2, r12, lsr #28 - - // test to see what we should do now - cmp r2, #32 - bhs .L_bigcopy - b .L_wordwise - - // src and dest overlap 'forwards' or dst > src + // dst is not 16 byte aligned, so we will copy up to 15 bytes to get it aligned. + // src is guaranteed to be similarly word aligned with dst. + + // set the condition flags based on the alignment. + lsl r12, r0, #28 + rsb r12, r12, #0 + msr CPSR_f, r12 // move into NZCV fields in CPSR + + // move as many bytes as necessary to get the dst aligned + ldrvsb r3, [r1], #1 // V set + ldrcsh r4, [r1], #2 // C set + ldreq r5, [r1], #4 // Z set + + strvsb r3, [r0], #1 + strcsh r4, [r0], #2 + streq r5, [r0], #4 + + ldmmiia r1!, {r3-r4} // N set + stmmiia r0!, {r3-r4} + + // fix the remaining len + sub r2, r2, r12, lsr #28 + + // test to see what we should do now + cmp r2, #32 + bhs .L_bigcopy + b .L_wordwise + + // src and dest overlap 'forwards' or dst > src .L_forwardoverlap: - // do a bytewise reverse copy for now - add r1, r1, r2 - add r0, r0, r2 - sub r1, r1, #1 - sub r0, r0, #1 + // do a bytewise reverse copy for now + add r1, r1, r2 + add r0, r0, r2 + sub r1, r1, #1 + sub r0, r0, #1 .L_bytewisereverse: - // simple bytewise reverse copy - ldrb r3, [r1], #-1 - subs r2, r2, #1 - strb r3, [r0], #-1 - bhi .L_bytewisereverse + // simple bytewise reverse copy + ldrb r3, [r1], #-1 + subs r2, r2, #1 + strb r3, [r0], #-1 + bhi .L_bytewisereverse - b .L_done + b .L_done diff --git a/lib/libc/string/arch/arm/arm/memset.S b/lib/libc/string/arch/arm/arm/memset.S index b31d0532..1307e1c3 100644 --- a/lib/libc/string/arch/arm/arm/memset.S +++ b/lib/libc/string/arch/arm/arm/memset.S @@ -28,86 +28,86 @@ /* void bzero(void *s, size_t n); */ FUNCTION(bzero) - mov r2, r1 - mov r1, #0 + mov r2, r1 + mov r1, #0 /* void *memset(void *s, int c, size_t n); */ FUNCTION(memset) - // check for zero length - cmp r2, #0 - bxeq lr + // check for zero length + cmp r2, #0 + bxeq lr - // save the original pointer - mov r12, r0 + // save the original pointer + mov r12, r0 - // short memsets aren't worth optimizing - cmp r2, #(32 + 16) - blt .L_bytewise + // short memsets aren't worth optimizing + cmp r2, #(32 + 16) + blt .L_bytewise - // fill a 32 bit register with the 8 bit value - and r1, r1, #0xff - orr r1, r1, r1, lsl #8 - orr r1, r1, r1, lsl #16 + // fill a 32 bit register with the 8 bit value + and r1, r1, #0xff + orr r1, r1, r1, lsl #8 + orr r1, r1, r1, lsl #16 - // check for 16 byte alignment - tst r0, #15 - bne .L_not16bytealigned + // check for 16 byte alignment + tst r0, #15 + bne .L_not16bytealigned .L_bigset: - // dump some registers to make space for our values - stmfd sp!, { r4-r5 } - - // fill a bunch of registers with the set value - mov r3, r1 - mov r4, r1 - mov r5, r1 - - // prepare the count register so we can avoid an extra compare - sub r2, r2, #32 - - // 32 bytes at a time + // dump some registers to make space for our values + stmfd sp!, { r4-r5 } + + // fill a bunch of registers with the set value + mov r3, r1 + mov r4, r1 + mov r5, r1 + + // prepare the count register so we can avoid an extra compare + sub r2, r2, #32 + + // 32 bytes at a time .L_bigset_loop: - stmia r0!, { r1, r3, r4, r5 } - subs r2, r2, #32 - stmia r0!, { r1, r3, r4, r5 } - bge .L_bigset_loop + stmia r0!, { r1, r3, r4, r5 } + subs r2, r2, #32 + stmia r0!, { r1, r3, r4, r5 } + bge .L_bigset_loop - // restore our dumped registers - ldmfd sp!, { r4-r5 } + // restore our dumped registers + ldmfd sp!, { r4-r5 } - // see if we're done - adds r2, r2, #32 - beq .L_done + // see if we're done + adds r2, r2, #32 + beq .L_done .L_bytewise: - // bytewise memset - subs r2, r2, #1 - strb r1, [r0], #1 - bgt .L_bytewise + // bytewise memset + subs r2, r2, #1 + strb r1, [r0], #1 + bgt .L_bytewise .L_done: - // restore the base pointer as return value - mov r0, r12 - bx lr + // restore the base pointer as return value + mov r0, r12 + bx lr .L_not16bytealigned: - // dst is not 16 byte aligned, so we will set up to 15 bytes to get it aligned. + // dst is not 16 byte aligned, so we will set up to 15 bytes to get it aligned. - // set the condition flags based on the alignment. - lsl r3, r0, #28 - rsb r3, r3, #0 - msr CPSR_f, r3 // move into NZCV fields in CPSR + // set the condition flags based on the alignment. + lsl r3, r0, #28 + rsb r3, r3, #0 + msr CPSR_f, r3 // move into NZCV fields in CPSR - // move as many bytes as necessary to get the dst aligned - strvsb r1, [r0], #1 // V set - strcsh r1, [r0], #2 // C set - streq r1, [r0], #4 // Z set - strmi r1, [r0], #4 // N set - strmi r1, [r0], #4 // N set + // move as many bytes as necessary to get the dst aligned + strvsb r1, [r0], #1 // V set + strcsh r1, [r0], #2 // C set + streq r1, [r0], #4 // Z set + strmi r1, [r0], #4 // N set + strmi r1, [r0], #4 // N set - // fix the remaining len - sub r2, r2, r3, lsr #28 + // fix the remaining len + sub r2, r2, r3, lsr #28 - // do the large memset - b .L_bigset + // do the large memset + b .L_bigset diff --git a/lib/libc/string/arch/x86-64/memcpy.S b/lib/libc/string/arch/x86-64/memcpy.S index a2a2a44d..84aec44a 100644 --- a/lib/libc/string/arch/x86-64/memcpy.S +++ b/lib/libc/string/arch/x86-64/memcpy.S @@ -29,10 +29,10 @@ /* void bcopy(const void *src, void *dest, size_t n); */ FUNCTION(bcopy) - ret + ret /* void *memcpy(void *dest, const void *src, size_t n); */ FUNCTION(memmove) FUNCTION(memcpy) - ret + ret diff --git a/lib/libc/string/arch/x86-64/memset.S b/lib/libc/string/arch/x86-64/memset.S index b4bccc94..43367f6d 100644 --- a/lib/libc/string/arch/x86-64/memset.S +++ b/lib/libc/string/arch/x86-64/memset.S @@ -29,9 +29,9 @@ /* void bzero(void *s, size_t n); */ FUNCTION(bzero) - ret + ret /* void *memset(void *s, int c, size_t n); */ FUNCTION(memset) - ret + ret diff --git a/lib/libc/string/arch/x86/memcpy.S b/lib/libc/string/arch/x86/memcpy.S index a2a2a44d..84aec44a 100644 --- a/lib/libc/string/arch/x86/memcpy.S +++ b/lib/libc/string/arch/x86/memcpy.S @@ -29,10 +29,10 @@ /* void bcopy(const void *src, void *dest, size_t n); */ FUNCTION(bcopy) - ret + ret /* void *memcpy(void *dest, const void *src, size_t n); */ FUNCTION(memmove) FUNCTION(memcpy) - ret + ret diff --git a/lib/libc/string/arch/x86/memset.S b/lib/libc/string/arch/x86/memset.S index b4bccc94..43367f6d 100644 --- a/lib/libc/string/arch/x86/memset.S +++ b/lib/libc/string/arch/x86/memset.S @@ -29,9 +29,9 @@ /* void bzero(void *s, size_t n); */ FUNCTION(bzero) - ret + ret /* void *memset(void *s, int c, size_t n); */ FUNCTION(memset) - ret + ret diff --git a/lib/libc/string/bcopy.c b/lib/libc/string/bcopy.c index 6e48ab8f..cc025ffc 100644 --- a/lib/libc/string/bcopy.c +++ b/lib/libc/string/bcopy.c @@ -29,6 +29,6 @@ void bcopy(void const *src, void *dest, size_t count) { - memcpy(dest, src, count); + memcpy(dest, src, count); } diff --git a/lib/libc/string/bzero.c b/lib/libc/string/bzero.c index 65b35023..b787d4ea 100644 --- a/lib/libc/string/bzero.c +++ b/lib/libc/string/bzero.c @@ -30,6 +30,6 @@ void bzero(void *dst, size_t count) { - memset(dst, 0, count); + memset(dst, 0, count); } diff --git a/lib/libc/string/memchr.c b/lib/libc/string/memchr.c index 459f0ac1..cb3b4bf5 100644 --- a/lib/libc/string/memchr.c +++ b/lib/libc/string/memchr.c @@ -30,16 +30,16 @@ void * memchr(void const *buf, int c, size_t len) { - size_t i; - unsigned char const *b= buf; - unsigned char x= (c&0xff); + size_t i; + unsigned char const *b= buf; + unsigned char x= (c&0xff); - for (i= 0; i< len; i++) { - if (b[i]== x) { - return (void*)(b+i); - } - } + for (i= 0; i< len; i++) { + if (b[i]== x) { + return (void*)(b+i); + } + } - return NULL; + return NULL; } diff --git a/lib/libc/string/memcmp.c b/lib/libc/string/memcmp.c index d3b236b9..d6c0abdc 100644 --- a/lib/libc/string/memcmp.c +++ b/lib/libc/string/memcmp.c @@ -30,11 +30,11 @@ int memcmp(const void *cs, const void *ct, size_t count) { - const unsigned char *su1, *su2; - signed char res = 0; + const unsigned char *su1, *su2; + signed char res = 0; - for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) - if ((res = *su1 - *su2) != 0) - break; - return res; + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) + if ((res = *su1 - *su2) != 0) + break; + return res; } diff --git a/lib/libc/string/memcpy.c b/lib/libc/string/memcpy.c index e0472f10..f0e439c2 100644 --- a/lib/libc/string/memcpy.c +++ b/lib/libc/string/memcpy.c @@ -37,33 +37,33 @@ typedef long word; void *memcpy(void *dest, const void *src, size_t count) { - char *d = (char *)dest; - const char *s = (const char *)src; - int len; + char *d = (char *)dest; + const char *s = (const char *)src; + int len; - if (count == 0 || dest == src) - return dest; + if (count == 0 || dest == src) + return dest; - if (((long)d | (long)s) & lmask) { - // src and/or dest do not align on word boundary - if ((((long)d ^ (long)s) & lmask) || (count < lsize)) - len = count; // copy the rest of the buffer with the byte mover - else - len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary + if (((long)d | (long)s) & lmask) { + // src and/or dest do not align on word boundary + if ((((long)d ^ (long)s) & lmask) || (count < lsize)) + len = count; // copy the rest of the buffer with the byte mover + else + len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary - count -= len; - for (; len > 0; len--) - *d++ = *s++; - } - for (len = count / lsize; len > 0; len--) { - *(word *)d = *(word *)s; - d += lsize; - s += lsize; - } - for (len = count & lmask; len > 0; len--) - *d++ = *s++; + count -= len; + for (; len > 0; len--) + *d++ = *s++; + } + for (len = count / lsize; len > 0; len--) { + *(word *)d = *(word *)s; + d += lsize; + s += lsize; + } + for (len = count & lmask; len > 0; len--) + *d++ = *s++; - return dest; + return dest; } #endif diff --git a/lib/libc/string/memmove.c b/lib/libc/string/memmove.c index 87fd5f06..c7da3504 100644 --- a/lib/libc/string/memmove.c +++ b/lib/libc/string/memmove.c @@ -37,56 +37,56 @@ typedef long word; void * memmove(void *dest, void const *src, size_t count) { - char *d = (char *)dest; - const char *s = (const char *)src; - int len; + char *d = (char *)dest; + const char *s = (const char *)src; + int len; - if (count == 0 || dest == src) - return dest; + if (count == 0 || dest == src) + return dest; - if ((long)d < (long)s) { - if (((long)d | (long)s) & lmask) { - // src and/or dest do not align on word boundary - if ((((long)d ^ (long)s) & lmask) || (count < lsize)) - len = count; // copy the rest of the buffer with the byte mover - else - len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary + if ((long)d < (long)s) { + if (((long)d | (long)s) & lmask) { + // src and/or dest do not align on word boundary + if ((((long)d ^ (long)s) & lmask) || (count < lsize)) + len = count; // copy the rest of the buffer with the byte mover + else + len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary - count -= len; - for (; len > 0; len--) - *d++ = *s++; - } - for (len = count / lsize; len > 0; len--) { - *(word *)d = *(word *)s; - d += lsize; - s += lsize; - } - for (len = count & lmask; len > 0; len--) - *d++ = *s++; - } else { - d += count; - s += count; - if (((long)d | (long)s) & lmask) { - // src and/or dest do not align on word boundary - if ((((long)d ^ (long)s) & lmask) || (count <= lsize)) - len = count; - else - len = ((long)d & lmask); + count -= len; + for (; len > 0; len--) + *d++ = *s++; + } + for (len = count / lsize; len > 0; len--) { + *(word *)d = *(word *)s; + d += lsize; + s += lsize; + } + for (len = count & lmask; len > 0; len--) + *d++ = *s++; + } else { + d += count; + s += count; + if (((long)d | (long)s) & lmask) { + // src and/or dest do not align on word boundary + if ((((long)d ^ (long)s) & lmask) || (count <= lsize)) + len = count; + else + len = ((long)d & lmask); - count -= len; - for (; len > 0; len--) - *--d = *--s; - } - for (len = count / lsize; len > 0; len--) { - d -= lsize; - s -= lsize; - *(word *)d = *(word *)s; - } - for (len = count & lmask; len > 0; len--) - *--d = *--s; - } + count -= len; + for (; len > 0; len--) + *--d = *--s; + } + for (len = count / lsize; len > 0; len--) { + d -= lsize; + s -= lsize; + *(word *)d = *(word *)s; + } + for (len = count & lmask; len > 0; len--) + *--d = *--s; + } - return dest; + return dest; } #endif diff --git a/lib/libc/string/memscan.c b/lib/libc/string/memscan.c index 28183544..babe3d38 100644 --- a/lib/libc/string/memscan.c +++ b/lib/libc/string/memscan.c @@ -29,13 +29,13 @@ void *memscan(void *addr, int c, size_t size) { - unsigned char *p = (unsigned char *)addr; + unsigned char *p = (unsigned char *)addr; - while (size) { - if (*p == c) - return (void *)p; - p++; - size--; - } - return (void *)p; + while (size) { + if (*p == c) + return (void *)p; + p++; + size--; + } + return (void *)p; } diff --git a/lib/libc/string/memset.c b/lib/libc/string/memset.c index f39e5f08..9a19e16b 100644 --- a/lib/libc/string/memset.c +++ b/lib/libc/string/memset.c @@ -31,33 +31,33 @@ void * memset(void *s, int c, size_t count) { - char *xs = (char *) s; - size_t len = (-(size_t)s) & (sizeof(size_t)-1); - size_t cc = c & 0xff; + char *xs = (char *) s; + size_t len = (-(size_t)s) & (sizeof(size_t)-1); + size_t cc = c & 0xff; - if ( count > len ) { - count -= len; - cc |= cc << 8; - cc |= cc << 16; - if (sizeof(size_t) == 8) - cc |= (uint64_t)cc << 32; // should be optimized out on 32 bit machines + if ( count > len ) { + count -= len; + cc |= cc << 8; + cc |= cc << 16; + if (sizeof(size_t) == 8) + cc |= (uint64_t)cc << 32; // should be optimized out on 32 bit machines - // write to non-aligned memory byte-wise - for ( ; len > 0; len-- ) - *xs++ = c; + // write to non-aligned memory byte-wise + for ( ; len > 0; len-- ) + *xs++ = c; - // write to aligned memory dword-wise - for ( len = count/sizeof(size_t); len > 0; len-- ) { - *((size_t *)xs) = (size_t)cc; - xs += sizeof(size_t); - } + // write to aligned memory dword-wise + for ( len = count/sizeof(size_t); len > 0; len-- ) { + *((size_t *)xs) = (size_t)cc; + xs += sizeof(size_t); + } - count &= sizeof(size_t)-1; - } + count &= sizeof(size_t)-1; + } - // write remaining bytes - for ( ; count > 0; count-- ) - *xs++ = c; + // write remaining bytes + for ( ; count > 0; count-- ) + *xs++ = c; - return s; + return s; } diff --git a/lib/libc/string/strcat.c b/lib/libc/string/strcat.c index 2d04fd85..c6655196 100644 --- a/lib/libc/string/strcat.c +++ b/lib/libc/string/strcat.c @@ -30,13 +30,13 @@ char * strcat(char *dest, char const*src) { - char *tmp = dest; + char *tmp = dest; - while (*dest) - dest++; - while ((*dest++ = *src++) != '\0') - ; + while (*dest) + dest++; + while ((*dest++ = *src++) != '\0') + ; - return tmp; + return tmp; } diff --git a/lib/libc/string/strchr.c b/lib/libc/string/strchr.c index f9ab14b2..f57e5d14 100644 --- a/lib/libc/string/strchr.c +++ b/lib/libc/string/strchr.c @@ -30,8 +30,8 @@ char * strchr(const char *s, int c) { - for (; *s != (char) c; ++s) - if (*s == '\0') - return NULL; - return (char *) s; + for (; *s != (char) c; ++s) + if (*s == '\0') + return NULL; + return (char *) s; } diff --git a/lib/libc/string/strcmp.c b/lib/libc/string/strcmp.c index 7c80a8c1..a89d57c2 100644 --- a/lib/libc/string/strcmp.c +++ b/lib/libc/string/strcmp.c @@ -30,12 +30,12 @@ int strcmp(char const *cs, char const *ct) { - signed char __res; + signed char __res; - while (1) { - if ((__res = *cs - *ct++) != 0 || !*cs++) - break; - } + while (1) { + if ((__res = *cs - *ct++) != 0 || !*cs++) + break; + } - return __res; + return __res; } diff --git a/lib/libc/string/strcoll.c b/lib/libc/string/strcoll.c index 704539bf..549cb893 100644 --- a/lib/libc/string/strcoll.c +++ b/lib/libc/string/strcoll.c @@ -29,6 +29,6 @@ int strcoll(const char *s1, const char *s2) { - return strcmp(s1, s2); + return strcmp(s1, s2); } diff --git a/lib/libc/string/strcpy.c b/lib/libc/string/strcpy.c index 216405ac..f8f43e13 100644 --- a/lib/libc/string/strcpy.c +++ b/lib/libc/string/strcpy.c @@ -30,10 +30,10 @@ char * strcpy(char *dest, char const *src) { - char *tmp = dest; + char *tmp = dest; - while ((*dest++ = *src++) != '\0') - ; - return tmp; + while ((*dest++ = *src++) != '\0') + ; + return tmp; } diff --git a/lib/libc/string/strdup.c b/lib/libc/string/strdup.c index 047c4789..ee292c48 100644 --- a/lib/libc/string/strdup.c +++ b/lib/libc/string/strdup.c @@ -30,14 +30,14 @@ char * strdup(const char *str) { - size_t len; - char *copy; + size_t len; + char *copy; - len = strlen(str) + 1; - copy = malloc(len); - if (copy == NULL) - return NULL; - memcpy(copy, str, len); - return copy; + len = strlen(str) + 1; + copy = malloc(len); + if (copy == NULL) + return NULL; + memcpy(copy, str, len); + return copy; } diff --git a/lib/libc/string/strerror.c b/lib/libc/string/strerror.c index 73196599..b6b8770c 100644 --- a/lib/libc/string/strerror.c +++ b/lib/libc/string/strerror.c @@ -27,10 +27,10 @@ char const * strerror(int errnum) { - if (errnum < 0) { - return "General Error"; - } else { - return "No Error"; - } + if (errnum < 0) { + return "General Error"; + } else { + return "No Error"; + } } diff --git a/lib/libc/string/strlcat.c b/lib/libc/string/strlcat.c index 9086fa7d..4760e917 100644 --- a/lib/libc/string/strlcat.c +++ b/lib/libc/string/strlcat.c @@ -31,20 +31,20 @@ size_t strlcat(char *dst, char const *src, size_t s) { - size_t i; - size_t j= strnlen(dst, s); + size_t i; + size_t j= strnlen(dst, s); - if (!s) { - return j+strlen(src); - } + if (!s) { + return j+strlen(src); + } - dst+= j; + dst+= j; - for (i= 0; ((i< s-1) && src[i]); i++) { - dst[i]= src[i]; - } + for (i= 0; ((i< s-1) && src[i]); i++) { + dst[i]= src[i]; + } - dst[i]= 0; + dst[i]= 0; - return j + i + strlen(src+i); + return j + i + strlen(src+i); } diff --git a/lib/libc/string/strlcpy.c b/lib/libc/string/strlcpy.c index c377d203..7c76be6a 100644 --- a/lib/libc/string/strlcpy.c +++ b/lib/libc/string/strlcpy.c @@ -31,17 +31,17 @@ size_t strlcpy(char *dst, char const *src, size_t s) { - size_t i= 0; + size_t i= 0; - if (!s) { - return strlen(src); - } + if (!s) { + return strlen(src); + } - for (i= 0; ((i< s-1) && src[i]); i++) { - dst[i]= src[i]; - } + for (i= 0; ((i< s-1) && src[i]); i++) { + dst[i]= src[i]; + } - dst[i]= 0; + dst[i]= 0; - return i + strlen(src+i); + return i + strlen(src+i); } diff --git a/lib/libc/string/strlen.c b/lib/libc/string/strlen.c index 79683a18..1659f78a 100644 --- a/lib/libc/string/strlen.c +++ b/lib/libc/string/strlen.c @@ -30,12 +30,12 @@ size_t strlen(char const *s) { - size_t i; + size_t i; - i= 0; - while (s[i]) { - i+= 1; - } + i= 0; + while (s[i]) { + i+= 1; + } - return i; + return i; } diff --git a/lib/libc/string/strncat.c b/lib/libc/string/strncat.c index ecee0440..078c33d8 100644 --- a/lib/libc/string/strncat.c +++ b/lib/libc/string/strncat.c @@ -30,19 +30,19 @@ char * strncat(char *dest, char const *src, size_t count) { - char *tmp = dest; + char *tmp = dest; - if (count > 0) { - while (*dest) - dest++; - while ((*dest++ = *src++)) { - if (--count == 0) { - *dest = '\0'; - break; - } - } - } + if (count > 0) { + while (*dest) + dest++; + while ((*dest++ = *src++)) { + if (--count == 0) { + *dest = '\0'; + break; + } + } + } - return tmp; + return tmp; } diff --git a/lib/libc/string/strncmp.c b/lib/libc/string/strncmp.c index d59fd3c7..41854821 100644 --- a/lib/libc/string/strncmp.c +++ b/lib/libc/string/strncmp.c @@ -30,13 +30,13 @@ int strncmp(char const *cs, char const *ct, size_t count) { - signed char __res = 0; + signed char __res = 0; - while (count > 0) { - if ((__res = *cs - *ct++) != 0 || !*cs++) - break; - count--; - } + while (count > 0) { + if ((__res = *cs - *ct++) != 0 || !*cs++) + break; + count--; + } - return __res; + return __res; } diff --git a/lib/libc/string/strncpy.c b/lib/libc/string/strncpy.c index e4ce80a4..70ea19bb 100644 --- a/lib/libc/string/strncpy.c +++ b/lib/libc/string/strncpy.c @@ -30,11 +30,11 @@ char * strncpy(char *dest, char const *src, size_t count) { - char *tmp = dest; + char *tmp = dest; - while (count-- && (*dest++ = *src++) != '\0') - ; + while (count-- && (*dest++ = *src++) != '\0') + ; - return tmp; + return tmp; } diff --git a/lib/libc/string/strnicmp.c b/lib/libc/string/strnicmp.c index 45120f41..67c7b9fa 100644 --- a/lib/libc/string/strnicmp.c +++ b/lib/libc/string/strnicmp.c @@ -31,27 +31,27 @@ int strnicmp(char const *s1, char const *s2, size_t len) { - unsigned char c1 = '\0'; - unsigned char c2 = '\0'; + unsigned char c1 = '\0'; + unsigned char c2 = '\0'; - if (len > 0) { - do { - c1 = *s1; - c2 = *s2; - s1++; - s2++; - if (!c1) - break; - if (!c2) - break; - if (c1 == c2) - continue; - c1 = tolower(c1); - c2 = tolower(c2); - if (c1 != c2) - break; - } while (--len); - } - return (int)c1 - (int)c2; + if (len > 0) { + do { + c1 = *s1; + c2 = *s2; + s1++; + s2++; + if (!c1) + break; + if (!c2) + break; + if (c1 == c2) + continue; + c1 = tolower(c1); + c2 = tolower(c2); + if (c1 != c2) + break; + } while (--len); + } + return (int)c1 - (int)c2; } #pragma weak strncasecmp=strnicmp diff --git a/lib/libc/string/strnlen.c b/lib/libc/string/strnlen.c index bdbf8214..07e56e8c 100644 --- a/lib/libc/string/strnlen.c +++ b/lib/libc/string/strnlen.c @@ -30,9 +30,9 @@ size_t strnlen(char const *s, size_t count) { - const char *sc; + const char *sc; - for (sc = s; count-- && *sc != '\0'; ++sc) - ; - return sc - s; + for (sc = s; count-- && *sc != '\0'; ++sc) + ; + return sc - s; } diff --git a/lib/libc/string/strpbrk.c b/lib/libc/string/strpbrk.c index 032e7162..0812037e 100644 --- a/lib/libc/string/strpbrk.c +++ b/lib/libc/string/strpbrk.c @@ -30,15 +30,15 @@ char * strpbrk(char const *cs, char const *ct) { - const char *sc1; - const char *sc2; + const char *sc1; + const char *sc2; - for (sc1 = cs; *sc1 != '\0'; ++sc1) { - for (sc2 = ct; *sc2 != '\0'; ++sc2) { - if (*sc1 == *sc2) - return (char *)sc1; - } - } + for (sc1 = cs; *sc1 != '\0'; ++sc1) { + for (sc2 = ct; *sc2 != '\0'; ++sc2) { + if (*sc1 == *sc2) + return (char *)sc1; + } + } - return NULL; + return NULL; } diff --git a/lib/libc/string/strrchr.c b/lib/libc/string/strrchr.c index 53315c5f..f0dc21ca 100644 --- a/lib/libc/string/strrchr.c +++ b/lib/libc/string/strrchr.c @@ -30,16 +30,16 @@ char * strrchr(char const *s, int c) { - char const *last= c?0:s; + char const *last= c?0:s; - while (*s) { - if (*s== c) { - last= s; - } + while (*s) { + if (*s== c) { + last= s; + } - s+= 1; - } + s+= 1; + } - return (char *)last; + return (char *)last; } diff --git a/lib/libc/string/strspn.c b/lib/libc/string/strspn.c index 10aaa121..bfeb7ac7 100644 --- a/lib/libc/string/strspn.c +++ b/lib/libc/string/strspn.c @@ -30,19 +30,19 @@ size_t strspn(char const *s, char const *accept) { - const char *p; - const char *a; - size_t count = 0; + const char *p; + const char *a; + size_t count = 0; - for (p = s; *p != '\0'; ++p) { - for (a = accept; *a != '\0'; ++a) { - if (*p == *a) - break; - } - if (*a == '\0') - return count; - ++count; - } + for (p = s; *p != '\0'; ++p) { + for (a = accept; *a != '\0'; ++a) { + if (*p == *a) + break; + } + if (*a == '\0') + return count; + ++count; + } - return count; + return count; } diff --git a/lib/libc/string/strstr.c b/lib/libc/string/strstr.c index 7c6996d7..15dc6af6 100644 --- a/lib/libc/string/strstr.c +++ b/lib/libc/string/strstr.c @@ -30,17 +30,17 @@ char * strstr(char const *s1, char const *s2) { - int l1, l2; + int l1, l2; - l2 = strlen(s2); - if (!l2) - return (char *)s1; - l1 = strlen(s1); - while (l1 >= l2) { - l1--; - if (!memcmp(s1,s2,l2)) - return (char *)s1; - s1++; - } - return NULL; + l2 = strlen(s2); + if (!l2) + return (char *)s1; + l1 = strlen(s1); + while (l1 >= l2) { + l1--; + if (!memcmp(s1,s2,l2)) + return (char *)s1; + s1++; + } + return NULL; } diff --git a/lib/libc/string/strtok.c b/lib/libc/string/strtok.c index 1c28680f..48396145 100644 --- a/lib/libc/string/strtok.c +++ b/lib/libc/string/strtok.c @@ -32,20 +32,20 @@ static char *___strtok = NULL; char * strtok(char *s, char const *ct) { - char *sbegin, *send; + char *sbegin, *send; - sbegin = s ? s : ___strtok; - if (!sbegin) { - return NULL; - } - sbegin += strspn(sbegin,ct); - if (*sbegin == '\0') { - ___strtok = NULL; - return( NULL ); - } - send = strpbrk( sbegin, ct); - if (send && *send != '\0') - *send++ = '\0'; - ___strtok = send; - return (sbegin); + sbegin = s ? s : ___strtok; + if (!sbegin) { + return NULL; + } + sbegin += strspn(sbegin,ct); + if (*sbegin == '\0') { + ___strtok = NULL; + return ( NULL ); + } + send = strpbrk( sbegin, ct); + if (send && *send != '\0') + *send++ = '\0'; + ___strtok = send; + return (sbegin); } diff --git a/lib/libc/string/strxfrm.c b/lib/libc/string/strxfrm.c index 284c1f2d..0eff870e 100644 --- a/lib/libc/string/strxfrm.c +++ b/lib/libc/string/strxfrm.c @@ -30,13 +30,13 @@ size_t strxfrm(char *dest, const char *src, size_t n) { - size_t len = strlen(src); + size_t len = strlen(src); - if (n) { - size_t copy_len = len < n ? len : n - 1; - memcpy(dest, src, copy_len); - dest[copy_len] = 0; - } - return len; + if (n) { + size_t copy_len = len < n ? len : n - 1; + memcpy(dest, src, copy_len); + dest[copy_len] = 0; + } + return len; } diff --git a/lib/libc/strtol.c b/lib/libc/strtol.c index a3cdbcdd..d87bde03 100644 --- a/lib/libc/strtol.c +++ b/lib/libc/strtol.c @@ -1,4 +1,4 @@ -/* $OpenBSD: strtol.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ +/* $OpenBSD: strtol.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. @@ -43,98 +43,98 @@ long strtol(const char *nptr, char **endptr, int base) { - const char *s; - long acc, cutoff; - int c; - int neg, any, cutlim; + const char *s; + long acc, cutoff; + int c; + 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; + /* + * 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 longs is - * [-2147483648..2147483647] and the input base is 10, - * cutoff will be set to 214748364 and cutlim to either - * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated - * a value > 214748364, 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 ? LONG_MIN : LONG_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 = LONG_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = LONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); + /* + * 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 longs is + * [-2147483648..2147483647] and the input base is 10, + * cutoff will be set to 214748364 and cutlim to either + * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated + * a value > 214748364, 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 ? LONG_MIN : LONG_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 = LONG_MIN; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc -= c; + } + } else { + if (acc > cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = LONG_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc += c; + } + } + } + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return (acc); } diff --git a/lib/libc/strtoll.c b/lib/libc/strtoll.c index 96e6ed69..e63c66ae 100644 --- a/lib/libc/strtoll.c +++ b/lib/libc/strtoll.c @@ -44,99 +44,99 @@ long long strtoll(const char *nptr, char **endptr, int base) { - const char *s; - long long acc, cutoff; - int c; - int neg, any, cutlim; + const char *s; + long long acc, cutoff; + int c; + 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; + /* + * 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 long longs 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 ? LLONG_MIN : LLONG_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 = LLONG_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = LLONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); + /* + * 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 long longs 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 ? LLONG_MIN : LLONG_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 = LLONG_MIN; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc -= c; + } + } else { + if (acc > cutoff || (acc == cutoff && c > cutlim)) { + any = -1; + acc = LLONG_MAX; + errno = ERANGE; + } else { + any = 1; + acc *= base; + acc += c; + } + } + } + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return (acc); } |