xref: /netbsd-src/lib/libc/stdlib/strtol.3 (revision 01869ca4d24a86379a68731bf9706a9f0820fe4e)
1.\"	$NetBSD: strtol.3,v 1.40 2017/07/03 21:32:50 wiz 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. 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.
21.\"
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
32.\" SUCH DAMAGE.
33.\"
34.\"     from: @(#)strtol.3	8.1 (Berkeley) 6/4/93
35.\"
36.Dd November 4, 2016
37.Dt STRTOL 3
38.Os
39.Sh NAME
40.Nm strtol ,
41.Nm strtoll ,
42.Nm strtoimax ,
43.Nm strtoq
44.Nd convert string value to a long, long long, intmax_t or quad_t integer
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In stdlib.h
49.In limits.h
50.Ft long int
51.Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
52.Ft long long int
53.Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
54.Pp
55.In inttypes.h
56.Ft intmax_t
57.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
58.Pp
59.In sys/types.h
60.In stdlib.h
61.In limits.h
62.Ft quad_t
63.Fn strtoq "const char * restrict nptr" "char ** restrict endptr" "int base"
64.Sh DESCRIPTION
65The
66.Fn strtol
67function
68converts the string in
69.Fa nptr
70to a
71.Ft long int
72value.
73The
74.Fn strtoll
75function
76converts the string in
77.Fa nptr
78to a
79.Ft long long int
80value.
81The
82.Fn strtoimax
83function
84converts the string in
85.Fa nptr
86to an
87.Ft intmax_t
88value.
89The
90.Fn strtoq
91function
92converts the string in
93.Fa nptr
94to a
95.Ft quad_t
96value.
97.Pp
98The conversion is done according to the given
99.Fa base ,
100which must be between 2 and 36 inclusive,
101or be the special value 0.
102.Pp
103The string may begin with an arbitrary amount of white space
104(as determined by
105.Xr isspace 3 )
106followed by a single optional
107.Ql +
108or
109.Ql -
110sign.
111If
112.Fa base
113is zero or 16,
114the string may then include a
115.Ql 0x
116or
117.Ql 0X
118prefix,
119and the number will be read in base 16; otherwise,
120.\" if the
121.\" .Fa base
122.\" is zero or 2,
123.\" the string may then include a
124.\" .Ql 0b
125.\" or
126.\" .Ql 0B
127.\" prefix,
128.\" and the number will be read in base 2; otherwise,
129a zero
130.Fa base
131is taken as 10 (decimal) unless the next character is
132.Ql 0 ,
133in which case it is taken as 8 (octal).
134.Pp
135The remainder of the string is converted to an appropriate value
136in the obvious manner,
137stopping at the first character which is not a valid digit in the given base.
138(In bases above 10, the letter
139.Ql A
140in either upper or lower case
141represents 10,
142.Ql B
143represents 11, and so forth, with
144.Ql Z
145representing 35.)
146.Pp
147If
148.Fa endptr
149is non-nil, the functions store the address of the first invalid character in
150.Fa *endptr .
151If there were no digits at all, however,
152the functions store the original value of
153.Fa nptr
154in
155.Fa *endptr .
156(Thus, if
157.Fa *nptr
158is not
159.Ql \e0
160but
161.Fa **endptr
162is
163.Ql \e0
164on return, the entire string was valid.)
165.Sh RETURN VALUES
166The
167.Fn strtol
168function
169returns the result of the conversion,
170unless the value would underflow or overflow.
171If an underflow occurs,
172.Fn strtol
173returns
174.Dv LONG_MIN ,
175.Fn strtoll
176returns
177.Dv LLONG_MIN ,
178and
179.Fn strtoimax
180returns
181.Dv INTMAX_MIN .
182If an overflow occurs,
183.Fn strtol
184returns
185.Dv LONG_MAX ,
186.Fn strtoll
187returns
188.Dv LLONG_MAX ,
189and
190.Fn strtoimax
191returns
192.Dv INTMAX_MAX .
193In these cases,
194.Va errno
195is set to
196.Er ERANGE .
197If the
198.Fa base
199argument is not supported then
200.Va errno
201is set to
202.Er EINVAL
203and the functions return 0.
204.Pp
205If no error occurs,
206.Va errno
207is left unchanged.
208This behavior (which is unlike most library functions) is guaranteed
209by the pertinent standards.
210.Sh EXAMPLES
211Because the return value of
212.Fn strtol
213cannot be used unambiguously to detect an error,
214.Va errno
215is left unchanged after a successful call.
216To ensure that a string is a valid number (i.e., in range and containing no
217trailing characters), clear
218.Va errno
219beforehand explicitly, then check it afterwards:
220.Bd -literal -offset indent
221char *ep;
222long lval;
223
224\&...
225
226errno = 0;
227lval = strtol(buf, &ep, 10);
228if (ep == buf)
229	goto not_a_number;
230if (*ep != '\e0')
231	goto trailing_garbage;
232if (errno) {
233	assert(errno == ERANGE);
234	assert(lval == LONG_MAX || lval == LONG_MIN);
235	goto out_of_range;
236}
237.Ed
238.Pp
239This example will accept
240.Dq 12
241but not
242.Dq 12foo
243or
244.Dq 12\en .
245If trailing whitespace is acceptable, further checks must be done on
246.Va *ep ;
247alternately, use
248.Xr sscanf 3 .
249.Pp
250If
251.Fn strtol
252is being used instead of
253.Xr atoi 3 ,
254error checking is further complicated because the desired return value is an
255.Li int
256rather than a
257.Li long ;
258however, on some architectures integers and long integers are the same size.
259Thus the following is necessary:
260.Bd -literal -offset indent
261char *ep;
262int ival;
263long lval;
264
265\&...
266
267errno = 0;
268lval = strtol(buf, &ep, 10);
269if (ep == buf)
270	goto not_a_number;
271if (*ep != '\e0')
272	goto trailing_garbage;
273if (errno == ERANGE || lval < INT_MIN || INT_MAX < lval)
274	goto out_of_range;
275assert(errno == 0);
276assert(INT_MIN <= lval);
277assert(lval <= INT_MAX);
278ival = lval;
279.Ed
280.Sh ERRORS
281.Bl -tag -width Er
282.It Bq Er EINVAL
283The
284.Ar base
285is not between 2 and 36 and does not contain the special value 0.
286.It Bq Er ERANGE
287The given string was out of range; the value converted has been clamped.
288.El
289.Sh SEE ALSO
290.Xr atof 3 ,
291.Xr atoi 3 ,
292.Xr atol 3 ,
293.Xr atoll 3 ,
294.Xr strtod 3 ,
295.Xr strtou 3 ,
296.Xr strtoul 3 ,
297.Xr strtoull 3 ,
298.Xr strtoumax 3
299.Sh STANDARDS
300The
301.Fn strtol
302function
303conforms to
304.St -ansiC .
305The
306.Fn strtoll
307and
308.Fn strtoimax
309functions conform to
310.St -isoC-99 .
311.Pp
312The
313.Fn strtoq
314function is a
315.Bx
316legacy function equivalent to
317.Fn strtoll
318and should not be used in a new code.
319.Sh BUGS
320Ignores the current locale.
321