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.3173
1 files changed, 127 insertions, 46 deletions
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3
index db551b0141..5e86772ef0 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,70 @@
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.20 2010/04/07 18:32:53 jmc 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: April 7 2010 $
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.Ft unsigned long long
49.Fn strtoull "const char *nptr" "char **endptr" "int base"
50.Fd #include <inttypes.h>
51.Ft uintmax_t
52.Fn strtoumax "const char *nptr" "char **endptr" "int base"
51.Fd #include <sys/types.h> 53.Fd #include <sys/types.h>
52.Fd #include <stdlib.h>
53.Fd #include <limits.h> 54.Fd #include <limits.h>
55.Fd #include <stdlib.h>
54.Ft u_quad_t 56.Ft u_quad_t
55.Fn strtouq "const char *nptr" "char **endptr" "int base" 57.Fn strtouq "const char *nptr" "char **endptr" "int base"
56.Sh DESCRIPTION 58.Sh DESCRIPTION
57The 59The
58.Fn strtoul 60.Fn strtoul
59function 61function converts the string in
60converts the string in
61.Fa nptr 62.Fa nptr
62to an 63to an
63.Em unsigned long 64.Li unsigned long
64value. 65value.
65The 66The
66.Fn strtouq 67.Fn strtoull
67function 68function converts the string in
68converts the string in 69.Fa nptr
70to an
71.Li unsigned long long
72value.
73The
74.Fn strtoumax
75function converts the string in
69.Fa nptr 76.Fa nptr
70to a 77to a
71.Em u_quad_t 78.Li umaxint_t
72value. 79value.
80The
81.Fn strtouq
82function is a deprecated equivalent of
83.Fn strtoull
84and is provided for backwards compatibility with legacy programs.
73The conversion is done according to the given 85The conversion is done according to the given
74.Fa base , 86.Fa base ,
75which must be between 2 and 36 inclusive, 87which must be a number between 2 and 36 inclusive
76or be the special value 0. 88or the special value 0.
89If the string in
90.Fa nptr
91represents a negative number, it will be converted to its unsigned equivalent.
92This behavior is consistent with what happens when a signed integer type is
93cast to its unsigned counterpart.
77.Pp 94.Pp
78The string may begin with an arbitrary amount of white space 95The string may begin with an arbitrary amount of whitespace
79(as determined by 96(as determined by
80.Xr isspace 3 ) 97.Xr isspace 3 )
81followed by a single optional 98followed by a single optional
@@ -85,26 +102,22 @@ or
85sign. 102sign.
86If 103If
87.Fa base 104.Fa base
88is zero or 16, 105is zero or 16, the string may then include a
89the string may then include a
90.Ql 0x 106.Ql 0x
91prefix, 107prefix, 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 108.Fa base
94is taken as 10 (decimal) unless the next character is 109is taken as 10 (decimal) unless the next character is
95.Ql 0 , 110.Ql 0 ,
96in which case it is taken as 8 (octal). 111in which case it is taken as 8 (octal).
97.Pp 112.Pp
98The remainder of the string is converted to an 113The remainder of the string is converted to an
99.Em unsigned long 114.Li unsigned long
100value in the obvious manner, 115value 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 116or at the first character that does not produce a valid digit
103in the given base. 117in the given base.
104(In bases above 10, the letter 118(In bases above 10, the letter
105.Ql A 119.Ql A
106in either upper or lower case 120in either upper or lower case represents 10,
107represents 10,
108.Ql B 121.Ql B
109represents 11, and so forth, with 122represents 11, and so forth, with
110.Ql Z 123.Ql Z
@@ -112,7 +125,7 @@ representing 35.)
112.Pp 125.Pp
113If 126If
114.Fa endptr 127.Fa endptr
115is non nil, 128is non-null,
116.Fn strtoul 129.Fn strtoul
117stores the address of the first invalid character in 130stores the address of the first invalid character in
118.Fa *endptr . 131.Fa *endptr .
@@ -133,32 +146,100 @@ is
133on return, the entire string was valid.) 146on return, the entire string was valid.)
134.Sh RETURN VALUES 147.Sh RETURN VALUES
135The 148The
136.Fn strtoul 149.Fn strtoul ,
137function 150.Fn strtoull ,
138returns either the result of the conversion 151.Fn strtoumax
139or, if there was a leading minus sign, 152and
153.Fn strtouq
154functions return either the result of the conversion or,
155if there was a leading minus sign,
140the negation of the result of the conversion, 156the negation of the result of the conversion,
141unless the original (non-negated) value would overflow; 157unless the original (non-negated) value would overflow.
142in the latter case, 158If overflow occurs,
143.Fn strtoul 159.Fn strtoul
144returns 160returns
145.Dv ULONG_MAX 161.Dv ULONG_MAX ,
146and sets the global variable 162.Fn strtoull
163returns
164.Dv ULLONG_MAX ,
165.Fn strtoumax
166returns
167.Dv UINTMAX_MAX ,
168.Fn strtouq
169returns
170.Dv ULLONG_MAX
171and the global variable
147.Va errno 172.Va errno
148to 173is set to
149.Er ERANGE . 174.Er ERANGE .
175If no conversion could be performed, 0 is returned;
176the global variable
177.Va errno
178is also set to
179.Er EINVAL ,
180though this is not portable across all platforms.
181.Pp
182There is no way to determine if
183.Fn strtoul
184has processed a negative number (and returned an unsigned value) short of
185examining the string in
186.Fa nptr
187directly.
188.Sh EXAMPLES
189Ensuring that a string is a valid number (i.e., in range and containing no
190trailing characters) requires clearing
191.Va errno
192beforehand explicitly since
193.Va errno
194is not changed on a successful call to
195.Fn strtoul ,
196and the return value of
197.Fn strtoul
198cannot be used unambiguously to signal an error:
199.Bd -literal -offset indent
200char *ep;
201unsigned long ulval;
202
203\&...
204
205errno = 0;
206ulval = strtoul(buf, &ep, 10);
207if (buf[0] == '\e0' || *ep != '\e0')
208 goto not_a_number;
209if (errno == ERANGE && ulval == ULONG_MAX)
210 goto out_of_range;
211.Ed
212.Pp
213This example will accept
214.Dq 12
215but not
216.Dq 12foo
217or
218.Dq 12\en .
219If trailing whitespace is acceptable, further checks must be done on
220.Va *ep ;
221alternately, use
222.Xr sscanf 3 .
150.Sh ERRORS 223.Sh ERRORS
151.Bl -tag -width Er 224.Bl -tag -width Er
152.It Bq Er ERANGE 225.It Bq Er ERANGE
153The given string was out of range; the value converted has been clamped. 226The given string was out of range; the value converted has been clamped.
154.El 227.El
155.Sh SEE ALSO 228.Sh SEE ALSO
229.Xr sscanf 3 ,
156.Xr strtol 3 230.Xr strtol 3
157.Sh STANDARDS 231.Sh STANDARDS
158The 232The
159.Fn strtoul 233.Fn strtoul ,
160function 234.Fn strtoull ,
161conforms to 235and
162.St -ansiC . 236.Fn strtoumax
237functions conform to
238.St -ansiC-99 .
239The
240.Fn strtouq
241function is a
242.Bx
243extension and is provided for backwards compatibility with legacy programs.
163.Sh BUGS 244.Sh BUGS
164Ignores the current locale. 245Ignores the current locale.