1.\" $NetBSD: strtoul.3,v 1.6 1995/12/28 08:52:55 thorpej Exp $ 2.\" 3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 4.\" 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. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the University of 21.\" California, Berkeley and its contributors. 22.\" 4. Neither the name of the University nor the names of its contributors 23.\" may be used to endorse or promote products derived from this software 24.\" without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36.\" SUCH DAMAGE. 37.\" 38.\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 39.\" 40.Dd June 25, 1992 41.Dt STRTOUL 3 42.Os 43.Sh NAME 44.Nm strtoul, strtouq 45.Nd convert a string to an unsigned long or uquad_t integer 46.Sh SYNOPSIS 47.Fd #include <stdlib.h> 48.Fd #include <limits.h> 49.Ft unsigned long 50.Fn strtoul "const char *nptr" "char **endptr" "int base" 51 52.Fd #include <sys/types.h> 53.Fd #include <stdlib.h> 54.Fd #include <limits.h> 55.Ft u_quad_t 56.Fn strtouq "const char *nptr" "char **endptr" "int base" 57.Sh DESCRIPTION 58The 59.Fn strtoul 60function 61converts the string in 62.Fa nptr 63to an 64.Em unsigned long 65value. 66The 67.Fn strtouq 68function 69converts the string in 70.Fa nptr 71to a 72.Em u_quad_t 73value. 74The conversion is done according to the given 75.Fa base , 76which must be between 2 and 36 inclusive, 77or be the special value 0. 78.Pp 79The string may begin with an arbitrary amount of white space 80(as determined by 81.Xr isspace 3 ) 82followed by a single optional 83.Ql + 84or 85.Ql - 86sign. 87If 88.Fa base 89is zero or 16, 90the string may then include a 91.Ql 0x 92prefix, 93and the number will be read in base 16; otherwise, a zero 94.Fa base 95is taken as 10 (decimal) unless the next character is 96.Ql 0 , 97in which case it is taken as 8 (octal). 98.Pp 99The remainder of the string is converted to an 100.Em unsigned long 101value in the obvious manner, 102stopping at the end of the string 103or at the first character that does not produce a valid digit 104in the given base. 105(In bases above 10, the letter 106.Ql A 107in either upper or lower case 108represents 10, 109.Ql B 110represents 11, and so forth, with 111.Ql Z 112representing 35.) 113.Pp 114If 115.Fa endptr 116is non nil, 117.Fn strtoul 118stores the address of the first invalid character in 119.Fa *endptr . 120If there were no digits at all, however, 121.Fn strtoul 122stores the original value of 123.Fa nptr 124in 125.Fa *endptr . 126(Thus, if 127.Fa *nptr 128is not 129.Ql \e0 130but 131.Fa **endptr 132is 133.Ql \e0 134on return, the entire string was valid.) 135.Sh RETURN VALUES 136The 137.Fn strtoul 138function 139returns either the result of the conversion 140or, if there was a leading minus sign, 141the negation of the result of the conversion, 142unless the original (non-negated) value would overflow; 143in the latter case, 144.Fn strtoul 145returns 146.Dv ULONG_MAX 147and sets the global variable 148.Va errno 149to 150.Er ERANGE . 151.Sh ERRORS 152.Bl -tag -width Er 153.It Bq Er ERANGE 154The given string was out of range; the value converted has been clamped. 155.El 156.Sh SEE ALSO 157.Xr strtol 3 158.Sh STANDARDS 159The 160.Fn strtoul 161function 162conforms to 163.St -ansiC . 164.Sh BUGS 165Ignores the current locale. 166