summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortedu <>2004-05-06 06:19:01 +0000
committertedu <>2004-05-06 06:19:01 +0000
commit7455a413fe887c060539c823b908a490bc474192 (patch)
tree0bafa3b12d48725b874ead7ab25e70b9e1d84ad8 /src/lib
parentbca42790fd5227cb52b052ff8c6a4fc4e57c6f11 (diff)
downloadopenbsd-7455a413fe887c060539c823b908a490bc474192.tar.gz
openbsd-7455a413fe887c060539c823b908a490bc474192.tar.bz2
openbsd-7455a413fe887c060539c823b908a490bc474192.zip
improved man page, with example and error strings, suggested by deraadt@
man help jmc@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/stdlib/strtonum.392
1 files changed, 56 insertions, 36 deletions
diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3
index 763efc180f..7a8d7faf6d 100644
--- a/src/lib/libc/stdlib/strtonum.3
+++ b/src/lib/libc/stdlib/strtonum.3
@@ -1,35 +1,18 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strtonum.3,v 1.5 2004/05/06 06:19:01 tedu Exp $
2.\" All rights reserved.
3.\" 2.\"
4.\" This code is derived from software contributed to Berkeley by 3.\" Copyright (c) 2004 Ted Unangst
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\" 4.\"
8.\" Redistribution and use in source and binary forms, with or without 5.\" Permission to use, copy, modify, and distribute this software for any
9.\" modification, are permitted provided that the following conditions 6.\" purpose with or without fee is hereby granted, provided that the above
10.\" are met: 7.\" copyright notice and this permission notice appear in all copies.
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\" 8.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strtonum.3,v 1.4 2004/05/06 04:20:20 deraadt Exp $
33.\" 16.\"
34.Dd April 29, 2004 17.Dd April 29, 2004
35.Dt STRTONUM 3 18.Dt STRTONUM 3
@@ -55,8 +38,15 @@ function converts the string in
55to an 38to an
56.Li unsigned long long 39.Li unsigned long long
57value. 40value.
58The conversion is done according to base 10.
59Negative values may be obtained by casting the result. 41Negative values may be obtained by casting the result.
42The
43.Fn strtonum
44function was designed to facilitate safe, robust programming
45and overcome the shortcomings of the
46.Xr atoi 3
47and
48.Xr strtol 3
49family of interfaces.
60.Pp 50.Pp
61The string may begin with an arbitrary amount of whitespace 51The string may begin with an arbitrary amount of whitespace
62(as determined by 52(as determined by
@@ -69,9 +59,7 @@ sign.
69.Pp 59.Pp
70The remainder of the string is converted to an 60The remainder of the string is converted to an
71.Li unsigned long long 61.Li unsigned long long
72value in the obvious manner, 62value according to base 10.
73stopping at the first character which is not a valid digit
74in the given base.
75.Pp 63.Pp
76The value obtained is then checked against the provided 64The value obtained is then checked against the provided
77.Fa minval 65.Fa minval
@@ -89,14 +77,41 @@ indicating the failure.
89The 77The
90.Fn strtonum 78.Fn strtonum
91function returns the result of the conversion, 79function returns the result of the conversion,
92unless the value would underflow or overflow. 80unless the value would exceed the provided bounds or is invalid.
93On error, 0 is returned. 81On error, 0 is returned and
82.Fa errstr
83will point to an error message.
84.Sh EXAMPLES
85Using
86.Fn strtonum
87correctly is meant to be simpler than the alternative functions.
88.Bd -literal -offset indent
89int iterations;
90const char *errstr;
91
92iterations = strtonum(optarg, 1, 64, &errstr);
93if (errstr)
94 errx(1, "number of iterations is %s: %s", errstr, optarg);
95.Ed
96.Pp
97The above example will guarantee that the value of iterations is between
981 and 64.
94.Sh ERRORS 99.Sh ERRORS
95.Bl -tag -width Er 100.Bl -tag -width Er
96.It Bq Er ERANGE 101.It Bq Er ERANGE
97The given string was out of range. 102The given string was out of range.
98.It Bq Er EINVAL 103.It Bq Er EINVAL
99The given string did not consist solely of a valid number. 104The given string did not consist solely of digit characters.
105.El
106.Pp
107If an error occurs, errstr will be set to one of the following strings.
108.Bl -tag -width "too large"
109.It "too large"
110The result was larger than the provided maximum value.
111.It "too small"
112The result was smaller than the provided minimum value.
113.It "invalid"
114The string did not consist solely of digit characters.
100.El 115.El
101.Sh SEE ALSO 116.Sh SEE ALSO
102.Xr atof 3 , 117.Xr atof 3 ,
@@ -112,3 +127,8 @@ The given string did not consist solely of a valid number.
112is an 127is an
113.Ox 128.Ox
114extension. 129extension.
130The existing alternatives, such as
131.Xr atoi 3
132and
133.Xr strtol 3
134are either impossible or difficult to use safely.