summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/strtoul.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.3')
-rw-r--r--src/lib/libc/stdlib/strtoul.3176
1 files changed, 130 insertions, 46 deletions
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3
index db551b0141..a7c6bbfa18 100644
--- a/src/lib/libc/stdlib/strtoul.3
+++ b/src/lib/libc/stdlib/strtoul.3
@@ -13,11 +13,7 @@
13.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution. 15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software 16.\" 3. Neither the name of the University nor the names of its contributors
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software 17.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission. 18.\" without specific prior written permission.
23.\" 19.\"
@@ -33,49 +29,73 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
35.\" 31.\"
36.\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 32.\" $OpenBSD: strtoul.3,v 1.19 2007/11/13 18:30:04 tobias Exp $
37.\" $Id: strtoul.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
38.\" 33.\"
39.Dd June 25, 1992 34.Dd $Mdocdate: November 13 2007 $
40.Dt STRTOUL 3 35.Dt STRTOUL 3
41.Os 36.Os
42.Sh NAME 37.Sh NAME
43.Nm strtoul, strtouq 38.Nm strtoul ,
44.Nd convert a string to an unsigned long or uquad_t integer 39.Nm strtoull ,
40.Nm strtoumax ,
41.Nm strtouq
42.Nd "convert a string to an unsigned long, unsigned long long or uintmax_t integer"
45.Sh SYNOPSIS 43.Sh SYNOPSIS
46.Fd #include <stdlib.h>
47.Fd #include <limits.h> 44.Fd #include <limits.h>
45.Fd #include <stdlib.h>
48.Ft unsigned long 46.Ft unsigned long
49.Fn strtoul "const char *nptr" "char **endptr" "int base" 47.Fn strtoul "const char *nptr" "char **endptr" "int base"
50 48.Pp
49.Ft unsigned long long
50.Fn strtoull "const char *nptr" "char **endptr" "int base"
51.Pp
52.Fd #include <inttypes.h>
53.Ft uintmax_t
54.Fn strtoumax "const char *nptr" "char **endptr" "int base"
55.Pp
51.Fd #include <sys/types.h> 56.Fd #include <sys/types.h>
52.Fd #include <stdlib.h>
53.Fd #include <limits.h> 57.Fd #include <limits.h>
58.Fd #include <stdlib.h>
54.Ft u_quad_t 59.Ft u_quad_t
55.Fn strtouq "const char *nptr" "char **endptr" "int base" 60.Fn strtouq "const char *nptr" "char **endptr" "int base"
56.Sh DESCRIPTION 61.Sh DESCRIPTION
57The 62The
58.Fn strtoul 63.Fn strtoul
59function 64function converts the string in
60converts the string in
61.Fa nptr 65.Fa nptr
62to an 66to an
63.Em unsigned long 67.Li unsigned long
64value. 68value.
65The 69The
66.Fn strtouq 70.Fn strtoull
67function 71function converts the string in
68converts the string in 72.Fa nptr
73to an
74.Li unsigned long long
75value.
76The
77.Fn strtoumax
78function converts the string in
69.Fa nptr 79.Fa nptr
70to a 80to a
71.Em u_quad_t 81.Li umaxint_t
72value. 82value.
83The
84.Fn strtouq
85function is a deprecated equivalent of
86.Fn strtoull
87and is provided for backwards compatibility with legacy programs.
73The conversion is done according to the given 88The conversion is done according to the given
74.Fa base , 89.Fa base ,
75which must be between 2 and 36 inclusive, 90which must be a number between 2 and 36 inclusive
76or be the special value 0. 91or the special value 0.
92If the string in
93.Fa nptr
94represents a negative number, it will be converted to its unsigned equivalent.
95This behavior is consistent with what happens when a signed integer type is
96cast to its unsigned counterpart.
77.Pp 97.Pp
78The string may begin with an arbitrary amount of white space 98The string may begin with an arbitrary amount of whitespace
79(as determined by 99(as determined by
80.Xr isspace 3 ) 100.Xr isspace 3 )
81followed by a single optional 101followed by a single optional
@@ -85,26 +105,22 @@ or
85sign. 105sign.
86If 106If
87.Fa base 107.Fa base
88is zero or 16, 108is zero or 16, the string may then include a
89the string may then include a
90.Ql 0x 109.Ql 0x
91prefix, 110prefix, and the number will be read in base 16; otherwise, a zero
92and the number will be read in base 16; otherwise, a zero
93.Fa base 111.Fa base
94is taken as 10 (decimal) unless the next character is 112is taken as 10 (decimal) unless the next character is
95.Ql 0 , 113.Ql 0 ,
96in which case it is taken as 8 (octal). 114in which case it is taken as 8 (octal).
97.Pp 115.Pp
98The remainder of the string is converted to an 116The remainder of the string is converted to an
99.Em unsigned long 117.Li unsigned long
100value in the obvious manner, 118value in the obvious manner, stopping at the end of the string
101stopping at the end of the string
102or at the first character that does not produce a valid digit 119or at the first character that does not produce a valid digit
103in the given base. 120in the given base.
104(In bases above 10, the letter 121(In bases above 10, the letter
105.Ql A 122.Ql A
106in either upper or lower case 123in either upper or lower case represents 10,
107represents 10,
108.Ql B 124.Ql B
109represents 11, and so forth, with 125represents 11, and so forth, with
110.Ql Z 126.Ql Z
@@ -112,7 +128,7 @@ representing 35.)
112.Pp 128.Pp
113If 129If
114.Fa endptr 130.Fa endptr
115is non nil, 131is non-null,
116.Fn strtoul 132.Fn strtoul
117stores the address of the first invalid character in 133stores the address of the first invalid character in
118.Fa *endptr . 134.Fa *endptr .
@@ -133,32 +149,100 @@ is
133on return, the entire string was valid.) 149on return, the entire string was valid.)
134.Sh RETURN VALUES 150.Sh RETURN VALUES
135The 151The
136.Fn strtoul 152.Fn strtoul ,
137function 153.Fn strtoull ,
138returns either the result of the conversion 154.Fn strtoumax
139or, if there was a leading minus sign, 155and
156.Fn strtouq
157functions return either the result of the conversion or,
158if there was a leading minus sign,
140the negation of the result of the conversion, 159the negation of the result of the conversion,
141unless the original (non-negated) value would overflow; 160unless the original (non-negated) value would overflow.
142in the latter case, 161If overflow occurs,
143.Fn strtoul 162.Fn strtoul
144returns 163returns
145.Dv ULONG_MAX 164.Dv ULONG_MAX ,
146and sets the global variable 165.Fn strtoull
166returns
167.Dv ULLONG_MAX ,
168.Fn strtoumax
169returns
170.Dv UINTMAX_MAX ,
171.Fn strtouq
172returns
173.Dv ULLONG_MAX
174and the global variable
147.Va errno 175.Va errno
148to 176is set to
149.Er ERANGE . 177.Er ERANGE .
178If no conversion could be performed, 0 is returned;
179the global variable
180.Va errno
181is also set to
182.Er EINVAL,
183though this is not portable across all platforms.
184.Pp
185There is no way to determine if
186.Fn strtoul
187has processed a negative number (and returned an unsigned value) short of
188examining the string in
189.Fa nptr
190directly.
191.Sh EXAMPLES
192Ensuring that a string is a valid number (i.e., in range and containing no
193trailing characters) requires clearing
194.Va errno
195beforehand explicitly since
196.Va errno
197is not changed on a successful call to
198.Fn strtoul ,
199and the return value of
200.Fn strtoul
201cannot be used unambiguously to signal an error:
202.Bd -literal -offset indent
203char *ep;
204unsigned long ulval;
205
206\&...
207
208errno = 0;
209ulval = strtoul(buf, &ep, 10);
210if (buf[0] == '\e0' || *ep != '\e0')
211 goto not_a_number;
212if (errno == ERANGE && ulval == ULONG_MAX)
213 goto out_of_range;
214.Ed
215.Pp
216This example will accept
217.Dq 12
218but not
219.Dq 12foo
220or
221.Dq 12\en .
222If trailing whitespace is acceptable, further checks must be done on
223.Va *ep ;
224alternately, use
225.Xr sscanf 3 .
150.Sh ERRORS 226.Sh ERRORS
151.Bl -tag -width Er 227.Bl -tag -width Er
152.It Bq Er ERANGE 228.It Bq Er ERANGE
153The given string was out of range; the value converted has been clamped. 229The given string was out of range; the value converted has been clamped.
154.El 230.El
155.Sh SEE ALSO 231.Sh SEE ALSO
232.Xr sscanf 3 ,
156.Xr strtol 3 233.Xr strtol 3
157.Sh STANDARDS 234.Sh STANDARDS
158The 235The
159.Fn strtoul 236.Fn strtoul ,
160function 237.Fn strtoull ,
161conforms to 238and
162.St -ansiC . 239.Fn strtoumax
240functions conform to
241.St -ansiC-99 .
242The
243.Fn strtouq
244function is a
245.Bx
246extension and is provided for backwards compatibility with legacy programs.
163.Sh BUGS 247.Sh BUGS
164Ignores the current locale. 248Ignores the current locale.