xref: /netbsd-src/lib/libc/stdlib/strtoi.3 (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1.\"	$NetBSD: strtoi.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     from: @(#)strtol.3	8.1 (Berkeley) 6/4/93
35.\"
36.\" Created by Kamil Rytarowski, based on ID:
37.\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp
38.\"
39.Dd November 13, 2015
40.Dt STRTOI 3
41.Os
42.Sh NAME
43.Nm strtoi
44.Nd convert string value to an intmax_t integer
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In inttypes.h
49.Ft intmax_t
50.Fo strtoi
51.Fa "const char * restrict nptr"
52.Fa "char ** restrict endptr"
53.Fa "int base"
54.Fa "intmax_t lo"
55.Fa "intmax_t hi"
56.Fa "int *rstatus"
57.Fc
58.Sh DESCRIPTION
59The
60.Fn strtoi
61function
62converts the string in
63.Fa nptr
64to an
65.Ft intmax_t
66value.
67The
68.Fn strtoi
69function uses internally
70.Xr strtoimax 3
71and ensures that the result is always in the range [
72.Fa lo ..
73.Fa hi
74].
75In adddition it always places
76.Dv 0
77on success or a conversion status in the
78.Fa rstatus
79argument, avoiding the
80.Dv errno
81gymnastics the other functions require.
82The
83.Fa rstatus
84argument can be
85.Dv NULL
86if conversion status is to be ignored.
87.Pp
88The string may begin with an arbitrary amount of white space
89(as determined by
90.Xr isspace 3 )
91followed by a single optional
92.Ql +
93or
94.Ql -
95sign.
96If
97.Fa base
98is zero or 16,
99the string may then include a
100.Ql 0x
101or
102.Ql 0X
103prefix,
104and the number will be read in base 16; otherwise,
105.\" if the
106.\" .Fa base
107.\" is zero or 2,
108.\" the string may then include a
109.\" .Ql 0b
110.\" or
111.\" .Ql 0B
112.\" prefix,
113.\" and the number will be read in base 2; otherwise,
114a zero
115.Fa base
116is taken as 10 (decimal) unless the next character is
117.Ql 0 ,
118in which case it is taken as 8 (octal).
119.Pp
120The remainder of the string is converted to a
121.Em intmax_t
122value in the obvious manner,
123stopping at the first character which is not a valid digit
124in the given base.
125(In bases above 10, the letter
126.Ql A
127in either upper or lower case
128represents 10,
129.Ql B
130represents 11, and so forth, with
131.Ql Z
132representing 35.)
133.Pp
134If
135.Fa endptr
136is non-nil,
137.Fn strtoi
138stores the address of the first invalid character in
139.Fa *endptr .
140If there were no digits at all, however,
141.Fn strtoi
142stores the original value of
143.Fa nptr
144in
145.Fa *endptr .
146(Thus, if
147.Fa *nptr
148is not
149.Ql \e0
150but
151.Fa **endptr
152is
153.Ql \e0
154on return, the entire string was valid.)
155.Sh RETURN VALUES
156The
157.Fn strtoi
158function
159always returns the closest value in the range specified by
160the
161.Fa lo
162and
163.Fa hi
164arguments.
165.Pp
166The
167.Va errno
168value is guaranteed to be left unchanged.
169.Pp
170Errors are stored as the conversion status in the
171.Fa rstatus
172argument.
173.Sh EXAMPLES
174The following example will always return a number in
175.Dv [1..99]
176range no matter what the input is, and warn if the conversion failed.
177.Bd -literal -offset indent
178int e;
179intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e);
180if (e)
181	warnc(e, "conversion of `%s' to a number failed, using %jd",
182	    buf, lval);
183.Ed
184.Sh ERRORS
185.Bl -tag -width Er
186.It Bq Er ECANCELED
187The string did not contain any characters that were converted.
188.It Bq Er EINVAL
189The
190.Ar base
191is not between 2 and 36 and does not contain the special value 0.
192.It Bq Er ENOTSUP
193The string contained non-numeric characters that did not get converted.
194In this case,
195.Fa endptr
196points to the first unconverted character.
197.It Bq Er ERANGE
198The given string was out of range; the value converted has been clamped;
199or the range given was invalid, i.e.
200.Fa lo
201>
202.Fa hi .
203.El
204.Sh SEE ALSO
205.Xr atof 3 ,
206.Xr atoi 3 ,
207.Xr atol 3 ,
208.Xr atoll 3 ,
209.Xr strtod 3 ,
210.Xr strtoimax 3 ,
211.Xr strtol 3 ,
212.Xr strtoll 3 ,
213.Xr strtou 3 ,
214.Xr strtoul 3 ,
215.Xr strtoull 3 ,
216.Xr strtoumax 3
217.Sh STANDARDS
218The
219.Fn strtoi
220function is a
221.Nx
222extension.
223.Sh HISTORY
224The
225.Fn strtoi
226function first appeared in
227.Nx 7 .
228.Ox
229introduced the
230.Fn strtonum 3
231function for the same purpose, but the interface makes it impossible to
232properly differentiate illegal returns.
233.Sh BUGS
234Ignores the current locale.
235