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