1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" $OpenBSD: strtoul.3,v 1.15 2003/06/02 20:18:38 millert Exp $ 33.\" 34.Dd June 25, 1992 35.Dt STRTOUL 3 36.Os 37.Sh NAME 38.Nm strtoul , 39.Nm strtoull , 40.Nm strtouq 41.Nd "convert a string to an unsigned long or unsigned long long integer" 42.Sh SYNOPSIS 43.Fd #include <stdlib.h> 44.Fd #include <limits.h> 45.Ft unsigned long 46.Fn strtoul "const char *nptr" "char **endptr" "int base" 47.Pp 48.Fd #include <stdlib.h> 49.Fd #include <limits.h> 50.Ft unsigned long long 51.Fn strtoull "const char *nptr" "char **endptr" "int base" 52.Pp 53.Fd #include <sys/types.h> 54.Fd #include <stdlib.h> 55.Fd #include <limits.h> 56.Ft u_quad_t 57.Fn strtouq "const char *nptr" "char **endptr" "int base" 58.Sh DESCRIPTION 59The 60.Fn strtoul 61function converts the string in 62.Fa nptr 63to an 64.Li unsigned long 65value. 66The 67.Fn strtoull 68function converts the string in 69.Fa nptr 70to an 71.Li unsigned long long 72value. 73The 74.Fn strtouq 75function is a deprecated equivalent of 76.Fn strtoull 77and is provided for backwards compatibility with legacy programs. 78The conversion is done according to the given 79.Fa base , 80which must be a number between 2 and 36 inclusive 81or the special value 0. 82If the string in 83.Fa nptr 84represents a negative number, it will be converted to its unsigned equivalent. 85This behavior is consistent with what happens when a signed integer type is 86cast to its unsigned counterpart. 87.Pp 88The string may begin with an arbitrary amount of whitespace 89(as determined by 90.Xr isspace 3 ) 91followed by a single optional 92.Ql + 93or 94.Ql - 95sign. 96If 97.Fa base 98is zero or 16, the string may then include a 99.Ql 0x 100prefix, and the number will be read in base 16; otherwise, a zero 101.Fa base 102is taken as 10 (decimal) unless the next character is 103.Ql 0 , 104in which case it is taken as 8 (octal). 105.Pp 106The remainder of the string is converted to an 107.Li unsigned long 108value in the obvious manner, stopping at the end of the string 109or at the first character that does not produce a valid digit 110in the given base. 111(In bases above 10, the letter 112.Ql A 113in either upper or lower case represents 10, 114.Ql B 115represents 11, and so forth, with 116.Ql Z 117representing 35.) 118.Pp 119If 120.Fa endptr 121is non-null, 122.Fn strtoul 123stores the address of the first invalid character in 124.Fa *endptr . 125If there were no digits at all, however, 126.Fn strtoul 127stores the original value of 128.Fa nptr 129in 130.Fa *endptr . 131(Thus, if 132.Fa *nptr 133is not 134.Ql \e0 135but 136.Fa **endptr 137is 138.Ql \e0 139on return, the entire string was valid.) 140.Sh RETURN VALUES 141The 142.Fn strtoul 143function returns the result of the conversion, 144unless the value would overflow, in which case 145.Dv ULONG_MAX 146is returned and 147.Va errno 148is set to 149.Er ERANGE . 150If there was a leading minus sign, 151.Fn strtoul 152returns the (unsigned) negation of the absolute value of the number, unless 153the absolute value would overflow. 154In this case, 155.Fn strtoul 156returns 157.Dv ULONG_MAX 158and sets the global variable 159.Va errno 160to 161.Er ERANGE . 162.Pp 163The 164.Fn strtoull 165function has identical return values except that 166.Dv ULLONG_MAX 167is used to indicate overflow. 168.Pp 169There is no way to determine if 170.Fn strtoul 171has processed a negative number (and returned an unsigned value) short of 172examining the string in 173.Fa nptr 174directly. 175.Sh EXAMPLES 176Ensuring that a string is a valid number (i.e., in range and containing no 177trailing characters) requires clearing 178.Va errno 179beforehand explicitly since 180.Va errno 181is not changed on a successful call to 182.Fn strtoul , 183and the return value of 184.Fn strtoul 185cannot be used unambiguously to signal an error: 186.Bd -literal -offset indent 187char *ep; 188unsigned long ulval; 189 190\&... 191 192errno = 0; 193ulval = strtoul(buf, &ep, 10); 194if (buf[0] == '\e0' || *ep != '\e0') 195 goto not_a_number; 196if (errno == ERANGE && ulval == ULONG_MAX) 197 goto out_of_range; 198.Ed 199.Pp 200This example will accept 201.Dq 12 202but not 203.Dq 12foo 204or 205.Dq 12\en . 206If trailing whitespace is acceptable, further checks must be done on 207.Va *ep ; 208alternately, use 209.Xr sscanf 3 . 210.Sh ERRORS 211.Bl -tag -width Er 212.It Bq Er ERANGE 213The given string was out of range; the value converted has been clamped. 214.El 215.Sh SEE ALSO 216.Xr sscanf 3 , 217.Xr strtol 3 218.Sh STANDARDS 219The 220.Fn strtoul 221and 222.Fn strtoull 223functions conform to 224.St -ansiC-99 . 225The 226.Fn strtouq 227function is a 228.Bx 229extension and is provided for backwards compatibility with legacy programs. 230.Sh BUGS 231Ignores the current locale. 232