diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.3')
-rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 225 |
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 | ||
63 | The | ||
64 | .Fn strtoul | ||
65 | function converts the string in | ||
66 | .Fa nptr | ||
67 | to an | ||
68 | .Li unsigned long | ||
69 | value. | ||
70 | The | ||
71 | .Fn strtoull | ||
72 | function converts the string in | ||
73 | .Fa nptr | ||
74 | to an | ||
75 | .Li unsigned long long | ||
76 | value. | ||
77 | The | ||
78 | .Fn strtouq | ||
79 | function is a deprecated equivalent of | ||
80 | .Fn strtoull | ||
81 | and is provided for backwards compatibility with legacy programs. | ||
82 | The conversion is done according to the given | ||
83 | .Fa base , | ||
84 | which must be a number between 2 and 36 inclusive | ||
85 | or the special value 0. | ||
86 | .Pp | ||
87 | The string may begin with an arbitrary amount of whitespace | ||
88 | (as determined by | ||
89 | .Xr isspace 3 ) | ||
90 | followed by a single optional | ||
91 | .Ql + | ||
92 | or | ||
93 | .Ql - | ||
94 | sign. | ||
95 | If | ||
96 | .Fa base | ||
97 | is zero or 16, the string may then include a | ||
98 | .Ql 0x | ||
99 | prefix, and the number will be read in base 16; otherwise, a zero | ||
100 | .Fa base | ||
101 | is taken as 10 (decimal) unless the next character is | ||
102 | .Ql 0 , | ||
103 | in which case it is taken as 8 (octal). | ||
104 | .Pp | ||
105 | The remainder of the string is converted to an | ||
106 | .Li unsigned long | ||
107 | value in the obvious manner, stopping at the end of the string | ||
108 | or at the first character that does not produce a valid digit | ||
109 | in the given base. | ||
110 | (In bases above 10, the letter | ||
111 | .Ql A | ||
112 | in either upper or lower case represents 10, | ||
113 | .Ql B | ||
114 | represents 11, and so forth, with | ||
115 | .Ql Z | ||
116 | representing 35.) | ||
117 | .Pp | ||
118 | If | ||
119 | .Fa endptr | ||
120 | is non-null, | ||
121 | .Fn strtoul | ||
122 | stores the address of the first invalid character in | ||
123 | .Fa *endptr . | ||
124 | If there were no digits at all, however, | ||
125 | .Fn strtoul | ||
126 | stores the original value of | ||
127 | .Fa nptr | ||
128 | in | ||
129 | .Fa *endptr . | ||
130 | (Thus, if | ||
131 | .Fa *nptr | ||
132 | is not | ||
133 | .Ql \e0 | ||
134 | but | ||
135 | .Fa **endptr | ||
136 | is | ||
137 | .Ql \e0 | ||
138 | on return, the entire string was valid.) | ||
139 | .Sh RETURN VALUES | ||
140 | The | ||
141 | .Fn strtoul | ||
142 | function returns the result of the conversion, | ||
143 | unless the value would overflow, in which case | ||
144 | .Dv ULONG_MAX | ||
145 | is returned and | ||
146 | .Va errno | ||
147 | is set to | ||
148 | .Er ERANGE . | ||
149 | If there was a leading minus sign, | ||
150 | .Fn strtoul | ||
151 | returns the (unsigned) negation of the absolute value of the number, unless | ||
152 | the absolute value would overflow. | ||
153 | In this case, | ||
154 | .Fn strtoul | ||
155 | returns | ||
156 | .Dv ULONG_MAX | ||
157 | and sets the global variable | ||
158 | .Va errno | ||
159 | to | ||
160 | .Er ERANGE . | ||
161 | .Pp | ||
162 | The | ||
163 | .Fn strtoull | ||
164 | function has identical return values except that | ||
165 | .Dv ULLONG_MIN | ||
166 | and | ||
167 | .Dv ULLONG_MAX | ||
168 | are used to indicate underflow and overflow respectively. | ||
169 | .Pp | ||
170 | There is no way to determine if | ||
171 | .Fn strtoul | ||
172 | has processed a negative number (and returned an unsigned value) short of | ||
173 | examining the string in | ||
174 | .Fa nptr | ||
175 | directly. | ||
176 | .Sh EXAMPLES | ||
177 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
178 | trailing characters) requires clearing | ||
179 | .Va errno | ||
180 | beforehand explicitly since | ||
181 | .Va errno | ||
182 | is not changed on a successful call to | ||
183 | .Fn strtoul , | ||
184 | and the return value of | ||
185 | .Fn strtoul | ||
186 | cannot be used unambiguously to signal an error: | ||
187 | .Bd -literal -offset indent | ||
188 | char *ep; | ||
189 | unsigned long ulval; | ||
190 | |||
191 | \&... | ||
192 | |||
193 | errno = 0; | ||
194 | ulval = strtoul(buf, &ep, 10); | ||
195 | if (buf[0] == '\e0' || *ep != '\e0') | ||
196 | goto not_a_number; | ||
197 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
198 | goto out_of_range; | ||
199 | .Ed | ||
200 | .Pp | ||
201 | This example will accept | ||
202 | .Dq 12 | ||
203 | but not | ||
204 | .Dq 12foo | ||
205 | or | ||
206 | .Dq 12\en . | ||
207 | If trailing whitespace is acceptable, further checks must be done on | ||
208 | .Va *ep ; | ||
209 | alternately, use | ||
210 | .Xr sscanf 3 . | ||
211 | .Sh ERRORS | ||
212 | .Bl -tag -width Er | ||
213 | .It Bq Er ERANGE | ||
214 | The 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 | ||
220 | The | ||
221 | .Fn strtoul | ||
222 | function conforms to | ||
223 | .St -ansiC . | ||
224 | .Sh BUGS | ||
225 | Ignores the current locale. | ||