]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdlib/strtonum.3
MFV: expat 2.6.0.
[FreeBSD/FreeBSD.git] / lib / libc / stdlib / strtonum.3
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
33 The
34 .Fn strtonum
35 function converts the string in
36 .Fa nptr
37 to a
38 .Vt "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 +
54 or
55 .Ql -
56 sign.
57 .Pp
58 The remainder of the string is converted to a
59 .Vt "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.
67 If
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 On success,
85 .Fa *errstr
86 will be set to
87 .Dv NULL ;
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;
97
98 iterations = strtonum(optarg, 1, 64, &errstr);
99 if (errstr != NULL)
100         errx(1, "number of iterations is %s: %s", errstr, optarg);
101 .Ed
102 .Pp
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 The supplied
113 .Fa minval
114 was larger than
115 .Fa maxval .
116 .El
117 .Pp
118 If an error occurs,
119 .Fa errstr
120 will be set to one of the following strings:
121 .Pp
122 .Bl -tag -width ".Li too large" -compact
123 .It Li "too large"
124 The result was larger than the provided maximum value.
125 .It Li "too small"
126 The result was smaller than the provided minimum value.
127 .It Li invalid
128 The 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
140 The
141 .Fn strtonum
142 function is a
143 .Bx
144 extension.
145 The existing alternatives, such as
146 .Xr atoi 3
147 and
148 .Xr strtol 3 ,
149 are either impossible or difficult to use safely.
150 .Sh HISTORY
151 The
152 .Fn strtonum
153 function first appeared in
154 .Ox 3.6 .