1 .\" $NetBSD: strtoul.3,v 1.24 2009/12/02 12:45:35 pooka Exp $
3 .\" Copyright (c) 1990, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
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.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" from: @(#)strtoul.3 8.1 (Berkeley) 6/4/93
44 .Nd convert a string to an unsigned long, unsigned long long, uintmax_t or uquad_t integer
51 .Fn strtoul "const char * restrict nptr" "char ** restrict endptr" "int base"
52 .Ft unsigned long long int
53 .Fn strtoull "const char * restrict nptr" "char ** restrict endptr" "int base"
57 .Fn strtoumax "const char * restrict nptr" "char ** restrict endptr" "int base"
63 .Fn strtouq "const char * restrict nptr" "char ** restrict endptr" "int base"
68 converts the string in
76 converts the string in
79 .Ft unsigned long long int
84 converts the string in
92 converts the string in
97 The conversion is done according to the given
99 which must be between 2 and 36 inclusive,
100 or be the special value 0.
102 The string may begin with an arbitrary amount of white space
105 followed by a single optional
113 the string may then include a
116 and the number will be read in base 16; otherwise, a zero
118 is taken as 10 (decimal) unless the next character is
120 in which case it is taken as 8 (octal).
122 The remainder of the string is converted to an
124 value in the obvious manner,
125 stopping at the end of the string
126 or at the first character that does not produce a valid digit
128 (In bases above 10, the letter
130 in either upper or lower case
133 represents 11, and so forth, with
141 stores the address of the first invalid character in
143 If there were no digits at all, however,
145 stores the original value of
157 on return, the entire string was valid.)
162 returns either the result of the conversion
163 or, if there was a leading minus sign,
164 the negation of the result of the conversion,
165 unless the original (non-negated) value would overflow;
179 and the global variable
184 There is no way to determine if
186 has processed a negative number (and returned an unsigned value) short of
187 examining the string in
192 argument is not supported then
196 and the functions return 0.
201 This behavior (which is unlike most library functions) is guaranteed
202 by the pertinent standards.
204 Because the return value of
206 cannot be used unambiguously to detect an error,
208 is left unchanged after a successful call.
209 To ensure that a string is a valid number (i.e., in range and containing no
210 trailing characters), clear
212 beforehand explicitly, then check it afterwards:
213 .Bd -literal -offset indent
220 ulval = strtoul(buf, \*[Am]ep, 10);
221 if (buf[0] == '\e0' || *ep != '\e0')
223 if (errno == ERANGE \*[Am]\*[Am] ulval == ULONG_MAX)
227 This example will accept
233 If trailing whitespace is acceptable, further checks must be done on
242 is not between 2 and 36 and does not contain the special value 0.
244 The given string was out of range; the value converted has been clamped.
263 Ignores the current locale.