diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtol.3')
-rw-r--r-- | src/lib/libc/stdlib/strtol.3 | 273 |
1 files changed, 0 insertions, 273 deletions
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 deleted file mode 100644 index 92774d082c..0000000000 --- a/src/lib/libc/stdlib/strtol.3 +++ /dev/null | |||
@@ -1,273 +0,0 @@ | |||
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: strtol.3,v 1.27 2015/04/14 22:16:03 nicm Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: April 14 2015 $ | ||
35 | .Dt STRTOL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtol , | ||
39 | .Nm strtoll , | ||
40 | .Nm strtoimax , | ||
41 | .Nm strtoq | ||
42 | .Nd convert string value to a long, long long or intmax_t integer | ||
43 | .Sh SYNOPSIS | ||
44 | .In limits.h | ||
45 | .In stdlib.h | ||
46 | .Ft long | ||
47 | .Fn strtol "const char *nptr" "char **endptr" "int base" | ||
48 | .Ft long long | ||
49 | .Fn strtoll "const char *nptr" "char **endptr" "int base" | ||
50 | .In inttypes.h | ||
51 | .Ft intmax_t | ||
52 | .Fn strtoimax "const char *nptr" "char **endptr" "int base" | ||
53 | .In sys/types.h | ||
54 | .In limits.h | ||
55 | .In stdlib.h | ||
56 | .Ft quad_t | ||
57 | .Fn strtoq "const char *nptr" "char **endptr" "int base" | ||
58 | .Sh DESCRIPTION | ||
59 | The | ||
60 | .Fn strtol | ||
61 | function converts the string in | ||
62 | .Fa nptr | ||
63 | to a | ||
64 | .Vt long | ||
65 | value. | ||
66 | The | ||
67 | .Fn strtoll | ||
68 | function converts the string in | ||
69 | .Fa nptr | ||
70 | to a | ||
71 | .Vt long long | ||
72 | value. | ||
73 | The | ||
74 | .Fn strtoimax | ||
75 | function converts the string in | ||
76 | .Fa nptr | ||
77 | to an | ||
78 | .Vt intmax_t | ||
79 | value. | ||
80 | The | ||
81 | .Fn strtoq | ||
82 | function is a deprecated equivalent of | ||
83 | .Fn strtoll | ||
84 | and is provided for backwards compatibility with legacy programs. | ||
85 | The conversion is done according to the given | ||
86 | .Fa base , | ||
87 | which must be a number between 2 and 36 inclusive or the special value 0. | ||
88 | .Pp | ||
89 | The string may begin with an arbitrary amount of whitespace | ||
90 | (as determined by | ||
91 | .Xr isspace 3 ) | ||
92 | followed by a single optional | ||
93 | .Ql + | ||
94 | or | ||
95 | .Ql - | ||
96 | sign. | ||
97 | If | ||
98 | .Fa base | ||
99 | is zero or 16, the string may then include a | ||
100 | .Ql 0x | ||
101 | prefix, and the number will be read in base 16; otherwise, a zero | ||
102 | .Fa base | ||
103 | is taken as 10 (decimal) unless the next character is | ||
104 | .Ql 0 , | ||
105 | in which case it is taken as 8 (octal). | ||
106 | .Pp | ||
107 | The remainder of the string is converted to a | ||
108 | .Vt long , | ||
109 | .Vt long long , | ||
110 | or | ||
111 | .Vt intmax_t | ||
112 | value in the obvious manner, | ||
113 | stopping at the first character which is not a valid digit | ||
114 | in the given base. | ||
115 | (In bases above 10, the letter | ||
116 | .Ql A | ||
117 | in either upper or lower case represents 10, | ||
118 | .Ql B | ||
119 | represents 11, and so forth, with | ||
120 | .Ql Z | ||
121 | representing 35.) | ||
122 | .Pp | ||
123 | If | ||
124 | .Fa endptr | ||
125 | is non-null, | ||
126 | .Fn strtol | ||
127 | stores the address of the first invalid character in | ||
128 | .Fa *endptr . | ||
129 | If there were no digits at all, however, | ||
130 | .Fn strtol | ||
131 | stores the original value of | ||
132 | .Fa nptr | ||
133 | in | ||
134 | .Fa *endptr . | ||
135 | (Thus, if | ||
136 | .Fa *nptr | ||
137 | is not | ||
138 | .Ql \e0 | ||
139 | but | ||
140 | .Fa **endptr | ||
141 | is | ||
142 | .Ql \e0 | ||
143 | on return, the entire string was valid.) | ||
144 | .Sh RETURN VALUES | ||
145 | The | ||
146 | .Fn strtol , | ||
147 | .Fn strtoll , | ||
148 | .Fn strtoimax , | ||
149 | and | ||
150 | .Fn strtoq | ||
151 | functions return the result of the conversion. | ||
152 | If overflow or underflow occurs, | ||
153 | .Va errno | ||
154 | is set to | ||
155 | .Er ERANGE | ||
156 | and the function return value is as follows: | ||
157 | .Bl -column "strtoimaxXX" "INTMAX_MIN" "INTMAX_MAX" -offset indent | ||
158 | .It Sy Function Ta Sy underflow Ta Sy overflow | ||
159 | .It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX | ||
160 | .It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX | ||
161 | .It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX | ||
162 | .It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX | ||
163 | .El | ||
164 | .Pp | ||
165 | If there is no valid digit, 0 is returned. | ||
166 | If | ||
167 | .Ar base | ||
168 | is invalid, 0 is returned and the global variable | ||
169 | .Va errno | ||
170 | is set to | ||
171 | .Er EINVAL . | ||
172 | .Sh EXAMPLES | ||
173 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
174 | trailing characters) requires clearing | ||
175 | .Va errno | ||
176 | beforehand explicitly since | ||
177 | .Va errno | ||
178 | is not changed on a successful call to | ||
179 | .Fn strtol , | ||
180 | and the return value of | ||
181 | .Fn strtol | ||
182 | cannot be used unambiguously to signal an error: | ||
183 | .Bd -literal -offset indent | ||
184 | char *ep; | ||
185 | long lval; | ||
186 | |||
187 | \&... | ||
188 | |||
189 | errno = 0; | ||
190 | lval = strtol(buf, &ep, 10); | ||
191 | if (buf[0] == '\e0' || *ep != '\e0') | ||
192 | goto not_a_number; | ||
193 | if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) | ||
194 | goto out_of_range; | ||
195 | .Ed | ||
196 | .Pp | ||
197 | This example will accept | ||
198 | .Dq 12 | ||
199 | but not | ||
200 | .Dq 12foo | ||
201 | or | ||
202 | .Dq 12\en . | ||
203 | If trailing whitespace is acceptable, further checks must be done on | ||
204 | .Va *ep ; | ||
205 | alternately, use | ||
206 | .Xr sscanf 3 . | ||
207 | .Pp | ||
208 | If | ||
209 | .Fn strtol | ||
210 | is being used instead of | ||
211 | .Xr atoi 3 , | ||
212 | error checking is further complicated because the desired return value is an | ||
213 | .Vt int | ||
214 | rather than a | ||
215 | .Vt long ; | ||
216 | however, on some architectures integers and long integers are the same size. | ||
217 | Thus the following is necessary: | ||
218 | .Bd -literal -offset indent | ||
219 | char *ep; | ||
220 | int ival; | ||
221 | long lval; | ||
222 | |||
223 | \&... | ||
224 | |||
225 | errno = 0; | ||
226 | lval = strtol(buf, &ep, 10); | ||
227 | if (buf[0] == '\e0' || *ep != '\e0') | ||
228 | goto not_a_number; | ||
229 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | ||
230 | (lval > INT_MAX || lval < INT_MIN)) | ||
231 | goto out_of_range; | ||
232 | ival = lval; | ||
233 | .Ed | ||
234 | .Sh ERRORS | ||
235 | .Bl -tag -width Er | ||
236 | .It Bq Er EINVAL | ||
237 | The value of | ||
238 | .Ar base | ||
239 | was neither between 2 and 36 inclusive nor the special value 0. | ||
240 | .It Bq Er ERANGE | ||
241 | The given string was out of range; the value converted has been clamped. | ||
242 | .El | ||
243 | .Sh SEE ALSO | ||
244 | .Xr atof 3 , | ||
245 | .Xr atoi 3 , | ||
246 | .Xr atol 3 , | ||
247 | .Xr atoll 3 , | ||
248 | .Xr sscanf 3 , | ||
249 | .Xr strtod 3 , | ||
250 | .Xr strtonum 3 , | ||
251 | .Xr strtoul 3 | ||
252 | .Sh STANDARDS | ||
253 | The | ||
254 | .Fn strtol , | ||
255 | .Fn strtoll , | ||
256 | and | ||
257 | .Fn strtoimax | ||
258 | functions conform to | ||
259 | .St -isoC-99 . | ||
260 | Setting | ||
261 | .Va errno | ||
262 | to | ||
263 | .Dv EINVAL | ||
264 | is an extension to that standard required by | ||
265 | .St -p1003.1-2008 . | ||
266 | .Pp | ||
267 | The | ||
268 | .Fn strtoq | ||
269 | function is a | ||
270 | .Bx | ||
271 | extension and is provided for backwards compatibility with legacy programs. | ||
272 | .Sh BUGS | ||
273 | Ignores the current locale. | ||