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