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