aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBryan O'Donoghue <bryan.odonoghue@linaro.org>2018-11-09 16:12:23 +0000
committerJérôme Forissier <jerome.forissier@linaro.org>2018-11-13 09:30:53 +0100
commitb38854bd5efa2eeef5945fa1af011e36103c2222 (patch)
treeeb26635ddf315aa7c5a5c8845e7adccce82457f3 /lib
parent9fdd6c3cb05fa98a3dcec403e73bda98ca8ab9fa (diff)
libutils: Import strtoul from newlib
This patch imports strtoul from newlib which the latest version of libfdt depends on. Some modification of the original source is required to do this, specifically: This is an import of the newlib 1.19.0 version of strtoul dropping - Headers and prototypes for re-entrancy - Any reliance on errno Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/libutils/isoc/include/stdlib.h3
-rw-r--r--lib/libutils/isoc/newlib/strtoul.c231
-rw-r--r--lib/libutils/isoc/newlib/sub.mk1
3 files changed, 235 insertions, 0 deletions
diff --git a/lib/libutils/isoc/include/stdlib.h b/lib/libutils/isoc/include/stdlib.h
index bae45bab..d2b62df1 100644
--- a/lib/libutils/isoc/include/stdlib.h
+++ b/lib/libutils/isoc/include/stdlib.h
@@ -26,4 +26,7 @@ int abs(int i);
#define RAND_MAX __INT_MAX__
int rand(void);
+unsigned long _strtoul (const char *nptr, char **endptr, int base);
+unsigned long strtoul (const char *s, char **ptr, int base);
+
#endif /* STDLIB_H */
diff --git a/lib/libutils/isoc/newlib/strtoul.c b/lib/libutils/isoc/newlib/strtoul.c
new file mode 100644
index 00000000..c1c096c3
--- /dev/null
+++ b/lib/libutils/isoc/newlib/strtoul.c
@@ -0,0 +1,231 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 1994-2009 Red Hat, Inc.
+ * 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. Neither the name of the copyright holder 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+ */
+
+/*
+FUNCTION
+ <<strtoul>>---string to unsigned long
+
+INDEX
+ strtoul
+INDEX
+ _strtoul_r
+
+ANSI_SYNOPSIS
+ #include <stdlib.h>
+ unsigned long strtoul(const char *<[s]>, char **<[ptr]>,
+ int <[base]>);
+
+ unsigned long _strtoul(const char *<[s]>,
+ char **<[ptr]>, int <[base]>);
+
+TRAD_SYNOPSIS
+ #include <stdlib.h>
+ unsigned long strtoul(<[s]>, <[ptr]>, <[base]>)
+ char *<[s]>;
+ char **<[ptr]>;
+ int <[base]>;
+
+ unsigned long _strtoul(<[s]>, <[ptr]>, <[base]>)
+ char *<[s]>;
+ char **<[ptr]>;
+ int <[base]>;
+
+DESCRIPTION
+The function <<strtoul>> converts the string <<*<[s]>>> to
+an <<unsigned long>>. First, it breaks down the string into three parts:
+leading whitespace, which is ignored; a subject string consisting
+of the digits meaningful in the radix specified by <[base]>
+(for example, <<0>> through <<7>> if the value of <[base]> is 8);
+and a trailing portion consisting of one or more unparseable characters,
+which always includes the terminating null character. Then, it attempts
+to convert the subject string into an unsigned long integer, and returns the
+result.
+
+If the value of <[base]> is zero, the subject string is expected to look
+like a normal C integer constant (save that no optional sign is permitted):
+a possible <<0x>> indicating hexadecimal radix, and a number.
+If <[base]> is between 2 and 36, the expected form of the subject is a
+sequence of digits (which may include letters, depending on the
+base) representing an integer in the radix specified by <[base]>.
+The letters <<a>>--<<z>> (or <<A>>--<<Z>>) are used as digits valued from
+10 to 35. If <[base]> is 16, a leading <<0x>> is permitted.
+
+The subject sequence is the longest initial sequence of the input
+string that has the expected form, starting with the first
+non-whitespace character. If the string is empty or consists entirely
+of whitespace, or if the first non-whitespace character is not a
+permissible digit, the subject string is empty.
+
+If the subject string is acceptable, and the value of <[base]> is zero,
+<<strtoul>> attempts to determine the radix from the input string. A
+string with a leading <<0x>> is treated as a hexadecimal value; a string with
+a leading <<0>> and no <<x>> is treated as octal; all other strings are
+treated as decimal. If <[base]> is between 2 and 36, it is used as the
+conversion radix, as described above. Finally, a pointer to the first
+character past the converted subject string is stored in <[ptr]>, if
+<[ptr]> is not <<NULL>>.
+
+If the subject string is empty (that is, if <<*>><[s]> does not start
+with a substring in acceptable form), no conversion
+is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
+not <<NULL>>).
+
+
+
+RETURNS
+<<strtoul>> returns the converted value, if any. If no conversion was
+made, <<0>> is returned.
+
+<<strtoul>> returns <<ULONG_MAX>> if the magnitude of the converted
+value is too large, and sets <<errno>> to <<ERANGE>>.
+
+PORTABILITY
+<<strtoul>> is ANSI.
+
+<<strtoul>> requires no supporting OS subroutines.
+*/
+
+/*
+ * Copyright (c) 1990 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 "_ansi.h"
+#include <limits.h>
+#include <ctype.h>
+#include <stdlib.h>
+
+/*
+ * 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
+_DEFUN (_strtoul, (nptr, endptr, base),
+ _CONST char *nptr _AND
+ char **endptr _AND
+ int base)
+{
+ register const unsigned char *s = (const unsigned char *)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;
+ } else if (neg)
+ acc = -acc;
+ if (endptr != 0)
+ *endptr = (char *) (any ? (char *)s - 1 : nptr);
+ return (acc);
+}
+
+#ifndef _REENT_ONLY
+
+unsigned long
+_DEFUN (strtoul, (s, ptr, base),
+ _CONST char *s _AND
+ char **ptr _AND
+ int base)
+{
+ return _strtoul (s, ptr, base);
+}
+
+#endif
diff --git a/lib/libutils/isoc/newlib/sub.mk b/lib/libutils/isoc/newlib/sub.mk
index dffbda14..6736cedf 100644
--- a/lib/libutils/isoc/newlib/sub.mk
+++ b/lib/libutils/isoc/newlib/sub.mk
@@ -18,3 +18,4 @@ srcs-y += strstr.c
srcs-y += strcpy.c
srcs-y += strncpy.c
srcs-y += strrchr.c
+srcs-y += strtoul.c