1*0a6a1f1dSLionel Sambuc.\" $NetBSD: strtonum.3,v 1.2 2015/01/19 11:47:41 wiz Exp $ 2*0a6a1f1dSLionel Sambuc.\" $OpenBSD: strtonum.3,v 1.17 2013/08/14 06:32:28 jmc Exp $ 3*0a6a1f1dSLionel Sambuc.\" 4*0a6a1f1dSLionel Sambuc.\" Copyright (c) 2004 Ted Unangst 5*0a6a1f1dSLionel Sambuc.\" 6*0a6a1f1dSLionel Sambuc.\" Permission to use, copy, modify, and distribute this software for any 7*0a6a1f1dSLionel Sambuc.\" purpose with or without fee is hereby granted, provided that the above 8*0a6a1f1dSLionel Sambuc.\" copyright notice and this permission notice appear in all copies. 9*0a6a1f1dSLionel Sambuc.\" 10*0a6a1f1dSLionel Sambuc.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11*0a6a1f1dSLionel Sambuc.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12*0a6a1f1dSLionel Sambuc.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13*0a6a1f1dSLionel Sambuc.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14*0a6a1f1dSLionel Sambuc.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15*0a6a1f1dSLionel Sambuc.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16*0a6a1f1dSLionel Sambuc.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17*0a6a1f1dSLionel Sambuc.\" 18*0a6a1f1dSLionel Sambuc.Dd January 18, 2015 19*0a6a1f1dSLionel Sambuc.Dt STRTONUM 3 20*0a6a1f1dSLionel Sambuc.Os 21*0a6a1f1dSLionel Sambuc.Sh NAME 22*0a6a1f1dSLionel Sambuc.Nm strtonum 23*0a6a1f1dSLionel Sambuc.Nd reliably convert string value to an integer 24*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS 25*0a6a1f1dSLionel Sambuc.Vt #define _OPENBSD_SOURCE 26*0a6a1f1dSLionel Sambuc.In stdlib.h 27*0a6a1f1dSLionel Sambuc.Ft long long 28*0a6a1f1dSLionel Sambuc.Fo strtonum 29*0a6a1f1dSLionel Sambuc.Fa "const char *nptr" 30*0a6a1f1dSLionel Sambuc.Fa "long long minval" 31*0a6a1f1dSLionel Sambuc.Fa "long long maxval" 32*0a6a1f1dSLionel Sambuc.Fa "const char **errstr" 33*0a6a1f1dSLionel Sambuc.Fc 34*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION 35*0a6a1f1dSLionel SambucThe 36*0a6a1f1dSLionel Sambuc.Fn strtonum 37*0a6a1f1dSLionel Sambucfunction converts the string in 38*0a6a1f1dSLionel Sambuc.Fa nptr 39*0a6a1f1dSLionel Sambucto a 40*0a6a1f1dSLionel Sambuc.Li long long 41*0a6a1f1dSLionel Sambucvalue. 42*0a6a1f1dSLionel Sambuc.Pp 43*0a6a1f1dSLionel SambucThe string may begin with an arbitrary amount of whitespace 44*0a6a1f1dSLionel Sambuc(as determined by 45*0a6a1f1dSLionel Sambuc.Xr isspace 3 ) 46*0a6a1f1dSLionel Sambucfollowed by a single optional 47*0a6a1f1dSLionel Sambuc.Ql + 48*0a6a1f1dSLionel Sambucor 49*0a6a1f1dSLionel Sambuc.Ql - 50*0a6a1f1dSLionel Sambucsign. 51*0a6a1f1dSLionel Sambuc.Pp 52*0a6a1f1dSLionel SambucThe remainder of the string is converted to a 53*0a6a1f1dSLionel Sambuc.Li long long 54*0a6a1f1dSLionel Sambucvalue according to base 10. 55*0a6a1f1dSLionel Sambuc.Pp 56*0a6a1f1dSLionel SambucThe value obtained is then checked against the provided 57*0a6a1f1dSLionel Sambuc.Fa minval 58*0a6a1f1dSLionel Sambucand 59*0a6a1f1dSLionel Sambuc.Fa maxval 60*0a6a1f1dSLionel Sambucbounds. 61*0a6a1f1dSLionel SambucIf 62*0a6a1f1dSLionel Sambuc.Fa errstr 63*0a6a1f1dSLionel Sambucis non-null, 64*0a6a1f1dSLionel Sambuc.Fn strtonum 65*0a6a1f1dSLionel Sambucstores an error string in 66*0a6a1f1dSLionel Sambuc.Fa *errstr 67*0a6a1f1dSLionel Sambucindicating the failure. 68*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES 69*0a6a1f1dSLionel SambucThe 70*0a6a1f1dSLionel Sambuc.Fn strtonum 71*0a6a1f1dSLionel Sambucfunction returns the result of the conversion, 72*0a6a1f1dSLionel Sambucunless the value would exceed the provided bounds or is invalid. 73*0a6a1f1dSLionel SambucOn error, 0 is returned, 74*0a6a1f1dSLionel Sambuc.Va errno 75*0a6a1f1dSLionel Sambucis set, and 76*0a6a1f1dSLionel Sambuc.Fa errstr 77*0a6a1f1dSLionel Sambucwill point to an error message. 78*0a6a1f1dSLionel Sambuc.Fa *errstr 79*0a6a1f1dSLionel Sambucwill be set to 80*0a6a1f1dSLionel Sambuc.Dv NULL 81*0a6a1f1dSLionel Sambucon success; 82*0a6a1f1dSLionel Sambucthis fact can be used to differentiate 83*0a6a1f1dSLionel Sambuca successful return of 0 from an error. 84*0a6a1f1dSLionel Sambuc.Sh EXAMPLES 85*0a6a1f1dSLionel SambucUsing 86*0a6a1f1dSLionel Sambuc.Fn strtonum 87*0a6a1f1dSLionel Sambuccorrectly is meant to be simpler than the alternative functions. 88*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 89*0a6a1f1dSLionel Sambucint iterations; 90*0a6a1f1dSLionel Sambucconst char *errstr; 91*0a6a1f1dSLionel Sambuc 92*0a6a1f1dSLionel Sambuciterations = strtonum(optarg, 1, 64, &errstr); 93*0a6a1f1dSLionel Sambucif (errstr) 94*0a6a1f1dSLionel Sambuc errx(1, "number of iterations is %s: %s", errstr, optarg); 95*0a6a1f1dSLionel Sambuc.Ed 96*0a6a1f1dSLionel Sambuc.Pp 97*0a6a1f1dSLionel SambucThe above example will guarantee that the value of iterations is between 98*0a6a1f1dSLionel Sambuc1 and 64 (inclusive). 99*0a6a1f1dSLionel Sambuc.Sh ERRORS 100*0a6a1f1dSLionel Sambuc.Bl -tag -width Er 101*0a6a1f1dSLionel Sambuc.It Bq Er EINVAL 102*0a6a1f1dSLionel SambucThe given string did not consist solely of digit characters; or 103*0a6a1f1dSLionel Sambuc.Ar minval 104*0a6a1f1dSLionel Sambucwas larger than 105*0a6a1f1dSLionel Sambuc.Ar maxval . 106*0a6a1f1dSLionel Sambuc.It Bq Er ERANGE 107*0a6a1f1dSLionel SambucThe given string was out of range. 108*0a6a1f1dSLionel Sambuc.El 109*0a6a1f1dSLionel Sambuc.Pp 110*0a6a1f1dSLionel SambucIf an error occurs, 111*0a6a1f1dSLionel Sambuc.Fa errstr 112*0a6a1f1dSLionel Sambucwill be set to one of the following strings: 113*0a6a1f1dSLionel Sambuc.Pp 114*0a6a1f1dSLionel Sambuc.Bl -tag -width "too largeXX" -compact 115*0a6a1f1dSLionel Sambuc.It Qq too large 116*0a6a1f1dSLionel SambucThe result was larger than the provided maximum value. 117*0a6a1f1dSLionel Sambuc.It Qq too small 118*0a6a1f1dSLionel SambucThe result was smaller than the provided minimum value. 119*0a6a1f1dSLionel Sambuc.It Qq invalid 120*0a6a1f1dSLionel SambucThe string did not consist solely of digit characters. 121*0a6a1f1dSLionel Sambuc.El 122*0a6a1f1dSLionel Sambuc.Sh SEE ALSO 123*0a6a1f1dSLionel Sambuc.Xr atof 3 , 124*0a6a1f1dSLionel Sambuc.Xr atoi 3 , 125*0a6a1f1dSLionel Sambuc.Xr atol 3 , 126*0a6a1f1dSLionel Sambuc.Xr atoll 3 , 127*0a6a1f1dSLionel Sambuc.Xr sscanf 3 , 128*0a6a1f1dSLionel Sambuc.Xr strtod 3 , 129*0a6a1f1dSLionel Sambuc.Xr strtoi 3 , 130*0a6a1f1dSLionel Sambuc.Xr strtol 3 , 131*0a6a1f1dSLionel Sambuc.Xr strtoll 3 , 132*0a6a1f1dSLionel Sambuc.Xr strtou 3 , 133*0a6a1f1dSLionel Sambuc.Xr strtoul 3 , 134*0a6a1f1dSLionel Sambuc.Xr strtoull 3 135*0a6a1f1dSLionel Sambuc.Sh STANDARDS 136*0a6a1f1dSLionel Sambuc.Fn strtonum 137*0a6a1f1dSLionel Sambucis an 138*0a6a1f1dSLionel Sambuc.Ox 139*0a6a1f1dSLionel Sambucextension. 140*0a6a1f1dSLionel Sambuc.Sh HISTORY 141*0a6a1f1dSLionel SambucThe 142*0a6a1f1dSLionel Sambuc.Fn strtonum 143*0a6a1f1dSLionel Sambucfunction first appeared in 144*0a6a1f1dSLionel Sambuc.Ox 3.6 . 145*0a6a1f1dSLionel Sambuc.Fn strtonum 146*0a6a1f1dSLionel Sambucwas redesigned in 147*0a6a1f1dSLionel Sambuc.Nx 8 148*0a6a1f1dSLionel Sambucas 149*0a6a1f1dSLionel Sambuc.Fn strtoi 3 150*0a6a1f1dSLionel Sambucand 151*0a6a1f1dSLionel Sambuc.Fn strtou 3 . 152*0a6a1f1dSLionel SambucFor compatibility reasons it's available since 153*0a6a1f1dSLionel Sambuc.Nx 8 154*0a6a1f1dSLionel Sambucin the 155*0a6a1f1dSLionel Sambuc.Vt _OPENBSD_SOURCE 156*0a6a1f1dSLionel Sambucnamespace. 157*0a6a1f1dSLionel Sambuc.Sh CAVEATS 158*0a6a1f1dSLionel SambucThe 159*0a6a1f1dSLionel Sambuc.Fn strtonum 160*0a6a1f1dSLionel Sambucfunction was designed to facilitate safe, 161*0a6a1f1dSLionel Sambucrobust programming and overcome the shortcomings of the 162*0a6a1f1dSLionel Sambuc.Xr atoi 3 163*0a6a1f1dSLionel Sambucand 164*0a6a1f1dSLionel Sambuc.Xr strtol 3 165*0a6a1f1dSLionel Sambucfamily of interfaces, however there are problems with the 166*0a6a1f1dSLionel Sambuc.Fn strtonum 167*0a6a1f1dSLionel SambucAPI: 168*0a6a1f1dSLionel Sambuc.Bl -dash 169*0a6a1f1dSLionel Sambuc.It 170*0a6a1f1dSLionel Sambucwill return 0 on failure; 0 might not be in range, so that necessitates 171*0a6a1f1dSLionel Sambucan error check even if you want to avoid it 172*0a6a1f1dSLionel Sambuc.It 173*0a6a1f1dSLionel Sambucdoes not differentiate 'illegal' returns, so we can't tell the 174*0a6a1f1dSLionel Sambucdifference between partial and no conversions 175*0a6a1f1dSLionel Sambuc.It 176*0a6a1f1dSLionel Sambucreturns english strings 177*0a6a1f1dSLionel Sambuc.It 178*0a6a1f1dSLionel Sambuccan't set the base, or find where the conversion ended 179*0a6a1f1dSLionel Sambuc.It 180*0a6a1f1dSLionel Sambuchardcodes long long integer type 181*0a6a1f1dSLionel Sambuc.El 182*0a6a1f1dSLionel SambucTo overcome the shortcomings of 183*0a6a1f1dSLionel Sambuc.Fn strtonum 184*0a6a1f1dSLionel Sambuc.Nx 185*0a6a1f1dSLionel Sambucprovides 186*0a6a1f1dSLionel Sambuc.Fn strtou 3 187*0a6a1f1dSLionel Sambucand 188*0a6a1f1dSLionel Sambuc.Fn strtoi 3 . 189