1 .\" $OpenBSD: strtonum.3,v 1.18 2016/02/07 20:50:24 mmcc Exp $
3 .\" Copyright (c) 2004 Ted Unangst
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .Dd $Mdocdate: February 7 2016 $
22 .Nd reliably convert string value to an integer
27 .Fa "const char *nptr"
28 .Fa "long long minval"
29 .Fa "long long maxval"
30 .Fa "const char **errstr"
35 function converts the string in
42 function was designed to facilitate safe, robust programming
43 and overcome the shortcomings of the
49 The string may begin with an arbitrary amount of whitespace
52 followed by a single optional
58 The remainder of the string is converted to a
60 value according to base 10.
62 The value obtained is then checked against the provided
71 stores an error string in
73 indicating the failure.
77 function returns the result of the conversion,
78 unless the value would exceed the provided bounds or is invalid.
79 On error, 0 is returned,
83 will point to an error message.
88 this fact can be used to differentiate
89 a successful return of 0 from an error.
93 correctly is meant to be simpler than the alternative functions.
94 .Bd -literal -offset indent
98 iterations = strtonum(optarg, 1, 64, &errstr);
100 errx(1, "number of iterations is %s: %s", errstr, optarg);
103 The above example will guarantee that the value of iterations is between
104 1 and 64 (inclusive).
108 The given string was out of range.
110 The given string did not consist solely of digit characters.
119 will be set to one of the following strings:
121 .Bl -tag -width "too largeXX" -compact
123 The result was larger than the provided maximum value.
125 The result was smaller than the provided minimum value.
127 The string did not consist solely of digit characters.
143 The existing alternatives, such as
147 are either impossible or difficult to use safely.
151 function first appeared in