xref: /minix3/lib/libc/stdlib/strtoul.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: strtoul.3,v 1.31 2015/05/03 12:29:28 wiz 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: @(#)strtoul.3	8.1 (Berkeley) 6/4/93
352fe8fb19SBen Gras.\"
36*0a6a1f1dSLionel Sambuc.Dd April 30, 2015
372fe8fb19SBen Gras.Dt STRTOUL 3
382fe8fb19SBen Gras.Os
392fe8fb19SBen Gras.Sh NAME
402fe8fb19SBen Gras.Nm strtoul ,
412fe8fb19SBen Gras.Nm strtoull ,
422fe8fb19SBen Gras.Nm strtoumax ,
432fe8fb19SBen Gras.Nm strtouq
442fe8fb19SBen Gras.Nd convert a string to an unsigned long, unsigned long long, uintmax_t or uquad_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 unsigned long int
512fe8fb19SBen Gras.Fn strtoul "const char * restrict nptr" "char ** restrict endptr" "int base"
522fe8fb19SBen Gras.Ft unsigned long long int
532fe8fb19SBen Gras.Fn strtoull "const char * restrict nptr" "char ** restrict endptr" "int base"
542fe8fb19SBen Gras.Pp
552fe8fb19SBen Gras.In inttypes.h
562fe8fb19SBen Gras.Ft uintmax_t
572fe8fb19SBen Gras.Fn strtoumax "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 u_quad_t
632fe8fb19SBen Gras.Fn strtouq "const char * restrict nptr" "char ** restrict endptr" "int base"
642fe8fb19SBen Gras.Sh DESCRIPTION
652fe8fb19SBen GrasThe
662fe8fb19SBen Gras.Fn strtoul
672fe8fb19SBen Grasfunction
682fe8fb19SBen Grasconverts the string in
692fe8fb19SBen Gras.Fa nptr
702fe8fb19SBen Grasto an
712fe8fb19SBen Gras.Ft unsigned long int
722fe8fb19SBen Grasvalue.
732fe8fb19SBen GrasThe
742fe8fb19SBen Gras.Fn strtoull
752fe8fb19SBen Grasfunction
762fe8fb19SBen Grasconverts the string in
772fe8fb19SBen Gras.Fa nptr
782fe8fb19SBen Grasto an
792fe8fb19SBen Gras.Ft unsigned long long int
802fe8fb19SBen Grasvalue.
812fe8fb19SBen GrasThe
822fe8fb19SBen Gras.Fn strtoumax
832fe8fb19SBen Grasfunction
842fe8fb19SBen Grasconverts the string in
852fe8fb19SBen Gras.Fa nptr
862fe8fb19SBen Grasto an
872fe8fb19SBen Gras.Ft uintmax_t
882fe8fb19SBen Grasvalue.
892fe8fb19SBen GrasThe
902fe8fb19SBen Gras.Fn strtouq
912fe8fb19SBen Grasfunction
922fe8fb19SBen Grasconverts the string in
932fe8fb19SBen Gras.Fa nptr
942fe8fb19SBen Grasto a
952fe8fb19SBen Gras.Ft u_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
1242fe8fb19SBen Grasvalue in the obvious manner,
1252fe8fb19SBen Grasstopping at the end of the string
1262fe8fb19SBen Grasor at the first character that does not produce a valid digit
1272fe8fb19SBen Grasin the given base.
1282fe8fb19SBen Gras(In bases above 10, the letter
1292fe8fb19SBen Gras.Ql A
1302fe8fb19SBen Grasin either upper or lower case
1312fe8fb19SBen Grasrepresents 10,
1322fe8fb19SBen Gras.Ql B
1332fe8fb19SBen Grasrepresents 11, and so forth, with
1342fe8fb19SBen Gras.Ql Z
1352fe8fb19SBen Grasrepresenting 35.)
1362fe8fb19SBen Gras.Pp
1372fe8fb19SBen GrasIf
1382fe8fb19SBen Gras.Fa endptr
139*0a6a1f1dSLionel Sambucis non-nil, the functions store the address of the first invalid character in
1402fe8fb19SBen Gras.Fa *endptr .
1412fe8fb19SBen GrasIf there were no digits at all, however,
142*0a6a1f1dSLionel Sambucthe functions store the original value of
1432fe8fb19SBen Gras.Fa nptr
1442fe8fb19SBen Grasin
1452fe8fb19SBen Gras.Fa *endptr .
1462fe8fb19SBen Gras(Thus, if
1472fe8fb19SBen Gras.Fa *nptr
1482fe8fb19SBen Grasis not
1492fe8fb19SBen Gras.Ql \e0
1502fe8fb19SBen Grasbut
1512fe8fb19SBen Gras.Fa **endptr
1522fe8fb19SBen Grasis
1532fe8fb19SBen Gras.Ql \e0
1542fe8fb19SBen Grason return, the entire string was valid.)
1552fe8fb19SBen Gras.Sh RETURN VALUES
1562fe8fb19SBen GrasThe
1572fe8fb19SBen Gras.Fn strtoul
1582fe8fb19SBen Grasfunction
1592fe8fb19SBen Grasreturns either the result of the conversion
1602fe8fb19SBen Grasor, if there was a leading minus sign,
1612fe8fb19SBen Grasthe negation of the result of the conversion,
1622fe8fb19SBen Grasunless the original (non-negated) value would overflow;
1632fe8fb19SBen Grasin the latter case,
1642fe8fb19SBen Gras.Fn strtoul
1652fe8fb19SBen Grasreturns
1662fe8fb19SBen Gras.Dv ULONG_MAX ,
1672fe8fb19SBen Gras.Fn strtoull
1682fe8fb19SBen Grasreturns
1692fe8fb19SBen Gras.Dv ULLONG_MAX ,
1702fe8fb19SBen Gras.Fn strtoumax
1712fe8fb19SBen Grasreturns
1722fe8fb19SBen Gras.Dv UINTMAX_MAX ,
1732fe8fb19SBen Gras.Fn strtouq
1742fe8fb19SBen Grasreturns
1752fe8fb19SBen Gras.Dv UQUAD_MAX ,
1762fe8fb19SBen Grasand the global variable
1772fe8fb19SBen Gras.Va errno
1782fe8fb19SBen Grasis set to
1792fe8fb19SBen Gras.Er ERANGE .
1802fe8fb19SBen Gras.Pp
1812fe8fb19SBen GrasThere is no way to determine if
1822fe8fb19SBen Gras.Fn strtoul
1832fe8fb19SBen Grashas processed a negative number (and returned an unsigned value) short of
1842fe8fb19SBen Grasexamining the string in
1852fe8fb19SBen Gras.Fa nptr
1862fe8fb19SBen Grasdirectly.
1872fe8fb19SBen GrasIf the
1882fe8fb19SBen Gras.Fa base
1892fe8fb19SBen Grasargument is not supported then
1902fe8fb19SBen Gras.Va errno
1912fe8fb19SBen Grasis set to
1922fe8fb19SBen Gras.Er EINVAL
1932fe8fb19SBen Grasand the functions return 0.
1942fe8fb19SBen Gras.Pp
1952fe8fb19SBen GrasIf no error occurs,
1962fe8fb19SBen Gras.Va errno
1972fe8fb19SBen Grasis left unchanged.
1982fe8fb19SBen GrasThis behavior (which is unlike most library functions) is guaranteed
1992fe8fb19SBen Grasby the pertinent standards.
2002fe8fb19SBen Gras.Sh EXAMPLES
2012fe8fb19SBen GrasBecause the return value of
2022fe8fb19SBen Gras.Fn strtoul
2032fe8fb19SBen Grascannot be used unambiguously to detect an error,
2042fe8fb19SBen Gras.Va errno
2052fe8fb19SBen Grasis left unchanged after a successful call.
2062fe8fb19SBen GrasTo ensure that a string is a valid number (i.e., in range and containing no
2072fe8fb19SBen Grastrailing characters), clear
2082fe8fb19SBen Gras.Va errno
2092fe8fb19SBen Grasbeforehand explicitly, then check it afterwards:
2102fe8fb19SBen Gras.Bd -literal -offset indent
2112fe8fb19SBen Graschar *ep;
2122fe8fb19SBen Grasunsigned long ulval;
2132fe8fb19SBen Gras
2142fe8fb19SBen Gras\&...
2152fe8fb19SBen Gras
2162fe8fb19SBen Graserrno = 0;
2172fe8fb19SBen Grasulval = strtoul(buf, \*[Am]ep, 10);
2182fe8fb19SBen Grasif (buf[0] == '\e0' || *ep != '\e0')
2192fe8fb19SBen Gras	goto not_a_number;
2202fe8fb19SBen Grasif (errno == ERANGE \*[Am]\*[Am] ulval == ULONG_MAX)
2212fe8fb19SBen Gras	goto out_of_range;
2222fe8fb19SBen Gras.Ed
2232fe8fb19SBen Gras.Pp
2242fe8fb19SBen GrasThis example will accept
2252fe8fb19SBen Gras.Dq 12
2262fe8fb19SBen Grasbut not
2272fe8fb19SBen Gras.Dq 12foo
2282fe8fb19SBen Grasor
2292fe8fb19SBen Gras.Dq 12\en .
2302fe8fb19SBen GrasIf trailing whitespace is acceptable, further checks must be done on
2312fe8fb19SBen Gras.Va *ep ;
2322fe8fb19SBen Grasalternately, use
2332fe8fb19SBen Gras.Xr sscanf 3 .
2342fe8fb19SBen Gras.Sh ERRORS
2352fe8fb19SBen Gras.Bl -tag -width Er
2362fe8fb19SBen Gras.It Bq Er EINVAL
2372fe8fb19SBen GrasThe
2382fe8fb19SBen Gras.Ar base
2392fe8fb19SBen Grasis not between 2 and 36 and does not contain the special value 0.
2402fe8fb19SBen Gras.It Bq Er ERANGE
2412fe8fb19SBen GrasThe given string was out of range; the value converted has been clamped.
2422fe8fb19SBen Gras.El
2432fe8fb19SBen Gras.Sh SEE ALSO
244*0a6a1f1dSLionel Sambuc.Xr atof 3 ,
245*0a6a1f1dSLionel Sambuc.Xr atoi 3 ,
246*0a6a1f1dSLionel Sambuc.Xr atol 3 ,
247*0a6a1f1dSLionel Sambuc.Xr atoll 3 ,
248*0a6a1f1dSLionel Sambuc.Xr strtod 3 ,
249*0a6a1f1dSLionel Sambuc.Xr strtoi 3 ,
2502fe8fb19SBen Gras.Xr strtoimax 3 ,
2512fe8fb19SBen Gras.Xr strtol 3 ,
252*0a6a1f1dSLionel Sambuc.Xr strtoll 3 ,
253*0a6a1f1dSLionel Sambuc.Xr strtou 3
2542fe8fb19SBen Gras.Sh STANDARDS
2552fe8fb19SBen GrasThe
2562fe8fb19SBen Gras.Fn strtoul
2572fe8fb19SBen Grasfunction
2582fe8fb19SBen Grasconforms to
2592fe8fb19SBen Gras.St -ansiC .
2602fe8fb19SBen GrasThe
2612fe8fb19SBen Gras.Fn strtoull
2622fe8fb19SBen Grasand
2632fe8fb19SBen Gras.Fn strtoumax
2642fe8fb19SBen Grasfunctions conform to
2652fe8fb19SBen Gras.St -isoC-99 .
266*0a6a1f1dSLionel Sambuc.Pp
267*0a6a1f1dSLionel SambucThe
268*0a6a1f1dSLionel Sambuc.Fn strtouq
269*0a6a1f1dSLionel Sambucfunction is a
270*0a6a1f1dSLionel Sambuc.Bx
271*0a6a1f1dSLionel Sambuclegacy function equivalent to
272*0a6a1f1dSLionel Sambuc.Fn strtoull
273*0a6a1f1dSLionel Sambucand should not be used in a new code.
2742fe8fb19SBen Gras.Sh BUGS
2752fe8fb19SBen GrasIgnores the current locale.
276