xref: /netbsd-src/lib/libc/stdlib/strtol.3 (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1.\"	$NetBSD: strtol.3,v 1.9 1998/02/05 18:50:19 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: @(#)strtol.3	8.1 (Berkeley) 6/4/93
39.\"
40.Dd June 4, 1993
41.Dt STRTOL 3
42.Os
43.Sh NAME
44.Nm strtol ,
45.Nm strtoq
46.Nd convert string value to a long or quad_t integer
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.Fd #include <stdlib.h>
51.Fd #include <limits.h>
52.Ft long
53.Fn strtol "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 quad_t
59.Fn strtoq "const char *nptr" "char **endptr" "int base"
60.Sh DESCRIPTION
61The
62.Fn strtol
63function
64converts the string in
65.Fa nptr
66to a
67.Em long
68value.
69The
70.Fn strtoq
71function
72converts the string in
73.Fa nptr
74to a
75.Em 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 a
103.Em long
104value in the obvious manner,
105stopping at the first character which is not a valid digit
106in the given base.
107(In bases above 10, the letter
108.Ql A
109in either upper or lower case
110represents 10,
111.Ql B
112represents 11, and so forth, with
113.Ql Z
114representing 35.)
115.Pp
116If
117.Fa endptr
118is non nil,
119.Fn strtol
120stores the address of the first invalid character in
121.Fa *endptr .
122If there were no digits at all, however,
123.Fn strtol
124stores the original value of
125.Fa nptr
126in
127.Fa *endptr .
128(Thus, if
129.Fa *nptr
130is not
131.Ql \e0
132but
133.Fa **endptr
134is
135.Ql \e0
136on return, the entire string was valid.)
137.Sh RETURN VALUES
138The
139.Fn strtol
140function
141returns the result of the conversion,
142unless the value would underflow or overflow.
143If an underflow occurs,
144.Fn strtol
145returns
146.Dv LONG_MIN .
147If an overflow occurs,
148.Fn strtol
149returns
150.Dv LONG_MAX .
151In both cases,
152.Va errno
153is set to
154.Er ERANGE .
155.Sh ERRORS
156.Bl -tag -width Er
157.It Bq Er ERANGE
158The given string was out of range; the value converted has been clamped.
159.El
160.Sh SEE ALSO
161.Xr atof 3 ,
162.Xr atoi 3 ,
163.Xr atol 3 ,
164.Xr strtod 3 ,
165.Xr strtoul 3
166.Sh STANDARDS
167The
168.Fn strtol
169function
170conforms to
171.St -ansiC .
172.Sh BUGS
173Ignores the current locale.
174