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 /src/lib/libc/stdlib/strtoul.3 | |
parent | 4fbe3f20ecbd74a298ba0cc4b8105b098f93b6ea (diff) | |
download | openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.tar.gz openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.tar.bz2 openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.zip |
Supply examples and discuss limitations.
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.3')
-rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 62 |
1 files changed, 56 insertions, 6 deletions
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 |