diff options
author | pjanzen <> | 1999-09-14 03:59:55 +0000 |
---|---|---|
committer | pjanzen <> | 1999-09-14 03:59:55 +0000 |
commit | f8087e2671bd27448a73944c96b9dfb2132a0ee2 (patch) | |
tree | 2bf23b5a52df56629542b965f131bb62453a7958 | |
parent | 4fbe3f20ecbd74a298ba0cc4b8105b098f93b6ea (diff) | |
download | openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.tar.gz openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.tar.bz2 openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.zip |
Supply examples and discuss limitations.
-rw-r--r-- | src/lib/libc/stdlib/strtol.3 | 65 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 62 |
2 files changed, 120 insertions, 7 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 |
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 index 570e3a9e3f..80eb037844 100644 --- a/src/lib/libc/stdlib/strtoul.3 +++ b/src/lib/libc/stdlib/strtoul.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: strtoul.3,v 1.4 1999/07/20 10:18:21 aaron Exp $ | 36 | .\" $OpenBSD: strtoul.3,v 1.5 1999/09/14 03:59:55 pjanzen Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 25, 1992 | 38 | .Dd June 25, 1992 |
39 | .Dt STRTOUL 3 | 39 | .Dt STRTOUL 3 |
@@ -134,11 +134,18 @@ on return, the entire string was valid.) | |||
134 | The | 134 | The |
135 | .Fn strtoul | 135 | .Fn strtoul |
136 | function | 136 | function |
137 | returns either the result of the conversion | 137 | returns the result of the conversion, |
138 | or, if there was a leading minus sign, | 138 | unless the value would overflow, in which case |
139 | the negation of the result of the conversion, | 139 | .Dv ULONG_MAX |
140 | unless the original (non-negated) value would overflow; | 140 | is returned and |
141 | in the latter case, | 141 | .Va errno |
142 | is set to | ||
143 | .Er ERANGE . | ||
144 | If there was a leading minus sign, | ||
145 | .Fn strtoul | ||
146 | returns the (unsigned) negation of the absolute value of the number, unless | ||
147 | the absolute value would overflow. | ||
148 | In this case, | ||
142 | .Fn strtoul | 149 | .Fn strtoul |
143 | returns | 150 | returns |
144 | .Dv ULONG_MAX | 151 | .Dv ULONG_MAX |
@@ -146,12 +153,55 @@ and sets the global variable | |||
146 | .Va errno | 153 | .Va errno |
147 | to | 154 | to |
148 | .Er ERANGE . | 155 | .Er ERANGE . |
156 | .Pp | ||
157 | There is no way to determine if | ||
158 | .Fn strtoul | ||
159 | has processed a negative number (and returned an unsigned value) short of | ||
160 | examining the string in | ||
161 | .Fa nptr | ||
162 | directly. | ||
163 | .Sh EXAMPLES | ||
164 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
165 | trailing characters) requires clearing | ||
166 | .Va errno | ||
167 | beforehand explicitly since | ||
168 | .Va errno | ||
169 | is not changed on a successful call to | ||
170 | .Fn strtoul , | ||
171 | and the return value of | ||
172 | .Fn strtoul | ||
173 | cannot be used unambiguously to signal an error: | ||
174 | .Bd -literal -offset indent | ||
175 | char *ep; | ||
176 | unsigned long ulval; | ||
177 | |||
178 | \&... | ||
179 | |||
180 | errno = 0; | ||
181 | ulval = strtoul(buf, &ep, 10); | ||
182 | if (buf[0] == '\e0' || *ep != '\e0') | ||
183 | goto not_a_number; | ||
184 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
185 | goto out_of_range; | ||
186 | .Ed | ||
187 | .Pp | ||
188 | This example will accept | ||
189 | .Dq 12 | ||
190 | but not | ||
191 | .Dq 12foo | ||
192 | or | ||
193 | .Dq 12\en . | ||
194 | If trailing whitespace is acceptable, further checks must be done on | ||
195 | .Va *ep ; | ||
196 | alternately, use | ||
197 | .Xr sscanf 3 . | ||
149 | .Sh ERRORS | 198 | .Sh ERRORS |
150 | .Bl -tag -width Er | 199 | .Bl -tag -width Er |
151 | .It Bq Er ERANGE | 200 | .It Bq Er ERANGE |
152 | The given string was out of range; the value converted has been clamped. | 201 | The given string was out of range; the value converted has been clamped. |
153 | .El | 202 | .El |
154 | .Sh SEE ALSO | 203 | .Sh SEE ALSO |
204 | .Xr sscanf 3 , | ||
155 | .Xr strtol 3 | 205 | .Xr strtol 3 |
156 | .Sh STANDARDS | 206 | .Sh STANDARDS |
157 | The | 207 | The |