diff --git a/lib/libc/minimal/CMakeLists.txt b/lib/libc/minimal/CMakeLists.txt index 036a4f139c09e..ddf15ab09d550 100644 --- a/lib/libc/minimal/CMakeLists.txt +++ b/lib/libc/minimal/CMakeLists.txt @@ -5,8 +5,7 @@ zephyr_system_include_directories(include) zephyr_library() zephyr_library_sources( source/stdlib/atoi.c - source/stdlib/strtol.c - source/stdlib/strtoul.c + source/stdlib/strtoi.c source/stdlib/malloc.c source/stdlib/bsearch.c source/stdlib/exit.c diff --git a/lib/libc/minimal/include/stdlib.h b/lib/libc/minimal/include/stdlib.h index fe396d4f9546f..23022fd4b4fb5 100644 --- a/lib/libc/minimal/include/stdlib.h +++ b/lib/libc/minimal/include/stdlib.h @@ -16,6 +16,8 @@ extern "C" { unsigned long int strtoul(const char *str, char **endptr, int base); long int strtol(const char *str, char **endptr, int base); +unsigned long long int strtoull(const char *str, char **endptr, int base); +long long int strtoll(const char *str, char **endptr, int base); int atoi(const char *s); void *malloc(size_t size); diff --git a/lib/libc/minimal/source/stdlib/strtol.c b/lib/libc/minimal/source/stdlib/strtoi.c similarity index 71% rename from lib/libc/minimal/source/stdlib/strtol.c rename to lib/libc/minimal/source/stdlib/strtoi.c index eceb3a88ef9bf..516e1c33eb1f8 100644 --- a/lib/libc/minimal/source/stdlib/strtol.c +++ b/lib/libc/minimal/source/stdlib/strtoi.c @@ -36,17 +36,29 @@ #include /* - * Convert a string to a long integer. + * @brief Convert a string to a long long integer, bounded by limit. + * + * This is not a standard library function, only a helper to support different + * target types. * * Ignores `locale' stuff. Assumes that the upper and lower case * alphabets and digits are each contiguous. + * + * @param nptr Pointer to the beginning of the string to convert + * @param endptr Pointer to store the first invalid character. May be null + * @param base Base of the conversion process + * @param issigned Defines if the target type will be signed or not + * @param limit Maximal positive value of the target type + * + * @retval A converted value, as unsigned long long. */ -long strtol(const char *nptr, char **endptr, register int base) +static unsigned long long strtoi(const char *nptr, char **endptr, + register int base, int issigned, unsigned long long limit) { register const char *s = nptr; - register unsigned long acc; + register unsigned long long acc; register int c; - register unsigned long cutoff; + register unsigned long long cutoff; register int neg = 0, any, cutlim; /* @@ -92,8 +104,8 @@ long strtol(const char *nptr, char **endptr, register int base) * Set any if any `digits' consumed; make it negative to indicate * overflow. */ - cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; - cutlim = cutoff % (unsigned long)base; + cutoff = neg && issigned ? (unsigned long long)(limit + 1) : limit; + cutlim = cutoff % (unsigned long long)base; cutoff /= (unsigned long)base; for (acc = 0, any = 0;; c = *s++) { if (isdigit(c)) { @@ -116,7 +128,7 @@ long strtol(const char *nptr, char **endptr, register int base) } if (any < 0) { - acc = neg ? LONG_MIN : LONG_MAX; + acc = neg && issigned ? -limit - 1LL : limit; errno = ERANGE; } else if (neg) { acc = -acc; @@ -127,3 +139,28 @@ long strtol(const char *nptr, char **endptr, register int base) } return acc; } + +/* Convert a string to an unsigned long integer. */ +unsigned long strtoul(const char *nptr, char **endptr, register int base) +{ + return (unsigned long)strtoi(nptr, endptr, base, 0, ULONG_MAX); +} + +/* Convert a string to a long integer. */ +long strtol(const char *nptr, char **endptr, register int base) +{ + return (long)strtoi(nptr, endptr, base, 1, LONG_MAX); +} + +/* Convert a string to an unsigned long long integer. */ +unsigned long long strtoull(const char *nptr, char **endptr, register int base) +{ + return (unsigned long long)strtoi(nptr, endptr, base, 0, ULLONG_MAX); +} + +/* Convert a string to a long long integer. */ +long long strtoll(const char *nptr, char **endptr, register int base) +{ + return (long long)strtoi(nptr, endptr, base, 1, LLONG_MAX); +} + diff --git a/lib/libc/minimal/source/stdlib/strtoul.c b/lib/libc/minimal/source/stdlib/strtoul.c deleted file mode 100644 index 188390e376535..0000000000000 --- a/lib/libc/minimal/source/stdlib/strtoul.c +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#include -#include -#include -#include - -/* - * Convert a string to an unsigned long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -unsigned long strtoul(const char *nptr, char **endptr, register int base) -{ - register const char *s = nptr; - register unsigned long acc; - register int c; - register unsigned long cutoff; - register int neg = 0, any, cutlim; - - /* - * See strtol for comments as to the logic used. - */ - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else 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 = (unsigned long)ULONG_MAX / (unsigned long)base; - cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; - for (acc = 0, any = 0;; c = *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 || acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - } else { - any = 1; - acc *= base; - acc += c; - } - } - if (any < 0) { - acc = ULONG_MAX; - errno = ERANGE; - } else if (neg) { - acc = -acc; - } - if (endptr != NULL) { - *endptr = (char *)(any ? s - 1 : nptr); - } - return acc; -} diff --git a/samples/net/sockets/civetweb/src/libc_extensions.c b/samples/net/sockets/civetweb/src/libc_extensions.c index a190b8e2b8609..4808427221c0b 100644 --- a/samples/net/sockets/civetweb/src/libc_extensions.c +++ b/samples/net/sockets/civetweb/src/libc_extensions.c @@ -150,12 +150,6 @@ double atof(const char *str) return (double)atoi(str); } -long long int strtoll(const char *str, char **endptr, int base) -{ - /* XXX good enough for civetweb uses */ - return (long long int)strtol(str, endptr, base); -} - time_t time(time_t *t) { return 0; diff --git a/samples/net/sockets/civetweb/src/libc_extensions.h b/samples/net/sockets/civetweb/src/libc_extensions.h index 75f29c514bb74..25ea018c01422 100644 --- a/samples/net/sockets/civetweb/src/libc_extensions.h +++ b/samples/net/sockets/civetweb/src/libc_extensions.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -26,10 +27,8 @@ size_t strspn(const char *s1, const char *s2); int iscntrl(int c); double atof(const char *str); -long long int strtoll(const char *str, char **endptr, int base); int sscanf(const char *s, const char *format, ...); char *strerror(int err); -unsigned long long int strtoull(const char *str, char **endptr, int base); time_t time(time_t *t); struct tm *gmtime(const time_t *ptime);