1.\" $NetBSD: strtoi.3,v 1.10 2024/02/10 18:43:51 andvar 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 January 20, 2024 40.Dt STRTOI 3 41.Os 42.Sh NAME 43.Nm strtoi 44.Nd convert a 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 converts the string in 62.Fa nptr 63to an 64.Ft intmax_t 65value. 66The 67.Fn strtoi 68function uses internally 69.Xr strtoimax 3 70and ensures that the result is always in the range [ 71.Fa lo .. 72.Fa hi 73]. 74In addition it always places 75.Dv 0 76on success or a conversion status in the 77.Fa rstatus 78argument, avoiding the 79.Dv errno 80gymnastics the other functions require. 81The 82.Fa rstatus 83argument can be 84.Dv NULL 85if conversion status is to be ignored. 86.Pp 87The string may begin with an arbitrary amount of white space 88(as determined by 89.Xr isspace 3 ) 90followed by a single optional 91.Ql + 92or 93.Ql - 94sign. 95If 96.Fa base 97is zero or 16, 98the string may then include a 99.Ql 0x 100or 101.Ql 0X 102prefix, 103and the number will be read in base 16; otherwise, 104.\" if the 105.\" .Fa base 106.\" is zero or 2, 107.\" the string may then include a 108.\" .Ql 0b 109.\" or 110.\" .Ql 0B 111.\" prefix, 112.\" and the number will be read in base 2; otherwise, 113a zero 114.Fa base 115is taken as 10 (decimal) unless the next character is 116.Ql 0 , 117in which case it is taken as 8 (octal). 118.Pp 119The remainder of the string is converted to an 120.Em intmax_t 121value in the obvious manner, 122stopping at the end of the string 123or 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.Pp 205The range check is more important than the unconverted characters check, 206and it is performed first. 207If a program needs to know if there were unconverted characters when an 208out of range number has been provided, it needs to supply and test 209.Fa endptr. 210.Sh SEE ALSO 211.Xr atof 3 , 212.Xr atoi 3 , 213.Xr atol 3 , 214.Xr atoll 3 , 215.Xr strtod 3 , 216.Xr strtoimax 3 , 217.Xr strtol 3 , 218.Xr strtoll 3 , 219.Xr strtou 3 , 220.Xr strtoul 3 , 221.Xr strtoull 3 , 222.Xr strtoumax 3 223.Sh STANDARDS 224The 225.Fn strtoi 226function is a 227.Nx 228extension. 229.Sh HISTORY 230The 231.Fn strtoi 232function first appeared in 233.Nx 7 . 234.Ox 235introduced the 236.Fn strtonum 3 237function for the same purpose, but the interface makes it impossible to 238properly differentiate illegal returns. 239.Sh BUGS 240Ignores the current locale. 241