xref: /illumos-gate/usr/src/man/man3c/strtonum.3c (revision 069e6b7e31ba5dcbc5441b98af272714d9a5455c)
1.\"
2.\" Copyright (c) 2004 Ted Unangst
3.\"
4.\" Permission to use, copy, modify, and distribute this software for any
5.\" purpose with or without fee is hereby granted, provided that the above
6.\" copyright notice and this permission notice appear in all copies.
7.\"
8.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15.\"
16.Dd $Mdocdate: February 7 2016 $
17.Dt STRTONUM 3C
18.Os
19.Sh NAME
20.Nm strtonum
21.Nd reliably convert string value to an integer
22.Sh LIBRARY
23.Lb libc
24.Sh SYNOPSIS
25.In stdlib.h
26.Ft long long
27.Fo strtonum
28.Fa "const char *nptr"
29.Fa "long long minval"
30.Fa "long long maxval"
31.Fa "const char **errstr"
32.Fc
33.Sh DESCRIPTION
34The
35.Fn strtonum
36function converts the string in
37.Fa nptr
38to a
39.Li long long
40value.
41The
42.Fn strtonum
43function was designed to facilitate safe, robust programming
44and overcome the shortcomings of the
45.Xr atoi 3C
46and
47.Xr strtol 3C
48family of interfaces.
49.Pp
50The string may begin with an arbitrary amount of whitespace
51.Pq as determined by Xr isspace 3C
52followed by a single optional
53.Ql +
54or
55.Ql -
56sign.
57.Pp
58The remainder of the string is converted to a
59.Li long long
60value according to base 10.
61.Pp
62The value obtained is then checked against the provided
63.Fa minval
64and
65.Fa maxval
66bounds.
67If
68.Fa errstr
69is non-null,
70.Fn strtonum
71stores an error string in
72.Fa errstr
73indicating the failure.
74.Sh RETURN VALUES
75The
76.Fn strtonum
77function returns the result of the conversion,
78unless the value would exceed the provided bounds or is invalid.
79On error, 0 is returned,
80.Va errno
81is set, and
82.Fa errstr
83will point to an error message.
84.Fa errstr
85will be set to
86.Dv NULL
87on success; this fact can be used to differentiate a successful return of 0 from
88an error.
89.Sh EXAMPLES
90Using
91.Fn strtonum
92correctly is meant to be simpler than the alternative functions.
93.Bd -literal -offset indent
94int iterations;
95const char *errstr;
96
97iterations = strtonum(optarg, 1, 64, &errstr);
98if (errstr != NULL)
99	errx(1, "number of iterations is %s: %s", errstr, optarg);
100.Ed
101.Pp
102The above example will guarantee that the value of iterations is between
1031 and 64
104.Pq inclusive .
105.Sh ERRORS
106.Bl -tag -width Er
107.It Er ERANGE
108The given string was out of range.
109.It Er EINVAL
110The given string did not consist solely of digit characters.
111.It Er EINVAL
112.Ar minval
113was larger than
114.Ar maxval .
115.El
116.Pp
117If an error occurs,
118.Fa errstr
119will be set to one of the following strings:
120.Pp
121.Bl -tag -width "too largeXX" -compact
122.It Qq too large
123The result was larger than the provided maximum value.
124.It Qq too small
125The result was smaller than the provided minimum value.
126.It Qq invalid
127The string did not consist solely of digit characters.
128.El
129.Sh INTERFACE STABILITY
130.Sy Committed .
131.Sh MT-LEVEL
132.Sy Safe .
133.Sh SEE ALSO
134.Xr atof 3C ,
135.Xr atoi 3C ,
136.Xr atol 3C ,
137.Xr atoll 3C ,
138.Xr sscanf 3C ,
139.Xr strtod 3C ,
140.Xr strtol 3C ,
141.Xr strtoul 3C
142.Sh STANDARDS
143.Fn strtonum
144is an
145.Ox
146extension.
147The existing alternatives, such as
148.Xr atoi 3C
149and
150.Xr strtol 3C ,
151are either impossible or difficult to use safely.
152