diff options
author | tedu <> | 2004-05-06 06:19:01 +0000 |
---|---|---|
committer | tedu <> | 2004-05-06 06:19:01 +0000 |
commit | 7455a413fe887c060539c823b908a490bc474192 (patch) | |
tree | 0bafa3b12d48725b874ead7ab25e70b9e1d84ad8 /src/lib | |
parent | bca42790fd5227cb52b052ff8c6a4fc4e57c6f11 (diff) | |
download | openbsd-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.3 | 92 |
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 | |||
55 | to an | 38 | to an |
56 | .Li unsigned long long | 39 | .Li unsigned long long |
57 | value. | 40 | value. |
58 | The conversion is done according to base 10. | ||
59 | Negative values may be obtained by casting the result. | 41 | Negative values may be obtained by casting the result. |
42 | The | ||
43 | .Fn strtonum | ||
44 | function was designed to facilitate safe, robust programming | ||
45 | and overcome the shortcomings of the | ||
46 | .Xr atoi 3 | ||
47 | and | ||
48 | .Xr strtol 3 | ||
49 | family of interfaces. | ||
60 | .Pp | 50 | .Pp |
61 | The string may begin with an arbitrary amount of whitespace | 51 | The 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 |
70 | The remainder of the string is converted to an | 60 | The remainder of the string is converted to an |
71 | .Li unsigned long long | 61 | .Li unsigned long long |
72 | value in the obvious manner, | 62 | value according to base 10. |
73 | stopping at the first character which is not a valid digit | ||
74 | in the given base. | ||
75 | .Pp | 63 | .Pp |
76 | The value obtained is then checked against the provided | 64 | The value obtained is then checked against the provided |
77 | .Fa minval | 65 | .Fa minval |
@@ -89,14 +77,41 @@ indicating the failure. | |||
89 | The | 77 | The |
90 | .Fn strtonum | 78 | .Fn strtonum |
91 | function returns the result of the conversion, | 79 | function returns the result of the conversion, |
92 | unless the value would underflow or overflow. | 80 | unless the value would exceed the provided bounds or is invalid. |
93 | On error, 0 is returned. | 81 | On error, 0 is returned and |
82 | .Fa errstr | ||
83 | will point to an error message. | ||
84 | .Sh EXAMPLES | ||
85 | Using | ||
86 | .Fn strtonum | ||
87 | correctly is meant to be simpler than the alternative functions. | ||
88 | .Bd -literal -offset indent | ||
89 | int iterations; | ||
90 | const char *errstr; | ||
91 | |||
92 | iterations = strtonum(optarg, 1, 64, &errstr); | ||
93 | if (errstr) | ||
94 | errx(1, "number of iterations is %s: %s", errstr, optarg); | ||
95 | .Ed | ||
96 | .Pp | ||
97 | The above example will guarantee that the value of iterations is between | ||
98 | 1 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 |
97 | The given string was out of range. | 102 | The given string was out of range. |
98 | .It Bq Er EINVAL | 103 | .It Bq Er EINVAL |
99 | The given string did not consist solely of a valid number. | 104 | The given string did not consist solely of digit characters. |
105 | .El | ||
106 | .Pp | ||
107 | If an error occurs, errstr will be set to one of the following strings. | ||
108 | .Bl -tag -width "too large" | ||
109 | .It "too large" | ||
110 | The result was larger than the provided maximum value. | ||
111 | .It "too small" | ||
112 | The result was smaller than the provided minimum value. | ||
113 | .It "invalid" | ||
114 | The 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. | |||
112 | is an | 127 | is an |
113 | .Ox | 128 | .Ox |
114 | extension. | 129 | extension. |
130 | The existing alternatives, such as | ||
131 | .Xr atoi 3 | ||
132 | and | ||
133 | .Xr strtol 3 | ||
134 | are either impossible or difficult to use safely. | ||