1*c2368254Skre.\" $NetBSD: strtoi.3,v 1.11 2024/07/24 08:55:08 kre Exp $ 235d355f3Schristos.\" 335d355f3Schristos.\" Copyright (c) 1990, 1991, 1993 435d355f3Schristos.\" The Regents of the University of California. All rights reserved. 535d355f3Schristos.\" 635d355f3Schristos.\" This code is derived from software contributed to Berkeley by 735d355f3Schristos.\" Chris Torek and the American National Standards Committee X3, 835d355f3Schristos.\" on Information Processing Systems. 935d355f3Schristos.\" 1035d355f3Schristos.\" Redistribution and use in source and binary forms, with or without 1135d355f3Schristos.\" modification, are permitted provided that the following conditions 1235d355f3Schristos.\" are met: 1335d355f3Schristos.\" 1. Redistributions of source code must retain the above copyright 1435d355f3Schristos.\" notice, this list of conditions and the following disclaimer. 1535d355f3Schristos.\" 2. Redistributions in binary form must reproduce the above copyright 1635d355f3Schristos.\" notice, this list of conditions and the following disclaimer in the 1735d355f3Schristos.\" documentation and/or other materials provided with the distribution. 1835d355f3Schristos.\" 3. Neither the name of the University nor the names of its contributors 1935d355f3Schristos.\" may be used to endorse or promote products derived from this software 2035d355f3Schristos.\" without specific prior written permission. 2135d355f3Schristos.\" 2235d355f3Schristos.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2335d355f3Schristos.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2435d355f3Schristos.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2535d355f3Schristos.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2635d355f3Schristos.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2735d355f3Schristos.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2835d355f3Schristos.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2935d355f3Schristos.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3035d355f3Schristos.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3135d355f3Schristos.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3235d355f3Schristos.\" SUCH DAMAGE. 3335d355f3Schristos.\" 3435d355f3Schristos.\" from: @(#)strtol.3 8.1 (Berkeley) 6/4/93 3535d355f3Schristos.\" 3635d355f3Schristos.\" Created by Kamil Rytarowski, based on ID: 3735d355f3Schristos.\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp 3835d355f3Schristos.\" 39*c2368254Skre.Dd July 24, 2024 4035d355f3Schristos.Dt STRTOI 3 4135d355f3Schristos.Os 4235d355f3Schristos.Sh NAME 43*c2368254Skre.Nm strtoi , 44*c2368254Skre.Nm strtoi_l 45f1987b28Schristos.Nd convert a string value to an intmax_t integer 4635d355f3Schristos.Sh LIBRARY 4735d355f3Schristos.Lb libc 4835d355f3Schristos.Sh SYNOPSIS 4935d355f3Schristos.In inttypes.h 5035d355f3Schristos.Ft intmax_t 5135d355f3Schristos.Fo strtoi 5235d355f3Schristos.Fa "const char * restrict nptr" 5335d355f3Schristos.Fa "char ** restrict endptr" 5435d355f3Schristos.Fa "int base" 5535d355f3Schristos.Fa "intmax_t lo" 5635d355f3Schristos.Fa "intmax_t hi" 5735d355f3Schristos.Fa "int *rstatus" 582aaae60fSwiz.Fc 59*c2368254Skre.In locale.h 60*c2368254Skre.Ft intmax_t 61*c2368254Skre.Fo strtoi_l 62*c2368254Skre.Fa "const char * restrict nptr" 63*c2368254Skre.Fa "char ** restrict endptr" 64*c2368254Skre.Fa "int base" 65*c2368254Skre.Fa "intmax_t lo" 66*c2368254Skre.Fa "intmax_t hi" 67*c2368254Skre.Fa "int *rstatus" 68*c2368254Skre.Fa "locale_t loc" 69*c2368254Skre.Fc 7035d355f3Schristos.Sh DESCRIPTION 7135d355f3SchristosThe 7235d355f3Schristos.Fn strtoi 73*c2368254Skregenerates the 7435d355f3Schristos.Ft intmax_t 75*c2368254Skreresult value equivalent to the numeric string in 76*c2368254Skre.Fa nptr . 7735d355f3SchristosThe 7835d355f3Schristos.Fn strtoi 79*c2368254Skrefunction internally uses 8035d355f3Schristos.Xr strtoimax 3 81*c2368254Skreand then ensures that the result is in the range 82*c2368254Skre.Bq Fa lo No .. Fa hi . 83*c2368254SkreIn addition it places 84*c2368254Skrea conversion status indicator, 8535d355f3Schristos.Dv 0 86*c2368254Skreif fully successful, 87*c2368254Skrein the integer addressed by the 8835d355f3Schristos.Fa rstatus 89*c2368254Skreargument, if that is not NULL, allowing the 9035d355f3Schristos.Dv errno 91*c2368254Skregymnastics that other similar functions require to be avoided. 9235d355f3SchristosThe 9335d355f3Schristos.Fa rstatus 9435d355f3Schristosargument can be 9535d355f3Schristos.Dv NULL 96*c2368254Skreif the conversion status is to be ignored. 97*c2368254Skre.Pp 98*c2368254SkreThe operation of 99*c2368254Skre.Fn strtoi 100*c2368254Skreis unspecified if 101*c2368254Skre.Fa lo 102*c2368254Skreis greater than 103*c2368254Skre.Fa hi . 10435d355f3Schristos.Pp 10535d355f3SchristosThe string may begin with an arbitrary amount of white space 10635d355f3Schristos(as determined by 10735d355f3Schristos.Xr isspace 3 ) 10835d355f3Schristosfollowed by a single optional 10935d355f3Schristos.Ql + 11035d355f3Schristosor 11135d355f3Schristos.Ql - 11235d355f3Schristossign. 11335d355f3SchristosIf 11435d355f3Schristos.Fa base 11535d355f3Schristosis zero or 16, 11635d355f3Schristosthe string may then include a 11735d355f3Schristos.Ql 0x 1186ea48155Schristosor 1196ea48155Schristos.Ql 0X 12035d355f3Schristosprefix, 121*c2368254Skreafter which there must immediately follow at least one hexadecimal digit, 1226ea48155Schristosand the number will be read in base 16; otherwise, 12353ba0e3dSchristos.\" if the 12453ba0e3dSchristos.\" .Fa base 12553ba0e3dSchristos.\" is zero or 2, 12653ba0e3dSchristos.\" the string may then include a 12753ba0e3dSchristos.\" .Ql 0b 12853ba0e3dSchristos.\" or 12953ba0e3dSchristos.\" .Ql 0B 13053ba0e3dSchristos.\" prefix, 13153ba0e3dSchristos.\" and the number will be read in base 2; otherwise, 1326ea48155Schristosa zero 13335d355f3Schristos.Fa base 13435d355f3Schristosis taken as 10 (decimal) unless the next character is 13535d355f3Schristos.Ql 0 , 13635d355f3Schristosin which case it is taken as 8 (octal). 13735d355f3Schristos.Pp 138*c2368254SkreThe remainder of the string is converted to the 13935d355f3Schristos.Em intmax_t 140*c2368254Skreresult in the obvious manner, 141f1987b28Schristosstopping at the end of the string 142f1987b28Schristosor at the first character which is not a valid digit 14335d355f3Schristosin the given base. 14435d355f3Schristos(In bases above 10, the letter 14535d355f3Schristos.Ql A 14635d355f3Schristosin either upper or lower case 14735d355f3Schristosrepresents 10, 14835d355f3Schristos.Ql B 14935d355f3Schristosrepresents 11, and so forth, with 15035d355f3Schristos.Ql Z 15135d355f3Schristosrepresenting 35.) 15235d355f3Schristos.Pp 15335d355f3SchristosIf 15435d355f3Schristos.Fa endptr 155*c2368254Skreis not NULL, 15635d355f3Schristos.Fn strtoi 157*c2368254Skrestores the address of the first character after those 158*c2368254Skrewhich were converted in 15935d355f3Schristos.Fa *endptr . 16035d355f3SchristosIf there were no digits at all, however, 161*c2368254Skreor if the 162*c2368254Skre.Fa base 163*c2368254Skreis invalid, 16435d355f3Schristos.Fn strtoi 16535d355f3Schristosstores the original value of 16635d355f3Schristos.Fa nptr 16735d355f3Schristosin 16835d355f3Schristos.Fa *endptr . 16935d355f3Schristos(Thus, if 17035d355f3Schristos.Fa *nptr 17135d355f3Schristosis not 17235d355f3Schristos.Ql \e0 17335d355f3Schristosbut 17435d355f3Schristos.Fa **endptr 17535d355f3Schristosis 17635d355f3Schristos.Ql \e0 17735d355f3Schristoson return, the entire string was valid.) 178*c2368254SkreNote that converting an out of range value has no 179*c2368254Skreimpact upon the value placed into 180*c2368254Skre.Fa *endptr . 181*c2368254Skre.Pp 182*c2368254SkreThe 183*c2368254Skre.Fn strtoi_l 184*c2368254Skrefunction is identical, except uses the locale given by 185*c2368254Skre.Fa loc 186*c2368254Skrerather than the current locale, when determining what is white space to 187*c2368254Skrebe skipped before the conversion begins. 18835d355f3Schristos.Sh RETURN VALUES 18935d355f3SchristosThe 19035d355f3Schristos.Fn strtoi 191*c2368254Skrefunction, 192*c2368254Skrereturns the converted value, 193*c2368254Skreor the closest value in the range specified by the 19435d355f3Schristos.Fa lo 19535d355f3Schristosand 19635d355f3Schristos.Fa hi 197*c2368254Skrearguments, if the value converted was outside that range. 198*c2368254SkreIf 199*c2368254Skre.Fa lo 200*c2368254Skreis equal to 201*c2368254Skre.Fa hi 202*c2368254Skreand no overriding error occurs, 203*c2368254Skrethat value will always be returned. 20435d355f3Schristos.Pp 20535d355f3SchristosThe 20635d355f3Schristos.Va errno 207*c2368254Skrevalue from 208*c2368254Skre.In errno.h , 209*c2368254Skreis guaranteed to be left unchanged. 21035d355f3Schristos.Pp 211*c2368254SkreErrors are stored as the conversion status error indicator, 212*c2368254Skretaken from a subset of the values from 213*c2368254Skre.In errno.h 214*c2368254Skrein the 21535d355f3Schristos.Fa rstatus 216*c2368254Skreargument, if that was not given as a NULL pointer. 217*c2368254SkreSee the ERRORS section below for the possible values. 21835d355f3Schristos.Sh EXAMPLES 21935d355f3SchristosThe following example will always return a number in 22035d355f3Schristos.Dv [1..99] 22135d355f3Schristosrange no matter what the input is, and warn if the conversion failed. 22235d355f3Schristos.Bd -literal -offset indent 22335d355f3Schristosint e; 22435d355f3Schristosintmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e); 22535d355f3Schristosif (e) 22635d355f3Schristos warnc(e, "conversion of `%s' to a number failed, using %jd", 22735d355f3Schristos buf, lval); 22835d355f3Schristos.Ed 22935d355f3Schristos.Sh ERRORS 23035d355f3Schristos.Bl -tag -width Er 231a4569887Swiz.It Bq Er ECANCELED 232a4569887SwizThe string did not contain any characters that were converted. 233*c2368254SkreIf given 234*c2368254Skre.Fa endptr 235*c2368254Skrewill be set to 236*c2368254Skre.Fa nptr . 23735d355f3Schristos.It Bq Er EINVAL 23835d355f3SchristosThe 23935d355f3Schristos.Ar base 240*c2368254Skreis not between 2 and 36 and nor is it the special value 0. 241*c2368254SkreIf given 242*c2368254Skre.Fa endptr 243*c2368254Skrewill be set to 244*c2368254Skre.Fa nptr . 24535d355f3Schristos.It Bq Er ENOTSUP 24635d355f3SchristosThe string contained non-numeric characters that did not get converted. 24735d355f3SchristosIn this case, 24835d355f3Schristos.Fa endptr 24935d355f3Schristospoints to the first unconverted character. 25035d355f3Schristos.It Bq Er ERANGE 251*c2368254SkreThe given string was out of range; the value converted has been clamped. 252*c2368254SkreIn this case, 253*c2368254Skre.Fa endptr 254*c2368254Skrepoints to the terminating 255*c2368254Skre.Sq \e0 256*c2368254Skreif the 257*c2368254Skre.Fa nptr 258*c2368254Skrestring was fully converted, or to the first unconverted character otherwise. 25935d355f3Schristos.El 260f1987b28Schristos.Pp 261*c2368254SkreThe validity of the provided base is checked first, and if that 262*c2368254Skrefails, no further processing is attempted. 2630c834cb5SchristosThe range check is more important than the unconverted characters check, 264*c2368254Skreand is given priority. 265f1987b28SchristosIf a program needs to know if there were unconverted characters when an 266f1987b28Schristosout of range number has been provided, it needs to supply and test 267f1987b28Schristos.Fa endptr. 26835d355f3Schristos.Sh SEE ALSO 26935d355f3Schristos.Xr atof 3 , 27035d355f3Schristos.Xr atoi 3 , 27135d355f3Schristos.Xr atol 3 , 27235d355f3Schristos.Xr atoll 3 , 27335d355f3Schristos.Xr strtod 3 , 274*c2368254Skre.Xr strtou 3 , 275a4569887Swiz.Xr strtoimax 3 , 27635d355f3Schristos.Xr strtol 3 , 27735d355f3Schristos.Xr strtoll 3 , 27835d355f3Schristos.Xr strtoul 3 , 27935d355f3Schristos.Xr strtoull 3 , 280*c2368254Skre.Xr warnc 3 28135d355f3Schristos.Sh STANDARDS 28235d355f3SchristosThe 28335d355f3Schristos.Fn strtoi 284*c2368254Skreand 285*c2368254Skre.Fn strtoi_l 286*c2368254Skrefunctions are a 28735d355f3Schristos.Nx 28835d355f3Schristosextension. 28935d355f3Schristos.Sh HISTORY 29035d355f3SchristosThe 29135d355f3Schristos.Fn strtoi 29235d355f3Schristosfunction first appeared in 29335d355f3Schristos.Nx 7 . 29435d355f3Schristos.Ox 29535d355f3Schristosintroduced the 29635d355f3Schristos.Fn strtonum 3 297*c2368254Skrefunction for the same purpose, but its interface makes it impossible to 298*c2368254Skreproperly differentiate error conditions. 29935d355f3Schristos.Sh BUGS 300*c2368254SkreIgnores the current locale while doing the numeric conversion, only 301*c2368254SkreASCII letters and digits are allowed, and no grouping characters. 302