xref: /netbsd-src/lib/libc/stdlib/strtonum.3 (revision 194f011f43c9d4da6dad3d29585a591ae3ed1ea2)
1*194f011fSwiz.\" $NetBSD: strtonum.3,v 1.2 2015/01/19 11:47:41 wiz Exp $
2566fb076Schristos.\" $OpenBSD: strtonum.3,v 1.17 2013/08/14 06:32:28 jmc Exp $
3566fb076Schristos.\"
4566fb076Schristos.\" Copyright (c) 2004 Ted Unangst
5566fb076Schristos.\"
6566fb076Schristos.\" Permission to use, copy, modify, and distribute this software for any
7566fb076Schristos.\" purpose with or without fee is hereby granted, provided that the above
8566fb076Schristos.\" copyright notice and this permission notice appear in all copies.
9566fb076Schristos.\"
10566fb076Schristos.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11566fb076Schristos.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12566fb076Schristos.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13566fb076Schristos.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14566fb076Schristos.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15566fb076Schristos.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16566fb076Schristos.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17566fb076Schristos.\"
18*194f011fSwiz.Dd January 18, 2015
19566fb076Schristos.Dt STRTONUM 3
20566fb076Schristos.Os
21566fb076Schristos.Sh NAME
22566fb076Schristos.Nm strtonum
23566fb076Schristos.Nd reliably convert string value to an integer
24566fb076Schristos.Sh SYNOPSIS
25566fb076Schristos.Vt #define _OPENBSD_SOURCE
26566fb076Schristos.In stdlib.h
27566fb076Schristos.Ft long long
28566fb076Schristos.Fo strtonum
29566fb076Schristos.Fa "const char *nptr"
30566fb076Schristos.Fa "long long minval"
31566fb076Schristos.Fa "long long maxval"
32566fb076Schristos.Fa "const char **errstr"
33566fb076Schristos.Fc
34566fb076Schristos.Sh DESCRIPTION
35566fb076SchristosThe
36566fb076Schristos.Fn strtonum
37566fb076Schristosfunction converts the string in
38566fb076Schristos.Fa nptr
39566fb076Schristosto a
40566fb076Schristos.Li long long
41566fb076Schristosvalue.
42566fb076Schristos.Pp
43566fb076SchristosThe string may begin with an arbitrary amount of whitespace
44566fb076Schristos(as determined by
45566fb076Schristos.Xr isspace 3 )
46566fb076Schristosfollowed by a single optional
47566fb076Schristos.Ql +
48566fb076Schristosor
49566fb076Schristos.Ql -
50566fb076Schristossign.
51566fb076Schristos.Pp
52566fb076SchristosThe remainder of the string is converted to a
53566fb076Schristos.Li long long
54566fb076Schristosvalue according to base 10.
55566fb076Schristos.Pp
56566fb076SchristosThe value obtained is then checked against the provided
57566fb076Schristos.Fa minval
58566fb076Schristosand
59566fb076Schristos.Fa maxval
60566fb076Schristosbounds.
61566fb076SchristosIf
62566fb076Schristos.Fa errstr
63566fb076Schristosis non-null,
64566fb076Schristos.Fn strtonum
65566fb076Schristosstores an error string in
66566fb076Schristos.Fa *errstr
67566fb076Schristosindicating the failure.
68566fb076Schristos.Sh RETURN VALUES
69566fb076SchristosThe
70566fb076Schristos.Fn strtonum
71566fb076Schristosfunction returns the result of the conversion,
72566fb076Schristosunless the value would exceed the provided bounds or is invalid.
73566fb076SchristosOn error, 0 is returned,
74566fb076Schristos.Va errno
75566fb076Schristosis set, and
76566fb076Schristos.Fa errstr
77566fb076Schristoswill point to an error message.
78566fb076Schristos.Fa *errstr
79566fb076Schristoswill be set to
80566fb076Schristos.Dv NULL
81566fb076Schristoson success;
82566fb076Schristosthis fact can be used to differentiate
83566fb076Schristosa successful return of 0 from an error.
84566fb076Schristos.Sh EXAMPLES
85566fb076SchristosUsing
86566fb076Schristos.Fn strtonum
87566fb076Schristoscorrectly is meant to be simpler than the alternative functions.
88566fb076Schristos.Bd -literal -offset indent
89566fb076Schristosint iterations;
90566fb076Schristosconst char *errstr;
91566fb076Schristos
92566fb076Schristositerations = strtonum(optarg, 1, 64, &errstr);
93566fb076Schristosif (errstr)
94566fb076Schristos	errx(1, "number of iterations is %s: %s", errstr, optarg);
95566fb076Schristos.Ed
96566fb076Schristos.Pp
97566fb076SchristosThe above example will guarantee that the value of iterations is between
98566fb076Schristos1 and 64 (inclusive).
99566fb076Schristos.Sh ERRORS
100566fb076Schristos.Bl -tag -width Er
101566fb076Schristos.It Bq Er EINVAL
102*194f011fSwizThe given string did not consist solely of digit characters; or
103566fb076Schristos.Ar minval
104566fb076Schristoswas larger than
105566fb076Schristos.Ar maxval .
106*194f011fSwiz.It Bq Er ERANGE
107*194f011fSwizThe given string was out of range.
108566fb076Schristos.El
109566fb076Schristos.Pp
110566fb076SchristosIf an error occurs,
111566fb076Schristos.Fa errstr
112566fb076Schristoswill be set to one of the following strings:
113566fb076Schristos.Pp
114566fb076Schristos.Bl -tag -width "too largeXX" -compact
115566fb076Schristos.It Qq too large
116566fb076SchristosThe result was larger than the provided maximum value.
117566fb076Schristos.It Qq too small
118566fb076SchristosThe result was smaller than the provided minimum value.
119566fb076Schristos.It Qq invalid
120566fb076SchristosThe string did not consist solely of digit characters.
121566fb076Schristos.El
122566fb076Schristos.Sh SEE ALSO
123566fb076Schristos.Xr atof 3 ,
124566fb076Schristos.Xr atoi 3 ,
125566fb076Schristos.Xr atol 3 ,
126566fb076Schristos.Xr atoll 3 ,
127566fb076Schristos.Xr sscanf 3 ,
128566fb076Schristos.Xr strtod 3 ,
129*194f011fSwiz.Xr strtoi 3 ,
130566fb076Schristos.Xr strtol 3 ,
131566fb076Schristos.Xr strtoll 3 ,
132566fb076Schristos.Xr strtou 3 ,
133*194f011fSwiz.Xr strtoul 3 ,
134566fb076Schristos.Xr strtoull 3
135566fb076Schristos.Sh STANDARDS
136566fb076Schristos.Fn strtonum
137566fb076Schristosis an
138566fb076Schristos.Ox
139566fb076Schristosextension.
140566fb076Schristos.Sh HISTORY
141566fb076SchristosThe
142566fb076Schristos.Fn strtonum
143566fb076Schristosfunction first appeared in
144566fb076Schristos.Ox 3.6 .
145566fb076Schristos.Fn strtonum
146566fb076Schristoswas redesigned in
147566fb076Schristos.Nx 8
148566fb076Schristosas
149566fb076Schristos.Fn strtoi 3
150566fb076Schristosand
151566fb076Schristos.Fn strtou 3 .
152566fb076SchristosFor compatibility reasons it's available since
153566fb076Schristos.Nx 8
154566fb076Schristosin the
155566fb076Schristos.Vt _OPENBSD_SOURCE
156566fb076Schristosnamespace.
157*194f011fSwiz.Sh CAVEATS
158*194f011fSwizThe
159*194f011fSwiz.Fn strtonum
160*194f011fSwizfunction was designed to facilitate safe,
161*194f011fSwizrobust programming and overcome the shortcomings of the
162*194f011fSwiz.Xr atoi 3
163*194f011fSwizand
164*194f011fSwiz.Xr strtol 3
165*194f011fSwizfamily of interfaces, however there are problems with the
166*194f011fSwiz.Fn strtonum
167*194f011fSwizAPI:
168*194f011fSwiz.Bl -dash
169*194f011fSwiz.It
170*194f011fSwizwill return 0 on failure; 0 might not be in range, so that necessitates
171*194f011fSwizan error check even if you want to avoid it
172*194f011fSwiz.It
173*194f011fSwizdoes not differentiate 'illegal' returns, so we can't tell the
174*194f011fSwizdifference between partial and no conversions
175*194f011fSwiz.It
176*194f011fSwizreturns english strings
177*194f011fSwiz.It
178*194f011fSwizcan't set the base, or find where the conversion ended
179*194f011fSwiz.It
180*194f011fSwizhardcodes long long integer type
181*194f011fSwiz.El
182*194f011fSwizTo overcome the shortcomings of
183*194f011fSwiz.Fn strtonum
184*194f011fSwiz.Nx
185*194f011fSwizprovides
186*194f011fSwiz.Fn strtou 3
187*194f011fSwizand
188*194f011fSwiz.Fn strtoi 3 .
189