diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.3')
-rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 242 |
1 files changed, 242 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..2ea5e83297 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.3 | |||
@@ -0,0 +1,242 @@ | |||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strtoul.3,v 1.16 2006/01/13 17:58:09 millert Exp $ | ||
33 | .\" | ||
34 | .Dd January 3, 2006 | ||
35 | .Dt STRTOUL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtoul , | ||
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" | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <stdlib.h> | ||
45 | .Fd #include <limits.h> | ||
46 | .Ft unsigned long | ||
47 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | ||
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 | ||
56 | .Fd #include <sys/types.h> | ||
57 | .Fd #include <stdlib.h> | ||
58 | .Fd #include <limits.h> | ||
59 | .Ft u_quad_t | ||
60 | .Fn strtouq "const char *nptr" "char **endptr" "int base" | ||
61 | .Sh DESCRIPTION | ||
62 | The | ||
63 | .Fn strtoul | ||
64 | function converts the string in | ||
65 | .Fa nptr | ||
66 | to an | ||
67 | .Li unsigned long | ||
68 | value. | ||
69 | The | ||
70 | .Fn strtoull | ||
71 | function converts the string in | ||
72 | .Fa nptr | ||
73 | to an | ||
74 | .Li unsigned long long | ||
75 | value. | ||
76 | The | ||
77 | .Fn strtoumax | ||
78 | function converts the string in | ||
79 | .Fa nptr | ||
80 | to a | ||
81 | .Li umaxint_t | ||
82 | value. | ||
83 | The | ||
84 | .Fn strtouq | ||
85 | function is a deprecated equivalent of | ||
86 | .Fn strtoull | ||
87 | and is provided for backwards compatibility with legacy programs. | ||
88 | The conversion is done according to the given | ||
89 | .Fa base , | ||
90 | which must be a number between 2 and 36 inclusive | ||
91 | or the special value 0. | ||
92 | If the string in | ||
93 | .Fa nptr | ||
94 | represents a negative number, it will be converted to its unsigned equivalent. | ||
95 | This behavior is consistent with what happens when a signed integer type is | ||
96 | cast to its unsigned counterpart. | ||
97 | .Pp | ||
98 | The string may begin with an arbitrary amount of whitespace | ||
99 | (as determined by | ||
100 | .Xr isspace 3 ) | ||
101 | followed by a single optional | ||
102 | .Ql + | ||
103 | or | ||
104 | .Ql - | ||
105 | sign. | ||
106 | If | ||
107 | .Fa base | ||
108 | is zero or 16, the string may then include a | ||
109 | .Ql 0x | ||
110 | prefix, and the number will be read in base 16; otherwise, a zero | ||
111 | .Fa base | ||
112 | is taken as 10 (decimal) unless the next character is | ||
113 | .Ql 0 , | ||
114 | in which case it is taken as 8 (octal). | ||
115 | .Pp | ||
116 | The remainder of the string is converted to an | ||
117 | .Li unsigned long | ||
118 | value in the obvious manner, stopping at the end of the string | ||
119 | or at the first character that does not produce a valid digit | ||
120 | in the given base. | ||
121 | (In bases above 10, the letter | ||
122 | .Ql A | ||
123 | in either upper or lower case represents 10, | ||
124 | .Ql B | ||
125 | represents 11, and so forth, with | ||
126 | .Ql Z | ||
127 | representing 35.) | ||
128 | .Pp | ||
129 | If | ||
130 | .Fa endptr | ||
131 | is non-null, | ||
132 | .Fn strtoul | ||
133 | stores the address of the first invalid character in | ||
134 | .Fa *endptr . | ||
135 | If there were no digits at all, however, | ||
136 | .Fn strtoul | ||
137 | stores the original value of | ||
138 | .Fa nptr | ||
139 | in | ||
140 | .Fa *endptr . | ||
141 | (Thus, if | ||
142 | .Fa *nptr | ||
143 | is not | ||
144 | .Ql \e0 | ||
145 | but | ||
146 | .Fa **endptr | ||
147 | is | ||
148 | .Ql \e0 | ||
149 | on return, the entire string was valid.) | ||
150 | .Sh RETURN VALUES | ||
151 | The | ||
152 | .Fn strtoul , | ||
153 | .Fn strtoull , | ||
154 | .Fn strtoumax | ||
155 | and | ||
156 | .Fn strtouq | ||
157 | functions return either the result of the conversion or, | ||
158 | if there was a leading minus sign, | ||
159 | the negation of the result of the conversion, | ||
160 | unless the original (non-negated) value would overflow. | ||
161 | If overflow occurs, | ||
162 | .Fn strtoul | ||
163 | returns | ||
164 | .Dv ULONG_MAX , | ||
165 | .Fn strtoull | ||
166 | returns | ||
167 | .Dv ULLONG_MAX , | ||
168 | .Fn strtoumax | ||
169 | returns | ||
170 | .Dv UINTMAX_MAX , | ||
171 | .Fn strtouq | ||
172 | returns | ||
173 | .Dv ULLONG_MAX | ||
174 | and the global variable | ||
175 | .Va errno | ||
176 | is set to | ||
177 | .Er ERANGE . | ||
178 | .Pp | ||
179 | There is no way to determine if | ||
180 | .Fn strtoul | ||
181 | has processed a negative number (and returned an unsigned value) short of | ||
182 | examining the string in | ||
183 | .Fa nptr | ||
184 | directly. | ||
185 | .Sh EXAMPLES | ||
186 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
187 | trailing characters) requires clearing | ||
188 | .Va errno | ||
189 | beforehand explicitly since | ||
190 | .Va errno | ||
191 | is not changed on a successful call to | ||
192 | .Fn strtoul , | ||
193 | and the return value of | ||
194 | .Fn strtoul | ||
195 | cannot be used unambiguously to signal an error: | ||
196 | .Bd -literal -offset indent | ||
197 | char *ep; | ||
198 | unsigned long ulval; | ||
199 | |||
200 | \&... | ||
201 | |||
202 | errno = 0; | ||
203 | ulval = strtoul(buf, &ep, 10); | ||
204 | if (buf[0] == '\e0' || *ep != '\e0') | ||
205 | goto not_a_number; | ||
206 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
207 | goto out_of_range; | ||
208 | .Ed | ||
209 | .Pp | ||
210 | This example will accept | ||
211 | .Dq 12 | ||
212 | but not | ||
213 | .Dq 12foo | ||
214 | or | ||
215 | .Dq 12\en . | ||
216 | If trailing whitespace is acceptable, further checks must be done on | ||
217 | .Va *ep ; | ||
218 | alternately, use | ||
219 | .Xr sscanf 3 . | ||
220 | .Sh ERRORS | ||
221 | .Bl -tag -width Er | ||
222 | .It Bq Er ERANGE | ||
223 | The given string was out of range; the value converted has been clamped. | ||
224 | .El | ||
225 | .Sh SEE ALSO | ||
226 | .Xr sscanf 3 , | ||
227 | .Xr strtol 3 | ||
228 | .Sh STANDARDS | ||
229 | The | ||
230 | .Fn strtoul , | ||
231 | .Fn strtoull , | ||
232 | and | ||
233 | .Fn strtoumax | ||
234 | functions conform to | ||
235 | .St -ansiC-99 . | ||
236 | The | ||
237 | .Fn strtouq | ||
238 | function is a | ||
239 | .Bx | ||
240 | extension and is provided for backwards compatibility with legacy programs. | ||
241 | .Sh BUGS | ||
242 | Ignores the current locale. | ||