diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.3')
-rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 166 |
1 files changed, 122 insertions, 44 deletions
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 index db551b0141..2ea5e83297 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,21 +29,30 @@ | |||
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.16 2006/01/13 17:58:09 millert 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 January 3, 2006 |
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> | 44 | .Fd #include <stdlib.h> |
47 | .Fd #include <limits.h> | 45 | .Fd #include <limits.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> | 57 | .Fd #include <stdlib.h> |
53 | .Fd #include <limits.h> | 58 | .Fd #include <limits.h> |
@@ -56,26 +61,41 @@ | |||
56 | .Sh DESCRIPTION | 61 | .Sh DESCRIPTION |
57 | The | 62 | The |
58 | .Fn strtoul | 63 | .Fn strtoul |
59 | function | 64 | function converts the string in |
60 | converts the string in | ||
61 | .Fa nptr | 65 | .Fa nptr |
62 | to an | 66 | to an |
63 | .Em unsigned long | 67 | .Li unsigned long |
64 | value. | 68 | value. |
65 | The | 69 | The |
66 | .Fn strtouq | 70 | .Fn strtoull |
67 | function | 71 | function converts the string in |
68 | 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 | ||
69 | .Fa nptr | 79 | .Fa nptr |
70 | to a | 80 | to a |
71 | .Em u_quad_t | 81 | .Li umaxint_t |
72 | value. | 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. | ||
73 | The conversion is done according to the given | 88 | The conversion is done according to the given |
74 | .Fa base , | 89 | .Fa base , |
75 | which must be between 2 and 36 inclusive, | 90 | which must be a number between 2 and 36 inclusive |
76 | or be the special value 0. | 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. | ||
77 | .Pp | 97 | .Pp |
78 | The string may begin with an arbitrary amount of white space | 98 | The 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 ) |
81 | followed by a single optional | 101 | followed by a single optional |
@@ -85,26 +105,22 @@ or | |||
85 | sign. | 105 | sign. |
86 | If | 106 | If |
87 | .Fa base | 107 | .Fa base |
88 | is zero or 16, | 108 | is zero or 16, the string may then include a |
89 | the string may then include a | ||
90 | .Ql 0x | 109 | .Ql 0x |
91 | prefix, | 110 | prefix, and the number will be read in base 16; otherwise, a zero |
92 | and the number will be read in base 16; otherwise, a zero | ||
93 | .Fa base | 111 | .Fa base |
94 | is taken as 10 (decimal) unless the next character is | 112 | is taken as 10 (decimal) unless the next character is |
95 | .Ql 0 , | 113 | .Ql 0 , |
96 | in which case it is taken as 8 (octal). | 114 | in which case it is taken as 8 (octal). |
97 | .Pp | 115 | .Pp |
98 | The remainder of the string is converted to an | 116 | The remainder of the string is converted to an |
99 | .Em unsigned long | 117 | .Li unsigned long |
100 | value in the obvious manner, | 118 | value in the obvious manner, stopping at the end of the string |
101 | stopping at the end of the string | ||
102 | or at the first character that does not produce a valid digit | 119 | or at the first character that does not produce a valid digit |
103 | in the given base. | 120 | in the given base. |
104 | (In bases above 10, the letter | 121 | (In bases above 10, the letter |
105 | .Ql A | 122 | .Ql A |
106 | in either upper or lower case | 123 | in either upper or lower case represents 10, |
107 | represents 10, | ||
108 | .Ql B | 124 | .Ql B |
109 | represents 11, and so forth, with | 125 | represents 11, and so forth, with |
110 | .Ql Z | 126 | .Ql Z |
@@ -112,7 +128,7 @@ representing 35.) | |||
112 | .Pp | 128 | .Pp |
113 | If | 129 | If |
114 | .Fa endptr | 130 | .Fa endptr |
115 | is non nil, | 131 | is non-null, |
116 | .Fn strtoul | 132 | .Fn strtoul |
117 | stores the address of the first invalid character in | 133 | stores the address of the first invalid character in |
118 | .Fa *endptr . | 134 | .Fa *endptr . |
@@ -133,32 +149,94 @@ is | |||
133 | on return, the entire string was valid.) | 149 | on return, the entire string was valid.) |
134 | .Sh RETURN VALUES | 150 | .Sh RETURN VALUES |
135 | The | 151 | The |
136 | .Fn strtoul | 152 | .Fn strtoul , |
137 | function | 153 | .Fn strtoull , |
138 | returns either the result of the conversion | 154 | .Fn strtoumax |
139 | or, if there was a leading minus sign, | 155 | and |
156 | .Fn strtouq | ||
157 | functions return either the result of the conversion or, | ||
158 | if there was a leading minus sign, | ||
140 | the negation of the result of the conversion, | 159 | the negation of the result of the conversion, |
141 | unless the original (non-negated) value would overflow; | 160 | unless the original (non-negated) value would overflow. |
142 | in the latter case, | 161 | If overflow occurs, |
143 | .Fn strtoul | 162 | .Fn strtoul |
144 | returns | 163 | returns |
145 | .Dv ULONG_MAX | 164 | .Dv ULONG_MAX , |
146 | and sets the global variable | 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 | ||
147 | .Va errno | 175 | .Va errno |
148 | to | 176 | is set to |
149 | .Er ERANGE . | 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 . | ||
150 | .Sh ERRORS | 220 | .Sh ERRORS |
151 | .Bl -tag -width Er | 221 | .Bl -tag -width Er |
152 | .It Bq Er ERANGE | 222 | .It Bq Er ERANGE |
153 | The given string was out of range; the value converted has been clamped. | 223 | The given string was out of range; the value converted has been clamped. |
154 | .El | 224 | .El |
155 | .Sh SEE ALSO | 225 | .Sh SEE ALSO |
226 | .Xr sscanf 3 , | ||
156 | .Xr strtol 3 | 227 | .Xr strtol 3 |
157 | .Sh STANDARDS | 228 | .Sh STANDARDS |
158 | The | 229 | The |
159 | .Fn strtoul | 230 | .Fn strtoul , |
160 | function | 231 | .Fn strtoull , |
161 | conforms to | 232 | and |
162 | .St -ansiC . | 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. | ||
163 | .Sh BUGS | 241 | .Sh BUGS |
164 | Ignores the current locale. | 242 | Ignores the current locale. |