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.3225
1 files changed, 225 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3
new file mode 100644
index 0000000000..6d55de4d7a
--- /dev/null
+++ b/src/lib/libc/stdlib/strtoul.3
@@ -0,0 +1,225 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
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
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strtoul.3,v 1.10 2002/06/29 00:20:11 millert Exp $
37.\"
38.Dd June 25, 1992
39.Dt STRTOUL 3
40.Os
41.Sh NAME
42.Nm strtoul ,
43.Nm strtoull ,
44.Nm strtouq
45.Nd convert a string to an unsigned long or unsigned long long integer
46.Sh SYNOPSIS
47.Fd #include <stdlib.h>
48.Fd #include <limits.h>
49.Ft unsigned long
50.Fn strtoul "const char *nptr" "char **endptr" "int base"
51.Pp
52.Fd #include <stdlib.h>
53.Fd #include <limits.h>
54.Ft unsigned long long
55.Fn strtoull "const char *nptr" "char **endptr" "int base"
56.Pp
57.Fd #include <sys/types.h>
58.Fd #include <stdlib.h>
59.Fd #include <limits.h>
60.Ft u_quad_t
61.Fn strtouq "const char *nptr" "char **endptr" "int base"
62.Sh DESCRIPTION
63The
64.Fn strtoul
65function converts the string in
66.Fa nptr
67to an
68.Li unsigned long
69value.
70The
71.Fn strtoull
72function converts the string in
73.Fa nptr
74to an
75.Li unsigned long long
76value.
77The
78.Fn strtouq
79function is a deprecated equivalent of
80.Fn strtoull
81and is provided for backwards compatibility with legacy programs.
82The conversion is done according to the given
83.Fa base ,
84which must be a number between 2 and 36 inclusive
85or the special value 0.
86.Pp
87The string may begin with an arbitrary amount of whitespace
88(as determined by
89.Xr isspace 3 )
90followed by a single optional
91.Ql +
92or
93.Ql -
94sign.
95If
96.Fa base
97is zero or 16, the string may then include a
98.Ql 0x
99prefix, and the number will be read in base 16; otherwise, a zero
100.Fa base
101is taken as 10 (decimal) unless the next character is
102.Ql 0 ,
103in which case it is taken as 8 (octal).
104.Pp
105The remainder of the string is converted to an
106.Li unsigned long
107value in the obvious manner, stopping at the end of the string
108or at the first character that does not produce a valid digit
109in the given base.
110(In bases above 10, the letter
111.Ql A
112in either upper or lower case represents 10,
113.Ql B
114represents 11, and so forth, with
115.Ql Z
116representing 35.)
117.Pp
118If
119.Fa endptr
120is non-null,
121.Fn strtoul
122stores the address of the first invalid character in
123.Fa *endptr .
124If there were no digits at all, however,
125.Fn strtoul
126stores the original value of
127.Fa nptr
128in
129.Fa *endptr .
130(Thus, if
131.Fa *nptr
132is not
133.Ql \e0
134but
135.Fa **endptr
136is
137.Ql \e0
138on return, the entire string was valid.)
139.Sh RETURN VALUES
140The
141.Fn strtoul
142function returns the result of the conversion,
143unless the value would overflow, in which case
144.Dv ULONG_MAX
145is returned and
146.Va errno
147is set to
148.Er ERANGE .
149If there was a leading minus sign,
150.Fn strtoul
151returns the (unsigned) negation of the absolute value of the number, unless
152the absolute value would overflow.
153In this case,
154.Fn strtoul
155returns
156.Dv ULONG_MAX
157and sets the global variable
158.Va errno
159to
160.Er ERANGE .
161.Pp
162The
163.Fn strtoull
164function has identical return values except that
165.Dv ULLONG_MIN
166and
167.Dv ULLONG_MAX
168are used to indicate underflow and overflow respectively.
169.Pp
170There is no way to determine if
171.Fn strtoul
172has processed a negative number (and returned an unsigned value) short of
173examining the string in
174.Fa nptr
175directly.
176.Sh EXAMPLES
177Ensuring that a string is a valid number (i.e., in range and containing no
178trailing characters) requires clearing
179.Va errno
180beforehand explicitly since
181.Va errno
182is not changed on a successful call to
183.Fn strtoul ,
184and the return value of
185.Fn strtoul
186cannot be used unambiguously to signal an error:
187.Bd -literal -offset indent
188char *ep;
189unsigned long ulval;
190
191\&...
192
193errno = 0;
194ulval = strtoul(buf, &ep, 10);
195if (buf[0] == '\e0' || *ep != '\e0')
196 goto not_a_number;
197if (errno == ERANGE && ulval == ULONG_MAX)
198 goto out_of_range;
199.Ed
200.Pp
201This example will accept
202.Dq 12
203but not
204.Dq 12foo
205or
206.Dq 12\en .
207If trailing whitespace is acceptable, further checks must be done on
208.Va *ep ;
209alternately, use
210.Xr sscanf 3 .
211.Sh ERRORS
212.Bl -tag -width Er
213.It Bq Er ERANGE
214The given string was out of range; the value converted has been clamped.
215.El
216.Sh SEE ALSO
217.Xr sscanf 3 ,
218.Xr strtol 3
219.Sh STANDARDS
220The
221.Fn strtoul
222function conforms to
223.St -ansiC .
224.Sh BUGS
225Ignores the current locale.