1.\" $NetBSD: strtoi.3,v 1.11 2024/07/24 08:55:08 kre 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 July 24, 2024 40.Dt STRTOI 3 41.Os 42.Sh NAME 43.Nm strtoi , 44.Nm strtoi_l 45.Nd convert a string value to an intmax_t integer 46.Sh LIBRARY 47.Lb libc 48.Sh SYNOPSIS 49.In inttypes.h 50.Ft intmax_t 51.Fo strtoi 52.Fa "const char * restrict nptr" 53.Fa "char ** restrict endptr" 54.Fa "int base" 55.Fa "intmax_t lo" 56.Fa "intmax_t hi" 57.Fa "int *rstatus" 58.Fc 59.In locale.h 60.Ft intmax_t 61.Fo strtoi_l 62.Fa "const char * restrict nptr" 63.Fa "char ** restrict endptr" 64.Fa "int base" 65.Fa "intmax_t lo" 66.Fa "intmax_t hi" 67.Fa "int *rstatus" 68.Fa "locale_t loc" 69.Fc 70.Sh DESCRIPTION 71The 72.Fn strtoi 73generates the 74.Ft intmax_t 75result value equivalent to the numeric string in 76.Fa nptr . 77The 78.Fn strtoi 79function internally uses 80.Xr strtoimax 3 81and then ensures that the result is in the range 82.Bq Fa lo No .. Fa hi . 83In addition it places 84a conversion status indicator, 85.Dv 0 86if fully successful, 87in the integer addressed by the 88.Fa rstatus 89argument, if that is not NULL, allowing the 90.Dv errno 91gymnastics that other similar functions require to be avoided. 92The 93.Fa rstatus 94argument can be 95.Dv NULL 96if the conversion status is to be ignored. 97.Pp 98The operation of 99.Fn strtoi 100is unspecified if 101.Fa lo 102is greater than 103.Fa hi . 104.Pp 105The string may begin with an arbitrary amount of white space 106(as determined by 107.Xr isspace 3 ) 108followed by a single optional 109.Ql + 110or 111.Ql - 112sign. 113If 114.Fa base 115is zero or 16, 116the string may then include a 117.Ql 0x 118or 119.Ql 0X 120prefix, 121after which there must immediately follow at least one hexadecimal digit, 122and the number will be read in base 16; otherwise, 123.\" if the 124.\" .Fa base 125.\" is zero or 2, 126.\" the string may then include a 127.\" .Ql 0b 128.\" or 129.\" .Ql 0B 130.\" prefix, 131.\" and the number will be read in base 2; otherwise, 132a zero 133.Fa base 134is taken as 10 (decimal) unless the next character is 135.Ql 0 , 136in which case it is taken as 8 (octal). 137.Pp 138The remainder of the string is converted to the 139.Em intmax_t 140result in the obvious manner, 141stopping at the end of the string 142or at the first character which is not a valid digit 143in the given base. 144(In bases above 10, the letter 145.Ql A 146in either upper or lower case 147represents 10, 148.Ql B 149represents 11, and so forth, with 150.Ql Z 151representing 35.) 152.Pp 153If 154.Fa endptr 155is not NULL, 156.Fn strtoi 157stores the address of the first character after those 158which were converted in 159.Fa *endptr . 160If there were no digits at all, however, 161or if the 162.Fa base 163is invalid, 164.Fn strtoi 165stores the original value of 166.Fa nptr 167in 168.Fa *endptr . 169(Thus, if 170.Fa *nptr 171is not 172.Ql \e0 173but 174.Fa **endptr 175is 176.Ql \e0 177on return, the entire string was valid.) 178Note that converting an out of range value has no 179impact upon the value placed into 180.Fa *endptr . 181.Pp 182The 183.Fn strtoi_l 184function is identical, except uses the locale given by 185.Fa loc 186rather than the current locale, when determining what is white space to 187be skipped before the conversion begins. 188.Sh RETURN VALUES 189The 190.Fn strtoi 191function, 192returns the converted value, 193or the closest value in the range specified by the 194.Fa lo 195and 196.Fa hi 197arguments, if the value converted was outside that range. 198If 199.Fa lo 200is equal to 201.Fa hi 202and no overriding error occurs, 203that value will always be returned. 204.Pp 205The 206.Va errno 207value from 208.In errno.h , 209is guaranteed to be left unchanged. 210.Pp 211Errors are stored as the conversion status error indicator, 212taken from a subset of the values from 213.In errno.h 214in the 215.Fa rstatus 216argument, if that was not given as a NULL pointer. 217See the ERRORS section below for the possible values. 218.Sh EXAMPLES 219The following example will always return a number in 220.Dv [1..99] 221range no matter what the input is, and warn if the conversion failed. 222.Bd -literal -offset indent 223int e; 224intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e); 225if (e) 226 warnc(e, "conversion of `%s' to a number failed, using %jd", 227 buf, lval); 228.Ed 229.Sh ERRORS 230.Bl -tag -width Er 231.It Bq Er ECANCELED 232The string did not contain any characters that were converted. 233If given 234.Fa endptr 235will be set to 236.Fa nptr . 237.It Bq Er EINVAL 238The 239.Ar base 240is not between 2 and 36 and nor is it the special value 0. 241If given 242.Fa endptr 243will be set to 244.Fa nptr . 245.It Bq Er ENOTSUP 246The string contained non-numeric characters that did not get converted. 247In this case, 248.Fa endptr 249points to the first unconverted character. 250.It Bq Er ERANGE 251The given string was out of range; the value converted has been clamped. 252In this case, 253.Fa endptr 254points to the terminating 255.Sq \e0 256if the 257.Fa nptr 258string was fully converted, or to the first unconverted character otherwise. 259.El 260.Pp 261The validity of the provided base is checked first, and if that 262fails, no further processing is attempted. 263The range check is more important than the unconverted characters check, 264and is given priority. 265If a program needs to know if there were unconverted characters when an 266out of range number has been provided, it needs to supply and test 267.Fa endptr. 268.Sh SEE ALSO 269.Xr atof 3 , 270.Xr atoi 3 , 271.Xr atol 3 , 272.Xr atoll 3 , 273.Xr strtod 3 , 274.Xr strtou 3 , 275.Xr strtoimax 3 , 276.Xr strtol 3 , 277.Xr strtoll 3 , 278.Xr strtoul 3 , 279.Xr strtoull 3 , 280.Xr warnc 3 281.Sh STANDARDS 282The 283.Fn strtoi 284and 285.Fn strtoi_l 286functions are a 287.Nx 288extension. 289.Sh HISTORY 290The 291.Fn strtoi 292function first appeared in 293.Nx 7 . 294.Ox 295introduced the 296.Fn strtonum 3 297function for the same purpose, but its interface makes it impossible to 298properly differentiate error conditions. 299.Sh BUGS 300Ignores the current locale while doing the numeric conversion, only 301ASCII letters and digits are allowed, and no grouping characters. 302