Skip to content

Commit 4fd24a4

Browse files
MeisterBobcarlescufi
authored andcommitted
libc: minimal: Add strtoll() and strtoull()
- strtoll() and strtoull() are copies of strtol() and strtoul() with types changed to long long instead of long. - added tests - added documentation - removed stubs from civetweb sample Signed-off-by: Gerhard Jörges <[email protected]>
1 parent 869ae2b commit 4fd24a4

File tree

8 files changed

+383
-10
lines changed

8 files changed

+383
-10
lines changed

doc/guides/portability/posix.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ This is implemented as part of the minimal C library available in Zephyr.
308308
strtok_r(),+
309309
strtol(),+
310310
strtold(),
311-
strtoll(),
311+
strtoll(),+
312312
strtoul(),+
313-
strtoull(),
313+
strtoull(),+
314314
strtoumax(),
315315
strxfrm(),
316316
time(),+

lib/libc/minimal/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ zephyr_library_sources(
88
source/stdlib/atoi.c
99
source/stdlib/strtol.c
1010
source/stdlib/strtoul.c
11+
source/stdlib/strtoll.c
12+
source/stdlib/strtoull.c
1113
source/stdlib/malloc.c
1214
source/stdlib/bsearch.c
1315
source/stdlib/exit.c

lib/libc/minimal/include/stdlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ extern "C" {
1818

1919
unsigned long strtoul(const char *nptr, char **endptr, int base);
2020
long strtol(const char *nptr, char **endptr, int base);
21+
unsigned long long strtoull(const char *nptr, char **endptr, int base);
22+
long long strtoll(const char *nptr, char **endptr, int base);
2123
int atoi(const char *s);
2224

2325
void *malloc(size_t size);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* SPDX-License-Identifier: BSD-4-Clause-UC */
2+
/*-
3+
* Copyright (c) 1990, 1993
4+
* The Regents of the University of California. All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* 3. All advertising materials mentioning features or use of this software
15+
* must display the following acknowledgment:
16+
* This product includes software developed by the University of
17+
* California, Berkeley and its contributors.
18+
* 4. Neither the name of the University nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
* SUCH DAMAGE.
33+
*/
34+
#include <limits.h>
35+
#include <ctype.h>
36+
#include <errno.h>
37+
#include <stdlib.h>
38+
39+
/*
40+
* Convert a string to a long long integer.
41+
*
42+
* Ignores `locale' stuff. Assumes that the upper and lower case
43+
* alphabets and digits are each contiguous.
44+
*/
45+
long long strtoll(const char *nptr, char **endptr, register int base)
46+
{
47+
register const char *s = nptr;
48+
register unsigned long long acc;
49+
register int c;
50+
register unsigned long long cutoff;
51+
register int neg = 0, any, cutlim;
52+
53+
/*
54+
* See strtol for comments as to the logic used.
55+
*/
56+
do {
57+
c = *s++;
58+
} while (isspace(c));
59+
if (c == '-') {
60+
neg = 1;
61+
c = *s++;
62+
} else if (c == '+') {
63+
c = *s++;
64+
}
65+
66+
if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
67+
c = s[1];
68+
s += 2;
69+
base = 16;
70+
}
71+
72+
if (base == 0) {
73+
base = c == '0' ? 8 : 10;
74+
}
75+
76+
cutoff = neg ? -(unsigned long long)LLONG_MIN : LLONG_MAX;
77+
cutlim = cutoff % (unsigned long long)base;
78+
cutoff /= (unsigned long long)base;
79+
for (acc = 0, any = 0;; c = *s++) {
80+
if (isdigit(c)) {
81+
c -= '0';
82+
} else if (isalpha(c)) {
83+
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
84+
} else {
85+
break;
86+
}
87+
if (c >= base) {
88+
break;
89+
}
90+
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
91+
any = -1;
92+
} else {
93+
any = 1;
94+
acc *= base;
95+
acc += c;
96+
}
97+
}
98+
99+
if (any < 0) {
100+
acc = neg ? LLONG_MIN : LLONG_MAX;
101+
errno = ERANGE;
102+
} else if (neg) {
103+
acc = -acc;
104+
}
105+
106+
if (endptr != NULL) {
107+
*endptr = (char *)(any ? s - 1 : nptr);
108+
}
109+
return acc;
110+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* SPDX-License-Identifier: BSD-4-Clause-UC */
2+
3+
/*
4+
* Copyright (c) 1990, 1993
5+
* The Regents of the University of California. All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* 3. All advertising materials mentioning features or use of this software
16+
* must display the following acknowledgment:
17+
* This product includes software developed by the University of
18+
* California, Berkeley and its contributors.
19+
* 4. Neither the name of the University nor the names of its contributors
20+
* may be used to endorse or promote products derived from this software
21+
* without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33+
* SUCH DAMAGE.
34+
*/
35+
#include <limits.h>
36+
#include <ctype.h>
37+
#include <errno.h>
38+
#include <stdlib.h>
39+
40+
/*
41+
* Convert a string to an unsigned long long integer.
42+
*
43+
* Ignores `locale' stuff. Assumes that the upper and lower case
44+
* alphabets and digits are each contiguous.
45+
*/
46+
unsigned long long strtoull(const char *nptr, char **endptr, register int base)
47+
{
48+
register const char *s = nptr;
49+
register unsigned long long acc;
50+
register int c;
51+
register unsigned long long cutoff;
52+
register int neg = 0, any, cutlim;
53+
54+
/*
55+
* See strtol for comments as to the logic used.
56+
*/
57+
do {
58+
c = *s++;
59+
} while (isspace(c));
60+
if (c == '-') {
61+
neg = 1;
62+
c = *s++;
63+
} else if (c == '+') {
64+
c = *s++;
65+
}
66+
67+
if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
68+
c = s[1];
69+
s += 2;
70+
base = 16;
71+
}
72+
73+
if (base == 0) {
74+
base = c == '0' ? 8 : 10;
75+
}
76+
77+
cutoff = (unsigned long long)ULLONG_MAX / (unsigned long long)base;
78+
cutlim = (unsigned long long)ULLONG_MAX % (unsigned long long)base;
79+
for (acc = 0, any = 0;; c = *s++) {
80+
if (isdigit(c)) {
81+
c -= '0';
82+
} else if (isalpha(c)) {
83+
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
84+
} else {
85+
break;
86+
}
87+
if (c >= base) {
88+
break;
89+
}
90+
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
91+
any = -1;
92+
} else {
93+
any = 1;
94+
acc *= base;
95+
acc += c;
96+
}
97+
}
98+
if (any < 0) {
99+
acc = ULLONG_MAX;
100+
errno = ERANGE;
101+
} else if (neg) {
102+
acc = -acc;
103+
}
104+
if (endptr != NULL) {
105+
*endptr = (char *)(any ? s - 1 : nptr);
106+
}
107+
return acc;
108+
}

samples/net/civetweb/common/include/libc_extensions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ size_t strspn(const char *s1, const char *s2);
2626
int iscntrl(int c);
2727

2828
double atof(const char *str);
29-
long long strtoll(const char *str, char **endptr, int base);
3029
int sscanf(const char *s, const char *format, ...);
3130
char *strerror(int err);
32-
unsigned long long strtoull(const char *str, char **endptr, int base);
3331

3432
time_t time(time_t *t);
3533
struct tm *gmtime(const time_t *ptime);

samples/net/civetweb/common/src/libc_extensions.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ double atof(const char *str)
153153
return (double)atoi(str);
154154
}
155155

156-
long long strtoll(const char *str, char **endptr, int base)
157-
{
158-
/* XXX good enough for civetweb uses */
159-
return (long long)strtol(str, endptr, base);
160-
}
161-
162156
/*
163157
* Most of the wrappers below are copies of the wrappers in net/sockets.h,
164158
* but they are available only if CONFIG_NET_SOCKETS_POSIX_NAMES is enabled

0 commit comments

Comments
 (0)