summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/strtol.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/strtol.3')
-rw-r--r--src/lib/libc/stdlib/strtol.3243
1 files changed, 243 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3
new file mode 100644
index 0000000000..a5bdff0b81
--- /dev/null
+++ b/src/lib/libc/stdlib/strtol.3
@@ -0,0 +1,243 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" 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.\" $OpenBSD: strtol.3,v 1.10 2002/06/29 00:20:11 millert Exp $
37.\"
38.Dd June 25, 1992
39.Dt STRTOL 3
40.Os
41.Sh NAME
42.Nm strtol ,
43.Nm strtoll ,
44.Nm strtoq
45.Nd convert string value to a long or long long integer
46.Sh SYNOPSIS
47.Fd #include <stdlib.h>
48.Fd #include <limits.h>
49.Ft long
50.Fn strtol "const char *nptr" "char **endptr" "int base"
51.Pp
52.Fd #include <stdlib.h>
53.Fd #include <limits.h>
54.Ft long long
55.Fn strtoll "const char *nptr" "char **endptr" "int base"
56.Pp
57.Fd #include <sys/types.h>
58.Fd #include <stdlib.h>
59.Fd #include <limits.h>
60.Ft quad_t
61.Fn strtoq "const char *nptr" "char **endptr" "int base"
62.Sh DESCRIPTION
63The
64.Fn strtol
65function converts the string in
66.Fa nptr
67to a
68.Li long
69value.
70The
71.Fn strtoll
72function converts the string in
73.Fa nptr
74to a
75.Li long long
76value.
77The
78.Fn strtoq
79function is a deprecated equivalent of
80.Fn strtoll
81and is provided for backwards compatibility with legacy programs.
82The conversion is done according to the given
83.Fa base ,
84which must be a number between 2 and 36 inclusive or the special value 0.
85.Pp
86The string may begin with an arbitrary amount of whitespace
87(as determined by
88.Xr isspace 3 )
89followed by a single optional
90.Ql +
91or
92.Ql -
93sign.
94If
95.Fa base
96is zero or 16, the string may then include a
97.Ql 0x
98prefix, and the number will be read in base 16; otherwise, a zero
99.Fa base
100is taken as 10 (decimal) unless the next character is
101.Ql 0 ,
102in which case it is taken as 8 (octal).
103.Pp
104The remainder of the string is converted to a
105.Li long
106value in the obvious manner,
107stopping at the first character which is not a valid digit
108in the given base.
109(In bases above 10, the letter
110.Ql A
111in either upper or lower case represents 10,
112.Ql B
113represents 11, and so forth, with
114.Ql Z
115representing 35.)
116.Pp
117If
118.Fa endptr
119is non-null,
120.Fn strtol
121stores the address of the first invalid character in
122.Fa *endptr .
123If there were no digits at all, however,
124.Fn strtol
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 strtol
141function returns 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.Pp
156The
157.Fn strtoll
158function has identical return values except that
159.Dv LLONG_MIN
160and
161.Dv LLONG_MAX
162are used to indicate underflow and overflow respectively.
163.Sh EXAMPLES
164Ensuring that a string is a valid number (i.e., in range and containing no
165trailing characters) requires clearing
166.Va errno
167beforehand explicitly since
168.Va errno
169is not changed on a successful call to
170.Fn strtol ,
171and the return value of
172.Fn strtol
173cannot be used unambiguously to signal an error:
174.Bd -literal -offset indent
175char *ep;
176long lval;
177
178\&...
179
180errno = 0;
181lval = strtol(buf, &ep, 10);
182if (buf[0] == '\e0' || *ep != '\e0')
183 goto not_a_number;
184if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
185 goto out_of_range;
186.Ed
187.Pp
188This example will accept
189.Dq 12
190but not
191.Dq 12foo
192or
193.Dq 12\en .
194If trailing whitespace is acceptable, further checks must be done on
195.Va *ep ;
196alternately, use
197.Xr sscanf 3 .
198.Pp
199If
200.Fn strtol
201is being used instead of
202.Xr atoi 3 ,
203error checking is further complicated because the desired return value is an
204.Li int
205rather than a
206.Li long ;
207however, on some architectures integers and long integers are the same size.
208Thus the following is necessary:
209.Bd -literal -offset indent
210char *ep;
211int ival;
212long lval;
213
214\&...
215
216errno = 0;
217lval = strtol(buf, &ep, 10);
218if (buf[0] == '\e0' || *ep != '\e0')
219 goto not_a_number;
220if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
221 (lval > INT_MAX || lval < INT_MIN))
222 goto out_of_range;
223ival = lval;
224.Ed
225.Sh ERRORS
226.Bl -tag -width Er
227.It Bq Er ERANGE
228The given string was out of range; the value converted has been clamped.
229.El
230.Sh SEE ALSO
231.Xr atof 3 ,
232.Xr atoi 3 ,
233.Xr atol 3 ,
234.Xr sscanf 3 ,
235.Xr strtod 3 ,
236.Xr strtoul 3
237.Sh STANDARDS
238The
239.Fn strtol
240function conforms to
241.St -ansiC .
242.Sh BUGS
243Ignores the current locale.