diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtol.3')
-rw-r--r-- | src/lib/libc/stdlib/strtol.3 | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 index e7435cf87a..3e0d5e67c7 100644 --- a/src/lib/libc/stdlib/strtol.3 +++ b/src/lib/libc/stdlib/strtol.3 | |||
@@ -33,7 +33,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
35 | .\" | 35 | .\" |
36 | .\" $OpenBSD: strtol.3,v 1.5 1999/07/20 10:18:22 aaron Exp $ | 36 | .\" $OpenBSD: strtol.3,v 1.6 1999/09/14 03:59:55 pjanzen Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 25, 1992 | 38 | .Dd June 25, 1992 |
39 | .Dt STRTOL 3 | 39 | .Dt STRTOL 3 |
@@ -147,6 +147,68 @@ In both cases, | |||
147 | .Va errno | 147 | .Va errno |
148 | is set to | 148 | is set to |
149 | .Er ERANGE . | 149 | .Er ERANGE . |
150 | .Sh EXAMPLES | ||
151 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
152 | trailing characters) requires clearing | ||
153 | .Va errno | ||
154 | beforehand explicitly since | ||
155 | .Va errno | ||
156 | is not changed on a successful call to | ||
157 | .Fn strtol , | ||
158 | and the return value of | ||
159 | .Fn strtol | ||
160 | cannot be used unambiguously to signal an error: | ||
161 | .Bd -literal -offset indent | ||
162 | char *ep; | ||
163 | long lval; | ||
164 | |||
165 | \&... | ||
166 | |||
167 | errno = 0; | ||
168 | lval = strtol(buf, &ep, 10); | ||
169 | if (buf[0] == '\e0' || *ep != '\e0') | ||
170 | goto not_a_number; | ||
171 | if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) | ||
172 | goto out_of_range; | ||
173 | .Ed | ||
174 | .Pp | ||
175 | This example will accept | ||
176 | .Dq 12 | ||
177 | but not | ||
178 | .Dq 12foo | ||
179 | or | ||
180 | .Dq 12\en . | ||
181 | If trailing whitespace is acceptable, further checks must be done on | ||
182 | .Va *ep ; | ||
183 | alternately, use | ||
184 | .Xr sscanf 3 . | ||
185 | .Pp | ||
186 | If | ||
187 | .Fn strtol | ||
188 | is being used instead of | ||
189 | .Xr atoi 3 , | ||
190 | error checking is further complicated because the desired return value is an | ||
191 | .Li int | ||
192 | rather than a | ||
193 | .Li long ; | ||
194 | however, on some architectures integers and long integers are the same | ||
195 | size. Thus the following is necessary: | ||
196 | .Bd -literal -offset indent | ||
197 | char *ep; | ||
198 | int ival; | ||
199 | long lval; | ||
200 | |||
201 | \&... | ||
202 | |||
203 | errno = 0; | ||
204 | lval = strtol(buf, &ep, 10); | ||
205 | if (buf[0] == '\e0' || *ep != '\e0') | ||
206 | goto not_a_number; | ||
207 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | ||
208 | (lval > INT_MAX || lval < INT_MIN)) | ||
209 | goto out_of_range; | ||
210 | ival = lval; | ||
211 | .Ed | ||
150 | .Sh ERRORS | 212 | .Sh ERRORS |
151 | .Bl -tag -width Er | 213 | .Bl -tag -width Er |
152 | .It Bq Er ERANGE | 214 | .It Bq Er ERANGE |
@@ -156,6 +218,7 @@ The given string was out of range; the value converted has been clamped. | |||
156 | .Xr atof 3 , | 218 | .Xr atof 3 , |
157 | .Xr atoi 3 , | 219 | .Xr atoi 3 , |
158 | .Xr atol 3 , | 220 | .Xr atol 3 , |
221 | .Xr sscanf 3 , | ||
159 | .Xr strtod 3 , | 222 | .Xr strtod 3 , |
160 | .Xr strtoul 3 | 223 | .Xr strtoul 3 |
161 | .Sh STANDARDS | 224 | .Sh STANDARDS |