nfs, smb: rename 'lfaware' to 'flags'
[unleashed.git] / lib / libc / strtonum.3
blob43df0edc78fec451aa769c98f6312c5e4249537a
1 .\" $OpenBSD: strtonum.3,v 1.18 2016/02/07 20:50:24 mmcc Exp $
2 .\"
3 .\" Copyright (c) 2004 Ted Unangst
4 .\"
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.
8 .\"
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.
16 .\"
17 .Dd $Mdocdate: February 7 2016 $
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
33 The
34 .Fn strtonum
35 function converts the string in
36 .Fa nptr
37 to a
38 .Li long long
39 value.
40 The
41 .Fn strtonum
42 function was designed to facilitate safe, robust programming
43 and overcome the shortcomings of the
44 .Xr atoi 3
45 and
46 .Xr strtol 3
47 family of interfaces.
48 .Pp
49 The string may begin with an arbitrary amount of whitespace
50 (as determined by
51 .Xr isspace 3 )
52 followed by a single optional
53 .Ql +
55 .Ql -
56 sign.
57 .Pp
58 The remainder of the string is converted to a
59 .Li long long
60 value according to base 10.
61 .Pp
62 The value obtained is then checked against the provided
63 .Fa minval
64 and
65 .Fa maxval
66 bounds.
68 .Fa errstr
69 is non-null,
70 .Fn strtonum
71 stores an error string in
72 .Fa *errstr
73 indicating the failure.
74 .Sh RETURN VALUES
75 The
76 .Fn strtonum
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,
80 .Va errno
81 is set, and
82 .Fa errstr
83 will point to an error message.
84 .Fa *errstr
85 will be set to
86 .Dv NULL
87 on success;
88 this fact can be used to differentiate
89 a successful return of 0 from an error.
90 .Sh EXAMPLES
91 Using
92 .Fn strtonum
93 correctly is meant to be simpler than the alternative functions.
94 .Bd -literal -offset indent
95 int iterations;
96 const char *errstr;
98 iterations = strtonum(optarg, 1, 64, &errstr);
99 if (errstr != NULL)
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).
105 .Sh ERRORS
106 .Bl -tag -width Er
107 .It Bq Er ERANGE
108 The given string was out of range.
109 .It Bq Er EINVAL
110 The given string did not consist solely of digit characters.
111 .It Bq Er EINVAL
112 .Ar minval
113 was larger than
114 .Ar maxval .
117 If an error occurs,
118 .Fa errstr
119 will be set to one of the following strings:
121 .Bl -tag -width "too largeXX" -compact
122 .It Qq too large
123 The result was larger than the provided maximum value.
124 .It Qq too small
125 The result was smaller than the provided minimum value.
126 .It Qq invalid
127 The string did not consist solely of digit characters.
129 .Sh SEE ALSO
130 .Xr atof 3 ,
131 .Xr atoi 3 ,
132 .Xr atol 3 ,
133 .Xr atoll 3 ,
134 .Xr sscanf 3 ,
135 .Xr strtod 3 ,
136 .Xr strtol 3 ,
137 .Xr strtoul 3
138 .Sh STANDARDS
139 .Fn strtonum
140 is an
142 extension.
143 The existing alternatives, such as
144 .Xr atoi 3
146 .Xr strtol 3 ,
147 are either impossible or difficult to use safely.
148 .Sh HISTORY
150 .Fn strtonum
151 function first appeared in
152 .Ox 3.6 .