1 .\" $NetBSD: strtonum.3,v 1.2 2015/01/19 11:47:41 wiz Exp $
2 .\" $OpenBSD: strtonum.3,v 1.17 2013/08/14 06:32:28 jmc Exp $
4 .\" Copyright (c) 2004 Ted Unangst
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 .Nd reliably convert string value to an integer
25 .Vt #define _OPENBSD_SOURCE
29 .Fa "const char *nptr"
30 .Fa "long long minval"
31 .Fa "long long maxval"
32 .Fa "const char **errstr"
37 function converts the string in
43 The string may begin with an arbitrary amount of whitespace
46 followed by a single optional
52 The remainder of the string is converted to a
54 value according to base 10.
56 The value obtained is then checked against the provided
65 stores an error string in
67 indicating the failure.
71 function returns the result of the conversion,
72 unless the value would exceed the provided bounds or is invalid.
73 On error, 0 is returned,
77 will point to an error message.
82 this fact can be used to differentiate
83 a successful return of 0 from an error.
87 correctly is meant to be simpler than the alternative functions.
88 .Bd -literal -offset indent
92 iterations = strtonum(optarg, 1, 64, &errstr);
94 errx(1, "number of iterations is %s: %s", errstr, optarg);
97 The above example will guarantee that the value of iterations is between
102 The given string did not consist solely of digit characters; or
107 The given string was out of range.
112 will be set to one of the following strings:
114 .Bl -tag -width "too largeXX" -compact
116 The result was larger than the provided maximum value.
118 The result was smaller than the provided minimum value.
120 The string did not consist solely of digit characters.
143 function first appeared in
152 For compatibility reasons it's available since
160 function was designed to facilitate safe,
161 robust programming and overcome the shortcomings of the
165 family of interfaces, however there are problems with the
170 will return 0 on failure; 0 might not be in range, so that necessitates
171 an error check even if you want to avoid it
173 does not differentiate 'illegal' returns, so we can't tell the
174 difference between partial and no conversions
176 returns english strings
178 can't set the base, or find where the conversion ended
180 hardcodes long long integer type
182 To overcome the shortcomings of