xref: /netbsd-src/lib/libc/stdlib/strtoul.3 (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1.\"	$NetBSD: strtoul.3,v 1.9 1998/02/05 18:50:20 perry Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  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	8.1 (Berkeley) 6/4/93
39.\"
40.Dd June 4, 1993
41.Dt STRTOUL 3
42.Os
43.Sh NAME
44.Nm strtoul ,
45.Nm strtouq
46.Nd convert a string to an unsigned long or uquad_t integer
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.Fd #include <stdlib.h>
51.Fd #include <limits.h>
52.Ft unsigned long
53.Fn strtoul "const char *nptr" "char **endptr" "int base"
54
55.Fd #include <sys/types.h>
56.Fd #include <stdlib.h>
57.Fd #include <limits.h>
58.Ft u_quad_t
59.Fn strtouq "const char *nptr" "char **endptr" "int base"
60.Sh DESCRIPTION
61The
62.Fn strtoul
63function
64converts the string in
65.Fa nptr
66to an
67.Em unsigned long
68value.
69The
70.Fn strtouq
71function
72converts the string in
73.Fa nptr
74to a
75.Em u_quad_t
76value.
77The conversion is done according to the given
78.Fa base ,
79which must be between 2 and 36 inclusive,
80or be the special value 0.
81.Pp
82The string may begin with an arbitrary amount of white space
83(as determined by
84.Xr isspace 3 )
85followed by a single optional
86.Ql +
87or
88.Ql -
89sign.
90If
91.Fa base
92is zero or 16,
93the string may then include a
94.Ql 0x
95prefix,
96and the number will be read in base 16; otherwise, a zero
97.Fa base
98is taken as 10 (decimal) unless the next character is
99.Ql 0 ,
100in which case it is taken as 8 (octal).
101.Pp
102The remainder of the string is converted to an
103.Em unsigned long
104value in the obvious manner,
105stopping at the end of the string
106or at the first character that does not produce a valid digit
107in the given base.
108(In bases above 10, the letter
109.Ql A
110in either upper or lower case
111represents 10,
112.Ql B
113represents 11, and so forth, with
114.Ql Z
115representing 35.)
116.Pp
117If
118.Fa endptr
119is non nil,
120.Fn strtoul
121stores the address of the first invalid character in
122.Fa *endptr .
123If there were no digits at all, however,
124.Fn strtoul
125stores the original value of
126.Fa nptr
127in
128.Fa *endptr .
129(Thus, if
130.Fa *nptr
131is not
132.Ql \e0
133but
134.Fa **endptr
135is
136.Ql \e0
137on return, the entire string was valid.)
138.Sh RETURN VALUES
139The
140.Fn strtoul
141function
142returns either the result of the conversion
143or, if there was a leading minus sign,
144the negation of the result of the conversion,
145unless the original (non-negated) value would overflow;
146in the latter case,
147.Fn strtoul
148returns
149.Dv ULONG_MAX
150and sets the global variable
151.Va errno
152to
153.Er ERANGE .
154.Sh ERRORS
155.Bl -tag -width Er
156.It Bq Er ERANGE
157The given string was out of range; the value converted has been clamped.
158.El
159.Sh SEE ALSO
160.Xr strtol 3
161.Sh STANDARDS
162The
163.Fn strtoul
164function
165conforms to
166.St -ansiC .
167.Sh BUGS
168Ignores the current locale.
169