3 <<strtoul>>, <<strtoul_l>>---string to unsigned long
16 unsigned long strtoul(const char *restrict <[s]>,
17 char **restrict <[ptr]>, int <[base]>);
20 unsigned long strtoul_l(const char *restrict <[s]>,
21 char **restrict <[ptr]>, int <[base]>,
24 unsigned long _strtoul_r(void *<[reent]>, const char *restrict <[s]>,
25 char **restrict <[ptr]>, int <[base]>);
28 The function <<strtoul>> converts the string <<*<[s]>>> to
29 an <<unsigned long>>. First, it breaks down the string into three parts:
30 leading whitespace, which is ignored; a subject string consisting
31 of the digits meaningful in the radix specified by <[base]>
32 (for example, <<0>> through <<7>> if the value of <[base]> is 8);
33 and a trailing portion consisting of one or more unparseable characters,
34 which always includes the terminating null character. Then, it attempts
35 to convert the subject string into an unsigned long integer, and returns the
38 If the value of <[base]> is zero, the subject string is expected to look
39 like a normal C integer constant (save that no optional sign is permitted):
40 a possible <<0x>> indicating hexadecimal radix, and a number.
41 If <[base]> is between 2 and 36, the expected form of the subject is a
42 sequence of digits (which may include letters, depending on the
43 base) representing an integer in the radix specified by <[base]>.
44 The letters <<a>>--<<z>> (or <<A>>--<<Z>>) are used as digits valued from
45 10 to 35. If <[base]> is 16, a leading <<0x>> is permitted.
47 The subject sequence is the longest initial sequence of the input
48 string that has the expected form, starting with the first
49 non-whitespace character. If the string is empty or consists entirely
50 of whitespace, or if the first non-whitespace character is not a
51 permissible digit, the subject string is empty.
53 If the subject string is acceptable, and the value of <[base]> is zero,
54 <<strtoul>> attempts to determine the radix from the input string. A
55 string with a leading <<0x>> is treated as a hexadecimal value; a string with
56 a leading <<0>> and no <<x>> is treated as octal; all other strings are
57 treated as decimal. If <[base]> is between 2 and 36, it is used as the
58 conversion radix, as described above. Finally, a pointer to the first
59 character past the converted subject string is stored in <[ptr]>, if
60 <[ptr]> is not <<NULL>>.
62 If the subject string is empty (that is, if <<*>><[s]> does not start
63 with a substring in acceptable form), no conversion
64 is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
67 <<strtoul_l>> is like <<strtoul>> but performs the conversion based on the
68 locale specified by the locale object locale. If <[locale]> is
69 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
71 The alternate function <<_strtoul_r>> is a reentrant version. The
72 extra argument <[reent]> is a pointer to a reentrancy structure.
75 <<strtoul>>, <<strtoul_l>> return the converted value, if any. If no
76 conversion was made, <<0>> is returned.
78 <<strtoul>>, <<strtoul_l>> return <<ULONG_MAX>> if the magnitude of the
79 converted value is too large, and sets <<errno>> to <<ERANGE>>.
83 <<strtoul_l>> is a GNU extension.
85 <<strtoul>> requires no supporting OS subroutines.
89 * Copyright (c) 1990 Regents of the University of California.
90 * All rights reserved.
92 * Redistribution and use in source and binary forms, with or without
93 * modification, are permitted provided that the following conditions
95 * 1. Redistributions of source code must retain the above copyright
96 * notice, this list of conditions and the following disclaimer.
97 * 2. Redistributions in binary form must reproduce the above copyright
98 * notice, this list of conditions and the following disclaimer in the
99 * documentation and/or other materials provided with the distribution.
100 * 3. Neither the name of the University nor the names of its contributors
101 * may be used to endorse or promote products derived from this software
102 * without specific prior written permission.
104 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
105 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
107 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
108 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
109 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
110 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
111 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
112 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
113 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124 #include "../locale/setlocale.h"
127 * Convert a string to an unsigned long integer.
130 _strtoul_l (struct _reent
*rptr
, const char *__restrict nptr
,
131 char **__restrict endptr
, int base
, locale_t loc
)
133 register const unsigned char *s
= (const unsigned char *)nptr
;
134 register unsigned long acc
;
136 register unsigned long cutoff
;
137 register int neg
= 0, any
, cutlim
;
140 * See strtol for comments as to the logic used.
144 } while (isspace_l(c
, loc
));
150 if ((base
== 0 || base
== 16) &&
151 c
== '0' && (*s
== 'x' || *s
== 'X')) {
157 base
= c
== '0' ? 8 : 10;
158 cutoff
= (unsigned long)ULONG_MAX
/ (unsigned long)base
;
159 cutlim
= (unsigned long)ULONG_MAX
% (unsigned long)base
;
160 for (acc
= 0, any
= 0;; c
= *s
++) {
161 if (c
>= '0' && c
<= '9')
163 else if (c
>= 'A' && c
<= 'Z')
165 else if (c
>= 'a' && c
<= 'z')
171 if (any
< 0 || acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
181 _REENT_ERRNO(rptr
) = ERANGE
;
185 *endptr
= (char *) (any
? (char *)s
- 1 : nptr
);
190 _strtoul_r (struct _reent
*rptr
,
191 const char *__restrict nptr
,
192 char **__restrict endptr
,
195 return _strtoul_l (rptr
, nptr
, endptr
, base
, __get_current_locale ());
201 strtoul_l (const char *__restrict s
, char **__restrict ptr
, int base
,
204 return _strtoul_l (_REENT
, s
, ptr
, base
, loc
);
208 strtoul (const char *__restrict s
,
209 char **__restrict ptr
,
212 return _strtoul_l (_REENT
, s
, ptr
, base
, __get_current_locale ());