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