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.3148
1 files changed, 113 insertions, 35 deletions
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3
index 808ba90165..3f5375c5e3 100644
--- a/src/lib/libc/stdlib/strtol.3
+++ b/src/lib/libc/stdlib/strtol.3
@@ -13,11 +13,7 @@
13.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution. 15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software 16.\" 3. Neither the name of the University nor the names of its contributors
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 17.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission. 18.\" without specific prior written permission.
23.\" 19.\"
@@ -33,49 +29,57 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
35.\" 31.\"
36.\" from: @(#)strtol.3 5.4 (Berkeley) 6/25/92 32.\" $OpenBSD: strtol.3,v 1.13 2003/06/02 20:18:38 millert Exp $
37.\" $Id: strtol.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
38.\" 33.\"
39.Dd June 25, 1992 34.Dd June 25, 1992
40.Dt STRTOL 3 35.Dt STRTOL 3
41.Os 36.Os
42.Sh NAME 37.Sh NAME
43.Nm strtol, strtoq 38.Nm strtol ,
44.Nd convert string value to a long or quad_t integer 39.Nm strtoll ,
40.Nm strtoq
41.Nd "convert string value to a long or long long integer"
45.Sh SYNOPSIS 42.Sh SYNOPSIS
46.Fd #include <stdlib.h> 43.Fd #include <stdlib.h>
47.Fd #include <limits.h> 44.Fd #include <limits.h>
48.Ft long 45.Ft long
49.Fn strtol "char *nptr" "char **endptr" "int base" 46.Fn strtol "const char *nptr" "char **endptr" "int base"
50 47.Pp
48.Fd #include <stdlib.h>
49.Fd #include <limits.h>
50.Ft long long
51.Fn strtoll "const char *nptr" "char **endptr" "int base"
52.Pp
51.Fd #include <sys/types.h> 53.Fd #include <sys/types.h>
52.Fd #include <stdlib.h> 54.Fd #include <stdlib.h>
53.Fd #include <limits.h> 55.Fd #include <limits.h>
54.Ft quad_t 56.Ft quad_t
55.Fn strtoq "char *nptr" "char **endptr" "int base" 57.Fn strtoq "const char *nptr" "char **endptr" "int base"
56.Sh DESCRIPTION 58.Sh DESCRIPTION
57The 59The
58.Fn strtol 60.Fn strtol
59function 61function converts the string in
60converts the string in
61.Fa nptr 62.Fa nptr
62to a 63to a
63.Em long 64.Li long
64value. 65value.
65The 66The
66.Fn strtoq 67.Fn strtoll
67function 68function converts the string in
68converts the string in
69.Fa nptr 69.Fa nptr
70to a 70to a
71.Em quad_t 71.Li long long
72value. 72value.
73The
74.Fn strtoq
75function is a deprecated equivalent of
76.Fn strtoll
77and is provided for backwards compatibility with legacy programs.
73The conversion is done according to the given 78The conversion is done according to the given
74.Fa base , 79.Fa base ,
75which must be between 2 and 36 inclusive, 80which must be a number between 2 and 36 inclusive or the special value 0.
76or be the special value 0.
77.Pp 81.Pp
78The string may begin with an arbitrary amount of white space 82The string may begin with an arbitrary amount of whitespace
79(as determined by 83(as determined by
80.Xr isspace 3 ) 84.Xr isspace 3 )
81followed by a single optional 85followed by a single optional
@@ -85,25 +89,22 @@ or
85sign. 89sign.
86If 90If
87.Fa base 91.Fa base
88is zero or 16, 92is zero or 16, the string may then include a
89the string may then include a
90.Ql 0x 93.Ql 0x
91prefix, 94prefix, and the number will be read in base 16; otherwise, a zero
92and the number will be read in base 16; otherwise, a zero
93.Fa base 95.Fa base
94is taken as 10 (decimal) unless the next character is 96is taken as 10 (decimal) unless the next character is
95.Ql 0 , 97.Ql 0 ,
96in which case it is taken as 8 (octal). 98in which case it is taken as 8 (octal).
97.Pp 99.Pp
98The remainder of the string is converted to a 100The remainder of the string is converted to a
99.Em long 101.Li long
100value in the obvious manner, 102value in the obvious manner,
101stopping at the first character which is not a valid digit 103stopping at the first character which is not a valid digit
102in the given base. 104in the given base.
103(In bases above 10, the letter 105(In bases above 10, the letter
104.Ql A 106.Ql A
105in either upper or lower case 107in either upper or lower case represents 10,
106represents 10,
107.Ql B 108.Ql B
108represents 11, and so forth, with 109represents 11, and so forth, with
109.Ql Z 110.Ql Z
@@ -111,7 +112,7 @@ representing 35.)
111.Pp 112.Pp
112If 113If
113.Fa endptr 114.Fa endptr
114is non nil, 115is non-null,
115.Fn strtol 116.Fn strtol
116stores the address of the first invalid character in 117stores the address of the first invalid character in
117.Fa *endptr . 118.Fa *endptr .
@@ -133,8 +134,7 @@ on return, the entire string was valid.)
133.Sh RETURN VALUES 134.Sh RETURN VALUES
134The 135The
135.Fn strtol 136.Fn strtol
136function 137function returns the result of the conversion,
137returns the result of the conversion,
138unless the value would underflow or overflow. 138unless the value would underflow or overflow.
139If an underflow occurs, 139If an underflow occurs,
140.Fn strtol 140.Fn strtol
@@ -148,6 +148,76 @@ In both cases,
148.Va errno 148.Va errno
149is set to 149is set to
150.Er ERANGE . 150.Er ERANGE .
151.Pp
152The
153.Fn strtoll
154function has identical return values except that
155.Dv LLONG_MIN
156and
157.Dv LLONG_MAX
158are used to indicate underflow and overflow respectively.
159.Sh EXAMPLES
160Ensuring that a string is a valid number (i.e., in range and containing no
161trailing characters) requires clearing
162.Va errno
163beforehand explicitly since
164.Va errno
165is not changed on a successful call to
166.Fn strtol ,
167and the return value of
168.Fn strtol
169cannot be used unambiguously to signal an error:
170.Bd -literal -offset indent
171char *ep;
172long lval;
173
174\&...
175
176errno = 0;
177lval = strtol(buf, &ep, 10);
178if (buf[0] == '\e0' || *ep != '\e0')
179 goto not_a_number;
180if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
181 goto out_of_range;
182.Ed
183.Pp
184This example will accept
185.Dq 12
186but not
187.Dq 12foo
188or
189.Dq 12\en .
190If trailing whitespace is acceptable, further checks must be done on
191.Va *ep ;
192alternately, use
193.Xr sscanf 3 .
194.Pp
195If
196.Fn strtol
197is being used instead of
198.Xr atoi 3 ,
199error checking is further complicated because the desired return value is an
200.Li int
201rather than a
202.Li long ;
203however, on some architectures integers and long integers are the same size.
204Thus the following is necessary:
205.Bd -literal -offset indent
206char *ep;
207int ival;
208long lval;
209
210\&...
211
212errno = 0;
213lval = strtol(buf, &ep, 10);
214if (buf[0] == '\e0' || *ep != '\e0')
215 goto not_a_number;
216if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
217 (lval > INT_MAX || lval < INT_MIN))
218 goto out_of_range;
219ival = lval;
220.Ed
151.Sh ERRORS 221.Sh ERRORS
152.Bl -tag -width Er 222.Bl -tag -width Er
153.It Bq Er ERANGE 223.It Bq Er ERANGE
@@ -157,13 +227,21 @@ The given string was out of range; the value converted has been clamped.
157.Xr atof 3 , 227.Xr atof 3 ,
158.Xr atoi 3 , 228.Xr atoi 3 ,
159.Xr atol 3 , 229.Xr atol 3 ,
230.Xr atoll 3 ,
231.Xr sscanf 3 ,
160.Xr strtod 3 , 232.Xr strtod 3 ,
161.Xr strtoul 3 233.Xr strtoul 3
162.Sh STANDARDS 234.Sh STANDARDS
163The 235The
164.Fn strtol 236.Fn strtol
165function 237and
166conforms to 238.Fn strtoll
167.St -ansiC . 239functions conform to
240.St -ansiC-99 .
241The
242.Fn strtoq
243function is a
244.Bx
245extension and is provided for backwards compatibility with legacy programs.
168.Sh BUGS 246.Sh BUGS
169Ignores the current locale. 247Ignores the current locale.