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