summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/strtol.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/strtol.3')
-rw-r--r--src/lib/libc/stdlib/strtol.365
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
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