xref: /minix3/lib/libc/stdlib/strtoi.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1.\"	$NetBSD: strtoi.3,v 1.3 2015/07/11 15:50:56 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 April 30, 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
101prefix,
102and the number will be read in base 16; otherwise, a zero
103.Fa base
104is taken as 10 (decimal) unless the next character is
105.Ql 0 ,
106in which case it is taken as 8 (octal).
107.Pp
108The remainder of the string is converted to a
109.Em intmax_t
110value in the obvious manner,
111stopping at the first character which is not a valid digit
112in the given base.
113(In bases above 10, the letter
114.Ql A
115in either upper or lower case
116represents 10,
117.Ql B
118represents 11, and so forth, with
119.Ql Z
120representing 35.)
121.Pp
122If
123.Fa endptr
124is non-nil,
125.Fn strtoi
126stores the address of the first invalid character in
127.Fa *endptr .
128If there were no digits at all, however,
129.Fn strtoi
130stores the original value of
131.Fa nptr
132in
133.Fa *endptr .
134(Thus, if
135.Fa *nptr
136is not
137.Ql \e0
138but
139.Fa **endptr
140is
141.Ql \e0
142on return, the entire string was valid.)
143.Sh RETURN VALUES
144The
145.Fn strtoi
146function
147always returns the closest value in the range specified by
148the
149.Fa lo
150and
151.Fa hi
152arguments.
153.Pp
154The
155.Va errno
156value is guaranteed to be left unchanged.
157.Pp
158Errors are stored as the conversion status in the
159.Fa rstatus
160argument.
161.Sh EXAMPLES
162The following example will always return a number in
163.Dv [1..99]
164range no matter what the input is, and warn if the conversion failed.
165.Bd -literal -offset indent
166int e;
167intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e);
168if (e)
169	warnc(e, "conversion of `%s' to a number failed, using %jd",
170	    buf, lval);
171.Ed
172.Sh ERRORS
173.Bl -tag -width Er
174.It Bq Er ECANCELED
175The string did not contain any characters that were converted.
176.It Bq Er EINVAL
177The
178.Ar base
179is not between 2 and 36 and does not contain the special value 0.
180.It Bq Er ENOTSUP
181The string contained non-numeric characters that did not get converted.
182In this case,
183.Fa endptr
184points to the first unconverted character.
185.It Bq Er ERANGE
186The given string was out of range; the value converted has been clamped;
187or the range given was invalid, i.e.
188.Fa lo
189\*[Gt]
190.Fa hi .
191.El
192.Sh SEE ALSO
193.Xr atof 3 ,
194.Xr atoi 3 ,
195.Xr atol 3 ,
196.Xr atoll 3 ,
197.Xr strtod 3 ,
198.Xr strtoimax 3 ,
199.Xr strtol 3 ,
200.Xr strtoll 3 ,
201.Xr strtou 3 ,
202.Xr strtoul 3 ,
203.Xr strtoull 3 ,
204.Xr strtoumax 3
205.Sh STANDARDS
206The
207.Fn strtoi
208function is a
209.Nx
210extension.
211.Sh HISTORY
212The
213.Fn strtoi
214function first appeared in
215.Nx 7 .
216.Ox
217introduced the
218.Fn strtonum 3
219function for the same purpose, but the interface makes it impossible to
220properly differentiate illegal returns.
221.Sh BUGS
222Ignores the current locale.
223