1*0a6a1f1dSLionel Sambuc.\" $NetBSD: strtou.3,v 1.3 2015/07/11 15:51:33 wiz Exp $ 2*0a6a1f1dSLionel Sambuc.\" 3*0a6a1f1dSLionel Sambuc.\" Copyright (c) 1990, 1991, 1993 4*0a6a1f1dSLionel Sambuc.\" The Regents of the University of California. All rights reserved. 5*0a6a1f1dSLionel Sambuc.\" 6*0a6a1f1dSLionel Sambuc.\" This code is derived from software contributed to Berkeley by 7*0a6a1f1dSLionel Sambuc.\" Chris Torek and the American National Standards Committee X3, 8*0a6a1f1dSLionel Sambuc.\" on Information Processing Systems. 9*0a6a1f1dSLionel Sambuc.\" 10*0a6a1f1dSLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 11*0a6a1f1dSLionel Sambuc.\" modification, are permitted provided that the following conditions 12*0a6a1f1dSLionel Sambuc.\" are met: 13*0a6a1f1dSLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 14*0a6a1f1dSLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 15*0a6a1f1dSLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 16*0a6a1f1dSLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 17*0a6a1f1dSLionel Sambuc.\" documentation and/or other materials provided with the distribution. 18*0a6a1f1dSLionel Sambuc.\" 3. Neither the name of the University nor the names of its contributors 19*0a6a1f1dSLionel Sambuc.\" may be used to endorse or promote products derived from this software 20*0a6a1f1dSLionel Sambuc.\" without specific prior written permission. 21*0a6a1f1dSLionel Sambuc.\" 22*0a6a1f1dSLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23*0a6a1f1dSLionel Sambuc.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24*0a6a1f1dSLionel Sambuc.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25*0a6a1f1dSLionel Sambuc.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26*0a6a1f1dSLionel Sambuc.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27*0a6a1f1dSLionel Sambuc.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28*0a6a1f1dSLionel Sambuc.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29*0a6a1f1dSLionel Sambuc.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30*0a6a1f1dSLionel Sambuc.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31*0a6a1f1dSLionel Sambuc.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32*0a6a1f1dSLionel Sambuc.\" SUCH DAMAGE. 33*0a6a1f1dSLionel Sambuc.\" 34*0a6a1f1dSLionel Sambuc.\" from: @(#)strtoul.3 8.1 (Berkeley) 6/4/93 35*0a6a1f1dSLionel Sambuc.\" 36*0a6a1f1dSLionel Sambuc.\" Created by Kamil Rytarowski, based on ID: 37*0a6a1f1dSLionel Sambuc.\" NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp 38*0a6a1f1dSLionel Sambuc.\" 39*0a6a1f1dSLionel Sambuc.Dd April 30, 2015 40*0a6a1f1dSLionel Sambuc.Dt STRTOU 3 41*0a6a1f1dSLionel Sambuc.Os 42*0a6a1f1dSLionel Sambuc.Sh NAME 43*0a6a1f1dSLionel Sambuc.Nm strtou 44*0a6a1f1dSLionel Sambuc.Nd convert a string to an uintmax_t integer 45*0a6a1f1dSLionel Sambuc.Sh LIBRARY 46*0a6a1f1dSLionel Sambuc.Lb libc 47*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS 48*0a6a1f1dSLionel Sambuc.In inttypes.h 49*0a6a1f1dSLionel Sambuc.Ft uintmax_t 50*0a6a1f1dSLionel Sambuc.Fo strtou 51*0a6a1f1dSLionel Sambuc.Fa "const char * restrict nptr" 52*0a6a1f1dSLionel Sambuc.Fa "char ** restrict endptr" 53*0a6a1f1dSLionel Sambuc.Fa "int base" 54*0a6a1f1dSLionel Sambuc.Fa "uintmax_t lo" 55*0a6a1f1dSLionel Sambuc.Fa "uintmax_t hi" 56*0a6a1f1dSLionel Sambuc.Fa "int *rstatus" 57*0a6a1f1dSLionel Sambuc.Fc 58*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION 59*0a6a1f1dSLionel SambucThe 60*0a6a1f1dSLionel Sambuc.Fn strtou 61*0a6a1f1dSLionel Sambucfunction converts the string in 62*0a6a1f1dSLionel Sambuc.Fa nptr 63*0a6a1f1dSLionel Sambucto an 64*0a6a1f1dSLionel Sambuc.Ft uintmax_t 65*0a6a1f1dSLionel Sambucvalue. 66*0a6a1f1dSLionel SambucThe 67*0a6a1f1dSLionel Sambuc.Fn strtou 68*0a6a1f1dSLionel Sambucfunction uses internally 69*0a6a1f1dSLionel Sambuc.Xr strtoumax 3 70*0a6a1f1dSLionel Sambucand ensures that the result is always in the range [ 71*0a6a1f1dSLionel Sambuc.Fa lo .. 72*0a6a1f1dSLionel Sambuc.Fa hi 73*0a6a1f1dSLionel Sambuc]. 74*0a6a1f1dSLionel SambucIn adddition it always places 75*0a6a1f1dSLionel Sambuc.Dv 0 76*0a6a1f1dSLionel Sambucon success or a conversion status in the 77*0a6a1f1dSLionel Sambuc.Fa rstatus 78*0a6a1f1dSLionel Sambucargument, avoiding the 79*0a6a1f1dSLionel Sambuc.Dv errno 80*0a6a1f1dSLionel Sambucgymnastics the other functions require. 81*0a6a1f1dSLionel SambucThe 82*0a6a1f1dSLionel Sambuc.Fa rstatus 83*0a6a1f1dSLionel Sambucargument can be 84*0a6a1f1dSLionel Sambuc.Dv NULL 85*0a6a1f1dSLionel Sambucif conversion status is to be ignored. 86*0a6a1f1dSLionel Sambuc.Pp 87*0a6a1f1dSLionel SambucThe string may begin with an arbitrary amount of white space 88*0a6a1f1dSLionel Sambuc(as determined by 89*0a6a1f1dSLionel Sambuc.Xr isspace 3 ) 90*0a6a1f1dSLionel Sambucfollowed by a single optional 91*0a6a1f1dSLionel Sambuc.Ql + 92*0a6a1f1dSLionel Sambucor 93*0a6a1f1dSLionel Sambuc.Ql - 94*0a6a1f1dSLionel Sambucsign. 95*0a6a1f1dSLionel SambucIf 96*0a6a1f1dSLionel Sambuc.Fa base 97*0a6a1f1dSLionel Sambucis zero or 16, 98*0a6a1f1dSLionel Sambucthe string may then include a 99*0a6a1f1dSLionel Sambuc.Ql 0x 100*0a6a1f1dSLionel Sambucprefix, 101*0a6a1f1dSLionel Sambucand the number will be read in base 16; otherwise, a zero 102*0a6a1f1dSLionel Sambuc.Fa base 103*0a6a1f1dSLionel Sambucis taken as 10 (decimal) unless the next character is 104*0a6a1f1dSLionel Sambuc.Ql 0 , 105*0a6a1f1dSLionel Sambucin which case it is taken as 8 (octal). 106*0a6a1f1dSLionel Sambuc.Pp 107*0a6a1f1dSLionel SambucThe remainder of the string is converted to an 108*0a6a1f1dSLionel Sambuc.Em uintmax_t 109*0a6a1f1dSLionel Sambucvalue in the obvious manner, 110*0a6a1f1dSLionel Sambucstopping at the end of the string 111*0a6a1f1dSLionel Sambucor at the first character that does not produce a valid digit 112*0a6a1f1dSLionel Sambucin the given base. 113*0a6a1f1dSLionel Sambuc(In bases above 10, the letter 114*0a6a1f1dSLionel Sambuc.Ql A 115*0a6a1f1dSLionel Sambucin either upper or lower case 116*0a6a1f1dSLionel Sambucrepresents 10, 117*0a6a1f1dSLionel Sambuc.Ql B 118*0a6a1f1dSLionel Sambucrepresents 11, and so forth, with 119*0a6a1f1dSLionel Sambuc.Ql Z 120*0a6a1f1dSLionel Sambucrepresenting 35.) 121*0a6a1f1dSLionel Sambuc.Pp 122*0a6a1f1dSLionel SambucIf 123*0a6a1f1dSLionel Sambuc.Fa endptr 124*0a6a1f1dSLionel Sambucis non-nil, 125*0a6a1f1dSLionel Sambuc.Fn strtou 126*0a6a1f1dSLionel Sambucstores the address of the first invalid character in 127*0a6a1f1dSLionel Sambuc.Fa *endptr . 128*0a6a1f1dSLionel SambucIf there were no digits at all, however, 129*0a6a1f1dSLionel Sambuc.Fn strtou 130*0a6a1f1dSLionel Sambucstores the original value of 131*0a6a1f1dSLionel Sambuc.Fa nptr 132*0a6a1f1dSLionel Sambucin 133*0a6a1f1dSLionel Sambuc.Fa *endptr . 134*0a6a1f1dSLionel Sambuc(Thus, if 135*0a6a1f1dSLionel Sambuc.Fa *nptr 136*0a6a1f1dSLionel Sambucis not 137*0a6a1f1dSLionel Sambuc.Ql \e0 138*0a6a1f1dSLionel Sambucbut 139*0a6a1f1dSLionel Sambuc.Fa **endptr 140*0a6a1f1dSLionel Sambucis 141*0a6a1f1dSLionel Sambuc.Ql \e0 142*0a6a1f1dSLionel Sambucon return, the entire string was valid.) 143*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES 144*0a6a1f1dSLionel SambucThe 145*0a6a1f1dSLionel Sambuc.Fn strtou 146*0a6a1f1dSLionel Sambucfunction 147*0a6a1f1dSLionel Sambucalways returns the closest value in the range specified by 148*0a6a1f1dSLionel Sambucthe 149*0a6a1f1dSLionel Sambuc.Fa lo 150*0a6a1f1dSLionel Sambucand 151*0a6a1f1dSLionel Sambuc.Fa hi 152*0a6a1f1dSLionel Sambucarguments. 153*0a6a1f1dSLionel Sambuc.Pp 154*0a6a1f1dSLionel SambucThe 155*0a6a1f1dSLionel Sambuc.Va errno 156*0a6a1f1dSLionel Sambucvalue is guaranteed to be left unchanged. 157*0a6a1f1dSLionel Sambuc.Pp 158*0a6a1f1dSLionel SambucErrors are stored as the conversion status in the 159*0a6a1f1dSLionel Sambuc.Fa rstatus 160*0a6a1f1dSLionel Sambucargument. 161*0a6a1f1dSLionel Sambuc.Sh EXAMPLES 162*0a6a1f1dSLionel SambucThe following example will always return a number in 163*0a6a1f1dSLionel Sambuc.Dv [1..99] 164*0a6a1f1dSLionel Sambucrange no matter what the input is, and warn if the conversion failed. 165*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 166*0a6a1f1dSLionel Sambucint e; 167*0a6a1f1dSLionel Sambucuintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e); 168*0a6a1f1dSLionel Sambucif (e) 169*0a6a1f1dSLionel Sambuc warnc(e, "conversion of `%s' to a number failed, using %ju", 170*0a6a1f1dSLionel Sambuc buf, lval); 171*0a6a1f1dSLionel Sambuc.Ed 172*0a6a1f1dSLionel Sambuc.Sh ERRORS 173*0a6a1f1dSLionel Sambuc.Bl -tag -width Er 174*0a6a1f1dSLionel Sambuc.It Bq Er ECANCELED 175*0a6a1f1dSLionel SambucThe string did not contain any characters that were converted. 176*0a6a1f1dSLionel Sambuc.It Bq Er EINVAL 177*0a6a1f1dSLionel SambucThe 178*0a6a1f1dSLionel Sambuc.Ar base 179*0a6a1f1dSLionel Sambucis not between 2 and 36 and does not contain the special value 0. 180*0a6a1f1dSLionel Sambuc.It Bq Er ENOTSUP 181*0a6a1f1dSLionel SambucThe string contained non-numeric characters that did not get converted. 182*0a6a1f1dSLionel SambucIn this case, 183*0a6a1f1dSLionel Sambuc.Fa endptr 184*0a6a1f1dSLionel Sambucpoints to the first unconverted character. 185*0a6a1f1dSLionel Sambuc.It Bq Er ERANGE 186*0a6a1f1dSLionel SambucThe given string was out of range; the value converted has been clamped; or 187*0a6a1f1dSLionel Sambucthe range given was invalid, i.e. 188*0a6a1f1dSLionel Sambuc.Fa lo 189*0a6a1f1dSLionel Sambuc\*[Gt] 190*0a6a1f1dSLionel Sambuc.Fa hi . 191*0a6a1f1dSLionel Sambuc.El 192*0a6a1f1dSLionel Sambuc.Sh SEE ALSO 193*0a6a1f1dSLionel Sambuc.Xr atof 3 , 194*0a6a1f1dSLionel Sambuc.Xr atoi 3 , 195*0a6a1f1dSLionel Sambuc.Xr atol 3 , 196*0a6a1f1dSLionel Sambuc.Xr atoll 3 , 197*0a6a1f1dSLionel Sambuc.Xr strtod 3 , 198*0a6a1f1dSLionel Sambuc.Xr strtoi 3 , 199*0a6a1f1dSLionel Sambuc.Xr strtoimax 3 , 200*0a6a1f1dSLionel Sambuc.Xr strtol 3 , 201*0a6a1f1dSLionel Sambuc.Xr strtoll 3 , 202*0a6a1f1dSLionel Sambuc.Xr strtoul 3 , 203*0a6a1f1dSLionel Sambuc.Xr strtoull 3 , 204*0a6a1f1dSLionel Sambuc.Xr strtoumax 3 205*0a6a1f1dSLionel Sambuc.Sh STANDARDS 206*0a6a1f1dSLionel SambucThe 207*0a6a1f1dSLionel Sambuc.Fn strtou 208*0a6a1f1dSLionel Sambucfunction is a 209*0a6a1f1dSLionel Sambuc.Nx 210*0a6a1f1dSLionel Sambucextension. 211*0a6a1f1dSLionel Sambuc.Sh HISTORY 212*0a6a1f1dSLionel SambucThe 213*0a6a1f1dSLionel Sambuc.Fn strtou 214*0a6a1f1dSLionel Sambucfunction first appeared in 215*0a6a1f1dSLionel Sambuc.Nx 7 . 216*0a6a1f1dSLionel Sambuc.Ox 217*0a6a1f1dSLionel Sambucintroduced the 218*0a6a1f1dSLionel Sambuc.Fn strtonum 3 219*0a6a1f1dSLionel Sambucfunction for the same purpose, but the interface makes it impossible to 220*0a6a1f1dSLionel Sambucproperly differentiate illegal returns. 221*0a6a1f1dSLionel Sambuc.Sh BUGS 222*0a6a1f1dSLionel SambucIgnores the current locale. 223