summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpjanzen <>1999-09-14 03:59:55 +0000
committerpjanzen <>1999-09-14 03:59:55 +0000
commitf8087e2671bd27448a73944c96b9dfb2132a0ee2 (patch)
tree2bf23b5a52df56629542b965f131bb62453a7958
parent4fbe3f20ecbd74a298ba0cc4b8105b098f93b6ea (diff)
downloadopenbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.tar.gz
openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.tar.bz2
openbsd-f8087e2671bd27448a73944c96b9dfb2132a0ee2.zip
Supply examples and discuss limitations.
-rw-r--r--src/lib/libc/stdlib/strtol.365
-rw-r--r--src/lib/libc/stdlib/strtoul.362
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
148is set to 148is set to
149.Er ERANGE . 149.Er ERANGE .
150.Sh EXAMPLES
151Ensuring that a string is a valid number (i.e., in range and containing no
152trailing characters) requires clearing
153.Va errno
154beforehand explicitly since
155.Va errno
156is not changed on a successful call to
157.Fn strtol ,
158and the return value of
159.Fn strtol
160cannot be used unambiguously to signal an error:
161.Bd -literal -offset indent
162char *ep;
163long lval;
164
165\&...
166
167errno = 0;
168lval = strtol(buf, &ep, 10);
169if (buf[0] == '\e0' || *ep != '\e0')
170 goto not_a_number;
171if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
172 goto out_of_range;
173.Ed
174.Pp
175This example will accept
176.Dq 12
177but not
178.Dq 12foo
179or
180.Dq 12\en .
181If trailing whitespace is acceptable, further checks must be done on
182.Va *ep ;
183alternately, use
184.Xr sscanf 3 .
185.Pp
186If
187.Fn strtol
188is being used instead of
189.Xr atoi 3 ,
190error checking is further complicated because the desired return value is an
191.Li int
192rather than a
193.Li long ;
194however, on some architectures integers and long integers are the same
195size. Thus the following is necessary:
196.Bd -literal -offset indent
197char *ep;
198int ival;
199long lval;
200
201\&...
202
203errno = 0;
204lval = strtol(buf, &ep, 10);
205if (buf[0] == '\e0' || *ep != '\e0')
206 goto not_a_number;
207if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
208 (lval > INT_MAX || lval < INT_MIN))
209 goto out_of_range;
210ival = 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.)
134The 134The
135.Fn strtoul 135.Fn strtoul
136function 136function
137returns either the result of the conversion 137returns the result of the conversion,
138or, if there was a leading minus sign, 138unless the value would overflow, in which case
139the negation of the result of the conversion, 139.Dv ULONG_MAX
140unless the original (non-negated) value would overflow; 140is returned and
141in the latter case, 141.Va errno
142is set to
143.Er ERANGE .
144If there was a leading minus sign,
145.Fn strtoul
146returns the (unsigned) negation of the absolute value of the number, unless
147the absolute value would overflow.
148In this case,
142.Fn strtoul 149.Fn strtoul
143returns 150returns
144.Dv ULONG_MAX 151.Dv ULONG_MAX
@@ -146,12 +153,55 @@ and sets the global variable
146.Va errno 153.Va errno
147to 154to
148.Er ERANGE . 155.Er ERANGE .
156.Pp
157There is no way to determine if
158.Fn strtoul
159has processed a negative number (and returned an unsigned value) short of
160examining the string in
161.Fa nptr
162directly.
163.Sh EXAMPLES
164Ensuring that a string is a valid number (i.e., in range and containing no
165trailing characters) requires clearing
166.Va errno
167beforehand explicitly since
168.Va errno
169is not changed on a successful call to
170.Fn strtoul ,
171and the return value of
172.Fn strtoul
173cannot be used unambiguously to signal an error:
174.Bd -literal -offset indent
175char *ep;
176unsigned long ulval;
177
178\&...
179
180errno = 0;
181ulval = strtoul(buf, &ep, 10);
182if (buf[0] == '\e0' || *ep != '\e0')
183 goto not_a_number;
184if (errno == ERANGE && ulval == ULONG_MAX)
185 goto out_of_range;
186.Ed
187.Pp
188This example will accept
189.Dq 12
190but not
191.Dq 12foo
192or
193.Dq 12\en .
194If trailing whitespace is acceptable, further checks must be done on
195.Va *ep ;
196alternately, 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
152The given string was out of range; the value converted has been clamped. 201The 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
157The 207The