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. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" $OpenBSD: strtol.3,v 1.27 2015/04/14 22:16:03 nicm Exp $ 33.\" 34.Dd $Mdocdate: April 14 2015 $ 35.Dt STRTOL 3 36.Os 37.Sh NAME 38.Nm strtol , 39.Nm strtoll , 40.Nm strtoimax , 41.Nm strtoq 42.Nd convert string value to a long, long long or intmax_t integer 43.Sh SYNOPSIS 44.In limits.h 45.In stdlib.h 46.Ft long 47.Fn strtol "const char *nptr" "char **endptr" "int base" 48.Ft long long 49.Fn strtoll "const char *nptr" "char **endptr" "int base" 50.In inttypes.h 51.Ft intmax_t 52.Fn strtoimax "const char *nptr" "char **endptr" "int base" 53.In sys/types.h 54.In limits.h 55.In stdlib.h 56.Ft quad_t 57.Fn strtoq "const char *nptr" "char **endptr" "int base" 58.Sh DESCRIPTION 59The 60.Fn strtol 61function converts the string in 62.Fa nptr 63to a 64.Vt long 65value. 66The 67.Fn strtoll 68function converts the string in 69.Fa nptr 70to a 71.Vt long long 72value. 73The 74.Fn strtoimax 75function converts the string in 76.Fa nptr 77to an 78.Vt intmax_t 79value. 80The 81.Fn strtoq 82function is a deprecated equivalent of 83.Fn strtoll 84and is provided for backwards compatibility with legacy programs. 85The conversion is done according to the given 86.Fa base , 87which must be a number between 2 and 36 inclusive or the special value 0. 88.Pp 89The string may begin with an arbitrary amount of whitespace 90(as determined by 91.Xr isspace 3 ) 92followed by a single optional 93.Ql + 94or 95.Ql - 96sign. 97If 98.Fa base 99is zero or 16, the string may then include a 100.Ql 0x 101prefix, and the number will be read in base 16; otherwise, a zero 102.Fa base 103is taken as 10 (decimal) unless the next character is 104.Ql 0 , 105in which case it is taken as 8 (octal). 106.Pp 107The remainder of the string is converted to a 108.Vt long , 109.Vt long long , 110or 111.Vt intmax_t 112value in the obvious manner, 113stopping at the first character which is not a valid digit 114in the given base. 115(In bases above 10, the letter 116.Ql A 117in either upper or lower case represents 10, 118.Ql B 119represents 11, and so forth, with 120.Ql Z 121representing 35.) 122.Pp 123If 124.Fa endptr 125is non-null, 126.Fn strtol 127stores the address of the first invalid character in 128.Fa *endptr . 129If there were no digits at all, however, 130.Fn strtol 131stores the original value of 132.Fa nptr 133in 134.Fa *endptr . 135(Thus, if 136.Fa *nptr 137is not 138.Ql \e0 139but 140.Fa **endptr 141is 142.Ql \e0 143on return, the entire string was valid.) 144.Sh RETURN VALUES 145The 146.Fn strtol , 147.Fn strtoll , 148.Fn strtoimax , 149and 150.Fn strtoq 151functions return the result of the conversion. 152If overflow or underflow occurs, 153.Va errno 154is set to 155.Er ERANGE 156and the function return value is as follows: 157.Bl -column "strtoimaxXX" "INTMAX_MIN" "INTMAX_MAX" -offset indent 158.It Sy Function Ta Sy underflow Ta Sy overflow 159.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX 160.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX 161.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX 162.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX 163.El 164.Pp 165If there is no valid digit, 0 is returned. 166If 167.Ar base 168is invalid, 0 is returned and the global variable 169.Va errno 170is set to 171.Er EINVAL . 172.Sh EXAMPLES 173Ensuring that a string is a valid number (i.e., in range and containing no 174trailing characters) requires clearing 175.Va errno 176beforehand explicitly since 177.Va errno 178is not changed on a successful call to 179.Fn strtol , 180and the return value of 181.Fn strtol 182cannot be used unambiguously to signal an error: 183.Bd -literal -offset indent 184char *ep; 185long lval; 186 187\&... 188 189errno = 0; 190lval = strtol(buf, &ep, 10); 191if (buf[0] == '\e0' || *ep != '\e0') 192 goto not_a_number; 193if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) 194 goto out_of_range; 195.Ed 196.Pp 197This example will accept 198.Dq 12 199but not 200.Dq 12foo 201or 202.Dq 12\en . 203If trailing whitespace is acceptable, further checks must be done on 204.Va *ep ; 205alternately, use 206.Xr sscanf 3 . 207.Pp 208If 209.Fn strtol 210is being used instead of 211.Xr atoi 3 , 212error checking is further complicated because the desired return value is an 213.Vt int 214rather than a 215.Vt long ; 216however, on some architectures integers and long integers are the same size. 217Thus the following is necessary: 218.Bd -literal -offset indent 219char *ep; 220int ival; 221long lval; 222 223\&... 224 225errno = 0; 226lval = strtol(buf, &ep, 10); 227if (buf[0] == '\e0' || *ep != '\e0') 228 goto not_a_number; 229if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || 230 (lval > INT_MAX || lval < INT_MIN)) 231 goto out_of_range; 232ival = lval; 233.Ed 234.Sh ERRORS 235.Bl -tag -width Er 236.It Bq Er EINVAL 237The value of 238.Ar base 239was neither between 2 and 36 inclusive nor the special value 0. 240.It Bq Er ERANGE 241The given string was out of range; the value converted has been clamped. 242.El 243.Sh SEE ALSO 244.Xr atof 3 , 245.Xr atoi 3 , 246.Xr atol 3 , 247.Xr atoll 3 , 248.Xr sscanf 3 , 249.Xr strtod 3 , 250.Xr strtonum 3 , 251.Xr strtoul 3 252.Sh STANDARDS 253The 254.Fn strtol , 255.Fn strtoll , 256and 257.Fn strtoimax 258functions conform to 259.St -isoC-99 . 260Setting 261.Va errno 262to 263.Dv EINVAL 264is an extension to that standard required by 265.St -p1003.1-2008 . 266.Pp 267The 268.Fn strtoq 269function is a 270.Bx 271extension and is provided for backwards compatibility with legacy programs. 272.Sh BUGS 273Ignores the current locale. 274