xref: /freebsd/lib/libc/stdlib/strtonum.3 (revision 61e21613)
1.\" Copyright (c) 2004 Ted Unangst
2.\"
3.\" Permission to use, copy, modify, and distribute this software for any
4.\" purpose with or without fee is hereby granted, provided that the above
5.\" copyright notice and this permission notice appear in all copies.
6.\"
7.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14.\"
15.\" $OpenBSD: strtonum.3,v 1.13 2006/04/25 05:15:42 tedu Exp $
16.\"
17.Dd April 29, 2004
18.Dt STRTONUM 3
19.Os
20.Sh NAME
21.Nm strtonum
22.Nd "reliably convert string value to an integer"
23.Sh SYNOPSIS
24.In stdlib.h
25.Ft long long
26.Fo strtonum
27.Fa "const char *nptr"
28.Fa "long long minval"
29.Fa "long long maxval"
30.Fa "const char **errstr"
31.Fc
32.Sh DESCRIPTION
33The
34.Fn strtonum
35function converts the string in
36.Fa nptr
37to a
38.Vt "long long"
39value.
40The
41.Fn strtonum
42function was designed to facilitate safe, robust programming
43and overcome the shortcomings of the
44.Xr atoi 3
45and
46.Xr strtol 3
47family of interfaces.
48.Pp
49The string may begin with an arbitrary amount of whitespace
50(as determined by
51.Xr isspace 3 )
52followed by a single optional
53.Ql +
54or
55.Ql -
56sign.
57.Pp
58The remainder of the string is converted to a
59.Vt "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.
84On success,
85.Fa *errstr
86will be set to
87.Dv NULL ;
88this fact can be used to differentiate
89a successful return of 0 from an error.
90.Sh EXAMPLES
91Using
92.Fn strtonum
93correctly is meant to be simpler than the alternative functions.
94.Bd -literal -offset indent
95int iterations;
96const char *errstr;
97
98iterations = strtonum(optarg, 1, 64, &errstr);
99if (errstr != NULL)
100	errx(1, "number of iterations is %s: %s", errstr, optarg);
101.Ed
102.Pp
103The above example will guarantee that the value of iterations is between
1041 and 64 (inclusive).
105.Sh ERRORS
106.Bl -tag -width Er
107.It Bq Er ERANGE
108The given string was out of range.
109.It Bq Er EINVAL
110The given string did not consist solely of digit characters.
111.It Bq Er EINVAL
112The supplied
113.Fa minval
114was larger than
115.Fa maxval .
116.El
117.Pp
118If an error occurs,
119.Fa errstr
120will be set to one of the following strings:
121.Pp
122.Bl -tag -width ".Li too large" -compact
123.It Li "too large"
124The result was larger than the provided maximum value.
125.It Li "too small"
126The result was smaller than the provided minimum value.
127.It Li invalid
128The string did not consist solely of digit characters.
129.El
130.Sh SEE ALSO
131.Xr atof 3 ,
132.Xr atoi 3 ,
133.Xr atol 3 ,
134.Xr atoll 3 ,
135.Xr sscanf 3 ,
136.Xr strtod 3 ,
137.Xr strtol 3 ,
138.Xr strtoul 3
139.Sh STANDARDS
140The
141.Fn strtonum
142function is a
143.Bx
144extension.
145The existing alternatives, such as
146.Xr atoi 3
147and
148.Xr strtol 3 ,
149are either impossible or difficult to use safely.
150.Sh HISTORY
151The
152.Fn strtonum
153function first appeared in
154.Ox 3.6 .
155