xref: /minix3/lib/libc/stdlib/strtol.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: strtol.3,v 1.32 2015/05/01 14:17:56 christos Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by
72fe8fb19SBen Gras.\" Chris Torek and the American National Standards Committee X3,
82fe8fb19SBen Gras.\" on Information Processing Systems.
92fe8fb19SBen Gras.\"
102fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
122fe8fb19SBen Gras.\" are met:
132fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras.\"    without specific prior written permission.
212fe8fb19SBen Gras.\"
222fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras.\" SUCH DAMAGE.
332fe8fb19SBen Gras.\"
342fe8fb19SBen Gras.\"     from: @(#)strtol.3	8.1 (Berkeley) 6/4/93
352fe8fb19SBen Gras.\"
36*0a6a1f1dSLionel Sambuc.Dd April 30, 2015
372fe8fb19SBen Gras.Dt STRTOL 3
382fe8fb19SBen Gras.Os
392fe8fb19SBen Gras.Sh NAME
402fe8fb19SBen Gras.Nm strtol ,
412fe8fb19SBen Gras.Nm strtoll ,
422fe8fb19SBen Gras.Nm strtoimax ,
432fe8fb19SBen Gras.Nm strtoq
442fe8fb19SBen Gras.Nd convert string value to a long, long long, intmax_t or quad_t integer
452fe8fb19SBen Gras.Sh LIBRARY
462fe8fb19SBen Gras.Lb libc
472fe8fb19SBen Gras.Sh SYNOPSIS
482fe8fb19SBen Gras.In stdlib.h
492fe8fb19SBen Gras.In limits.h
502fe8fb19SBen Gras.Ft long int
512fe8fb19SBen Gras.Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
522fe8fb19SBen Gras.Ft long long int
532fe8fb19SBen Gras.Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
542fe8fb19SBen Gras.Pp
552fe8fb19SBen Gras.In inttypes.h
562fe8fb19SBen Gras.Ft intmax_t
572fe8fb19SBen Gras.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
582fe8fb19SBen Gras.Pp
592fe8fb19SBen Gras.In sys/types.h
602fe8fb19SBen Gras.In stdlib.h
612fe8fb19SBen Gras.In limits.h
622fe8fb19SBen Gras.Ft quad_t
632fe8fb19SBen Gras.Fn strtoq "const char * restrict nptr" "char ** restrict endptr" "int base"
642fe8fb19SBen Gras.Sh DESCRIPTION
652fe8fb19SBen GrasThe
662fe8fb19SBen Gras.Fn strtol
672fe8fb19SBen Grasfunction
682fe8fb19SBen Grasconverts the string in
692fe8fb19SBen Gras.Fa nptr
702fe8fb19SBen Grasto a
712fe8fb19SBen Gras.Ft long int
722fe8fb19SBen Grasvalue.
732fe8fb19SBen GrasThe
742fe8fb19SBen Gras.Fn strtoll
752fe8fb19SBen Grasfunction
762fe8fb19SBen Grasconverts the string in
772fe8fb19SBen Gras.Fa nptr
782fe8fb19SBen Grasto a
792fe8fb19SBen Gras.Ft long long int
802fe8fb19SBen Grasvalue.
812fe8fb19SBen GrasThe
822fe8fb19SBen Gras.Fn strtoimax
832fe8fb19SBen Grasfunction
842fe8fb19SBen Grasconverts the string in
852fe8fb19SBen Gras.Fa nptr
862fe8fb19SBen Grasto an
872fe8fb19SBen Gras.Ft intmax_t
882fe8fb19SBen Grasvalue.
892fe8fb19SBen GrasThe
902fe8fb19SBen Gras.Fn strtoq
912fe8fb19SBen Grasfunction
922fe8fb19SBen Grasconverts the string in
932fe8fb19SBen Gras.Fa nptr
942fe8fb19SBen Grasto a
952fe8fb19SBen Gras.Ft quad_t
962fe8fb19SBen Grasvalue.
97*0a6a1f1dSLionel Sambuc.Pp
982fe8fb19SBen GrasThe conversion is done according to the given
992fe8fb19SBen Gras.Fa base ,
1002fe8fb19SBen Graswhich must be between 2 and 36 inclusive,
1012fe8fb19SBen Grasor be the special value 0.
1022fe8fb19SBen Gras.Pp
1032fe8fb19SBen GrasThe string may begin with an arbitrary amount of white space
1042fe8fb19SBen Gras(as determined by
1052fe8fb19SBen Gras.Xr isspace 3 )
1062fe8fb19SBen Grasfollowed by a single optional
1072fe8fb19SBen Gras.Ql +
1082fe8fb19SBen Grasor
1092fe8fb19SBen Gras.Ql -
1102fe8fb19SBen Grassign.
1112fe8fb19SBen GrasIf
1122fe8fb19SBen Gras.Fa base
1132fe8fb19SBen Grasis zero or 16,
1142fe8fb19SBen Grasthe string may then include a
1152fe8fb19SBen Gras.Ql 0x
1162fe8fb19SBen Grasprefix,
1172fe8fb19SBen Grasand the number will be read in base 16; otherwise, a zero
1182fe8fb19SBen Gras.Fa base
1192fe8fb19SBen Grasis taken as 10 (decimal) unless the next character is
1202fe8fb19SBen Gras.Ql 0 ,
1212fe8fb19SBen Grasin which case it is taken as 8 (octal).
1222fe8fb19SBen Gras.Pp
123*0a6a1f1dSLionel SambucThe remainder of the string is converted to an appropriate value
124*0a6a1f1dSLionel Sambucin the obvious manner,
125*0a6a1f1dSLionel Sambucstopping at the first character which is not a valid digit in the given base.
1262fe8fb19SBen Gras(In bases above 10, the letter
1272fe8fb19SBen Gras.Ql A
1282fe8fb19SBen Grasin either upper or lower case
1292fe8fb19SBen Grasrepresents 10,
1302fe8fb19SBen Gras.Ql B
1312fe8fb19SBen Grasrepresents 11, and so forth, with
1322fe8fb19SBen Gras.Ql Z
1332fe8fb19SBen Grasrepresenting 35.)
1342fe8fb19SBen Gras.Pp
1352fe8fb19SBen GrasIf
1362fe8fb19SBen Gras.Fa endptr
137*0a6a1f1dSLionel Sambucis non-nil, the functions store the address of the first invalid character in
1382fe8fb19SBen Gras.Fa *endptr .
1392fe8fb19SBen GrasIf there were no digits at all, however,
140*0a6a1f1dSLionel Sambucthe functions store the original value of
1412fe8fb19SBen Gras.Fa nptr
1422fe8fb19SBen Grasin
1432fe8fb19SBen Gras.Fa *endptr .
1442fe8fb19SBen Gras(Thus, if
1452fe8fb19SBen Gras.Fa *nptr
1462fe8fb19SBen Grasis not
1472fe8fb19SBen Gras.Ql \e0
1482fe8fb19SBen Grasbut
1492fe8fb19SBen Gras.Fa **endptr
1502fe8fb19SBen Grasis
1512fe8fb19SBen Gras.Ql \e0
1522fe8fb19SBen Grason return, the entire string was valid.)
1532fe8fb19SBen Gras.Sh RETURN VALUES
1542fe8fb19SBen GrasThe
1552fe8fb19SBen Gras.Fn strtol
1562fe8fb19SBen Grasfunction
1572fe8fb19SBen Grasreturns the result of the conversion,
1582fe8fb19SBen Grasunless the value would underflow or overflow.
1592fe8fb19SBen GrasIf an underflow occurs,
1602fe8fb19SBen Gras.Fn strtol
1612fe8fb19SBen Grasreturns
1622fe8fb19SBen Gras.Dv LONG_MIN ,
1632fe8fb19SBen Gras.Fn strtoll
1642fe8fb19SBen Grasreturns
1652fe8fb19SBen Gras.Dv LLONG_MIN ,
1662fe8fb19SBen Grasand
1672fe8fb19SBen Gras.Fn strtoimax
1682fe8fb19SBen Grasreturns
1692fe8fb19SBen Gras.Dv INTMAX_MIN .
1702fe8fb19SBen GrasIf an overflow occurs,
1712fe8fb19SBen Gras.Fn strtol
1722fe8fb19SBen Grasreturns
1732fe8fb19SBen Gras.Dv LONG_MAX ,
1742fe8fb19SBen Gras.Fn strtoll
1752fe8fb19SBen Grasreturns
1762fe8fb19SBen Gras.Dv LLONG_MAX ,
1772fe8fb19SBen Grasand
1782fe8fb19SBen Gras.Fn strtoimax
1792fe8fb19SBen Grasreturns
1802fe8fb19SBen Gras.Dv INTMAX_MAX .
1812fe8fb19SBen GrasIn these cases,
1822fe8fb19SBen Gras.Va errno
1832fe8fb19SBen Grasis set to
1842fe8fb19SBen Gras.Er ERANGE .
1852fe8fb19SBen GrasIf the
1862fe8fb19SBen Gras.Fa base
1872fe8fb19SBen Grasargument is not supported then
1882fe8fb19SBen Gras.Va errno
1892fe8fb19SBen Grasis set to
1902fe8fb19SBen Gras.Er EINVAL
1912fe8fb19SBen Grasand the functions return 0.
1922fe8fb19SBen Gras.Pp
1932fe8fb19SBen GrasIf no error occurs,
1942fe8fb19SBen Gras.Va errno
1952fe8fb19SBen Grasis left unchanged.
1962fe8fb19SBen GrasThis behavior (which is unlike most library functions) is guaranteed
1972fe8fb19SBen Grasby the pertinent standards.
1982fe8fb19SBen Gras.Sh EXAMPLES
1992fe8fb19SBen GrasBecause the return value of
2002fe8fb19SBen Gras.Fn strtol
2012fe8fb19SBen Grascannot be used unambiguously to detect an error,
2022fe8fb19SBen Gras.Va errno
2032fe8fb19SBen Grasis left unchanged after a successful call.
2042fe8fb19SBen GrasTo ensure that a string is a valid number (i.e., in range and containing no
2052fe8fb19SBen Grastrailing characters), clear
2062fe8fb19SBen Gras.Va errno
2072fe8fb19SBen Grasbeforehand explicitly, then check it afterwards:
2082fe8fb19SBen Gras.Bd -literal -offset indent
2092fe8fb19SBen Graschar *ep;
2102fe8fb19SBen Graslong lval;
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras\&...
2132fe8fb19SBen Gras
2142fe8fb19SBen Graserrno = 0;
2152fe8fb19SBen Graslval = strtol(buf, \*[Am]ep, 10);
2162fe8fb19SBen Grasif (buf[0] == '\e0' || *ep != '\e0')
2172fe8fb19SBen Gras	goto not_a_number;
2182fe8fb19SBen Grasif (errno == ERANGE \*[Am]\*[Am] (lval == LONG_MAX || lval == LONG_MIN))
2192fe8fb19SBen Gras	goto out_of_range;
2202fe8fb19SBen Gras.Ed
2212fe8fb19SBen Gras.Pp
2222fe8fb19SBen GrasThis example will accept
2232fe8fb19SBen Gras.Dq 12
2242fe8fb19SBen Grasbut not
2252fe8fb19SBen Gras.Dq 12foo
2262fe8fb19SBen Grasor
2272fe8fb19SBen Gras.Dq 12\en .
2282fe8fb19SBen GrasIf trailing whitespace is acceptable, further checks must be done on
2292fe8fb19SBen Gras.Va *ep ;
2302fe8fb19SBen Grasalternately, use
2312fe8fb19SBen Gras.Xr sscanf 3 .
2322fe8fb19SBen Gras.Pp
2332fe8fb19SBen GrasIf
2342fe8fb19SBen Gras.Fn strtol
2352fe8fb19SBen Grasis being used instead of
2362fe8fb19SBen Gras.Xr atoi 3 ,
2372fe8fb19SBen Graserror checking is further complicated because the desired return value is an
2382fe8fb19SBen Gras.Li int
2392fe8fb19SBen Grasrather than a
2402fe8fb19SBen Gras.Li long ;
2412fe8fb19SBen Grashowever, on some architectures integers and long integers are the same size.
2422fe8fb19SBen GrasThus the following is necessary:
2432fe8fb19SBen Gras.Bd -literal -offset indent
2442fe8fb19SBen Graschar *ep;
2452fe8fb19SBen Grasint ival;
2462fe8fb19SBen Graslong lval;
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras\&...
2492fe8fb19SBen Gras
2502fe8fb19SBen Graserrno = 0;
2512fe8fb19SBen Graslval = strtol(buf, \*[Am]ep, 10);
2522fe8fb19SBen Grasif (buf[0] == '\e0' || *ep != '\e0')
2532fe8fb19SBen Gras	goto not_a_number;
2542fe8fb19SBen Grasif ((errno == ERANGE \*[Am]\*[Am] (lval == LONG_MAX || lval == LONG_MIN)) ||
2552fe8fb19SBen Gras    (lval \*[Gt] INT_MAX || lval \*[Lt] INT_MIN))
2562fe8fb19SBen Gras	goto out_of_range;
2572fe8fb19SBen Grasival = lval;
2582fe8fb19SBen Gras.Ed
2592fe8fb19SBen Gras.Sh ERRORS
2602fe8fb19SBen Gras.Bl -tag -width Er
2612fe8fb19SBen Gras.It Bq Er EINVAL
2622fe8fb19SBen GrasThe
2632fe8fb19SBen Gras.Ar base
2642fe8fb19SBen Grasis not between 2 and 36 and does not contain the special value 0.
2652fe8fb19SBen Gras.It Bq Er ERANGE
2662fe8fb19SBen GrasThe given string was out of range; the value converted has been clamped.
2672fe8fb19SBen Gras.El
2682fe8fb19SBen Gras.Sh SEE ALSO
2692fe8fb19SBen Gras.Xr atof 3 ,
2702fe8fb19SBen Gras.Xr atoi 3 ,
2712fe8fb19SBen Gras.Xr atol 3 ,
2722fe8fb19SBen Gras.Xr atoll 3 ,
2732fe8fb19SBen Gras.Xr strtod 3 ,
274*0a6a1f1dSLionel Sambuc.Xr strtou 3 ,
2752fe8fb19SBen Gras.Xr strtoul 3 ,
2762fe8fb19SBen Gras.Xr strtoull 3 ,
2772fe8fb19SBen Gras.Xr strtoumax 3
2782fe8fb19SBen Gras.Sh STANDARDS
2792fe8fb19SBen GrasThe
2802fe8fb19SBen Gras.Fn strtol
2812fe8fb19SBen Grasfunction
2822fe8fb19SBen Grasconforms to
2832fe8fb19SBen Gras.St -ansiC .
2842fe8fb19SBen GrasThe
2852fe8fb19SBen Gras.Fn strtoll
2862fe8fb19SBen Grasand
2872fe8fb19SBen Gras.Fn strtoimax
2882fe8fb19SBen Grasfunctions conform to
2892fe8fb19SBen Gras.St -isoC-99 .
290*0a6a1f1dSLionel Sambuc.Pp
291*0a6a1f1dSLionel SambucThe
292*0a6a1f1dSLionel Sambuc.Fn strtoq
293*0a6a1f1dSLionel Sambucfunction is a
294*0a6a1f1dSLionel Sambuc.Bx
295*0a6a1f1dSLionel Sambuclegacy function equivalent to
296*0a6a1f1dSLionel Sambuc.Fn strtoll
297*0a6a1f1dSLionel Sambucand should not be used in a new code.
2982fe8fb19SBen Gras.Sh BUGS
2992fe8fb19SBen GrasIgnores the current locale.
300