xref: /netbsd-src/lib/libc/stdlib/strtoul.3 (revision 01869ca4d24a86379a68731bf9706a9f0820fe4e)
1*01869ca4Swiz.\"	$NetBSD: strtoul.3,v 1.37 2017/07/03 21:32:50 wiz Exp $
26dda330eSthorpej.\"
35f8a6c06Sperry.\" Copyright (c) 1990, 1991, 1993
45f8a6c06Sperry.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" This code is derived from software contributed to Berkeley by
761f28255Scgd.\" Chris Torek and the American National Standards Committee X3,
861f28255Scgd.\" on Information Processing Systems.
961f28255Scgd.\"
1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without
1161f28255Scgd.\" modification, are permitted provided that the following conditions
1261f28255Scgd.\" are met:
1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1461f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd.\"    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd.\"    may be used to endorse or promote products derived from this software
2061f28255Scgd.\"    without specific prior written permission.
2161f28255Scgd.\"
2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd.\" SUCH DAMAGE.
3361f28255Scgd.\"
345f8a6c06Sperry.\"     from: @(#)strtoul.3	8.1 (Berkeley) 6/4/93
3561f28255Scgd.\"
3653ba0e3dSchristos.Dd November 13, 2015
3761f28255Scgd.Dt STRTOUL 3
3861f28255Scgd.Os
3961f28255Scgd.Sh NAME
404e252e8cSmrg.Nm strtoul ,
41e4d7c2e3Skleink.Nm strtoull ,
42fe3c9ceaSkleink.Nm strtoumax ,
434e252e8cSmrg.Nm strtouq
44e073a944Sjoerg.Nd convert a string to an unsigned long, unsigned long long, uintmax_t or u_quad_t integer
45312aca53Sperry.Sh LIBRARY
46312aca53Sperry.Lb libc
4761f28255Scgd.Sh SYNOPSIS
48472351e1Swiz.In stdlib.h
49472351e1Swiz.In limits.h
50e4d7c2e3Skleink.Ft unsigned long int
51fe3c9ceaSkleink.Fn strtoul "const char * restrict nptr" "char ** restrict endptr" "int base"
52e4d7c2e3Skleink.Ft unsigned long long int
53fe3c9ceaSkleink.Fn strtoull "const char * restrict nptr" "char ** restrict endptr" "int base"
5462698086Swiz.Pp
55472351e1Swiz.In inttypes.h
56fe3c9ceaSkleink.Ft uintmax_t
57fe3c9ceaSkleink.Fn strtoumax "const char * restrict nptr" "char ** restrict endptr" "int base"
5862698086Swiz.Pp
59472351e1Swiz.In sys/types.h
60472351e1Swiz.In stdlib.h
61472351e1Swiz.In limits.h
62bfc2593aSjtc.Ft u_quad_t
63fe3c9ceaSkleink.Fn strtouq "const char * restrict nptr" "char ** restrict endptr" "int base"
6461f28255Scgd.Sh DESCRIPTION
6561f28255ScgdThe
6661f28255Scgd.Fn strtoul
6761f28255Scgdfunction
6861f28255Scgdconverts the string in
6961f28255Scgd.Fa nptr
7061f28255Scgdto an
71f6b43a0cSwiz.Ft unsigned long int
72e4d7c2e3Skleinkvalue.
73e4d7c2e3SkleinkThe
74e4d7c2e3Skleink.Fn strtoull
75e4d7c2e3Skleinkfunction
76e4d7c2e3Skleinkconverts the string in
77e4d7c2e3Skleink.Fa nptr
78e4d7c2e3Skleinkto an
79f6b43a0cSwiz.Ft unsigned long long int
80bfc2593aSjtcvalue.
81bfc2593aSjtcThe
82fe3c9ceaSkleink.Fn strtoumax
83fe3c9ceaSkleinkfunction
84fe3c9ceaSkleinkconverts the string in
85fe3c9ceaSkleink.Fa nptr
86fe3c9ceaSkleinkto an
87f6b43a0cSwiz.Ft uintmax_t
88fe3c9ceaSkleinkvalue.
89fe3c9ceaSkleinkThe
90bfc2593aSjtc.Fn strtouq
91bfc2593aSjtcfunction
92bfc2593aSjtcconverts the string in
93bfc2593aSjtc.Fa nptr
94bfc2593aSjtcto a
95f6b43a0cSwiz.Ft u_quad_t
96bfc2593aSjtcvalue.
9735d355f3Schristos.Pp
98bfc2593aSjtcThe conversion is done according to the given
9961f28255Scgd.Fa base ,
10061f28255Scgdwhich must be between 2 and 36 inclusive,
10161f28255Scgdor be the special value 0.
10261f28255Scgd.Pp
10361f28255ScgdThe string may begin with an arbitrary amount of white space
10461f28255Scgd(as determined by
10561f28255Scgd.Xr isspace 3 )
10661f28255Scgdfollowed by a single optional
10761f28255Scgd.Ql +
10861f28255Scgdor
10961f28255Scgd.Ql -
11061f28255Scgdsign.
11161f28255ScgdIf
11261f28255Scgd.Fa base
11361f28255Scgdis zero or 16,
11461f28255Scgdthe string may then include a
11561f28255Scgd.Ql 0x
1166ea48155Schristosor
1176ea48155Schristos.Ql 0X
11861f28255Scgdprefix,
1196ea48155Schristosand the number will be read in base 16; otherwise,
12053ba0e3dSchristos.\" if the
12153ba0e3dSchristos.\" .Fa base
12253ba0e3dSchristos.\" is zero or 2,
12353ba0e3dSchristos.\" the string may then include a
12453ba0e3dSchristos.\" .Ql 0b
12553ba0e3dSchristos.\" or
12653ba0e3dSchristos.\" .Ql 0B
12753ba0e3dSchristos.\" prefix,
12853ba0e3dSchristos.\" and the number will be read in base 2; otherwise,
1296ea48155Schristosa zero
13061f28255Scgd.Fa base
13161f28255Scgdis taken as 10 (decimal) unless the next character is
13261f28255Scgd.Ql 0 ,
13361f28255Scgdin which case it is taken as 8 (octal).
13461f28255Scgd.Pp
13535d355f3SchristosThe remainder of the string is converted to an appropriate
13661f28255Scgdvalue in the obvious manner,
13761f28255Scgdstopping at the end of the string
13861f28255Scgdor at the first character that does not produce a valid digit
13961f28255Scgdin the given base.
14061f28255Scgd(In bases above 10, the letter
14161f28255Scgd.Ql A
14261f28255Scgdin either upper or lower case
14361f28255Scgdrepresents 10,
14461f28255Scgd.Ql B
14561f28255Scgdrepresents 11, and so forth, with
14661f28255Scgd.Ql Z
14761f28255Scgdrepresenting 35.)
14861f28255Scgd.Pp
14961f28255ScgdIf
15061f28255Scgd.Fa endptr
15135d355f3Schristosis non-nil, the functions store the address of the first invalid character in
15261f28255Scgd.Fa *endptr .
15361f28255ScgdIf there were no digits at all, however,
15435d355f3Schristosthe functions store the original value of
15561f28255Scgd.Fa nptr
15661f28255Scgdin
15761f28255Scgd.Fa *endptr .
15861f28255Scgd(Thus, if
15961f28255Scgd.Fa *nptr
16061f28255Scgdis not
16161f28255Scgd.Ql \e0
16261f28255Scgdbut
16361f28255Scgd.Fa **endptr
16461f28255Scgdis
16561f28255Scgd.Ql \e0
16661f28255Scgdon return, the entire string was valid.)
16761f28255Scgd.Sh RETURN VALUES
16861f28255ScgdThe
16961f28255Scgd.Fn strtoul
17061f28255Scgdfunction
17161f28255Scgdreturns either the result of the conversion
17261f28255Scgdor, if there was a leading minus sign,
17361f28255Scgdthe negation of the result of the conversion,
17461f28255Scgdunless the original (non-negated) value would overflow;
17561f28255Scgdin the latter case,
17661f28255Scgd.Fn strtoul
17761f28255Scgdreturns
178fe3c9ceaSkleink.Dv ULONG_MAX ,
179fe3c9ceaSkleink.Fn strtoull
180fe3c9ceaSkleinkreturns
181fe3c9ceaSkleink.Dv ULLONG_MAX ,
182fe3c9ceaSkleink.Fn strtoumax
183fe3c9ceaSkleinkreturns
184fe3c9ceaSkleink.Dv UINTMAX_MAX ,
185304d2769Spooka.Fn strtouq
1865f6578a8Spookareturns
187304d2769Spooka.Dv UQUAD_MAX ,
188fe3c9ceaSkleinkand the global variable
18961f28255Scgd.Va errno
190fe3c9ceaSkleinkis set to
19161f28255Scgd.Er ERANGE .
192c5caadcfSyamt.Pp
193c5caadcfSyamtThere is no way to determine if
194c5caadcfSyamt.Fn strtoul
195c5caadcfSyamthas processed a negative number (and returned an unsigned value) short of
196c5caadcfSyamtexamining the string in
197c5caadcfSyamt.Fa nptr
198c5caadcfSyamtdirectly.
199fd673702SchristosIf the
200fd673702Schristos.Fa base
201fd673702Schristosargument is not supported then
202fd673702Schristos.Va errno
203fd673702Schristosis set to
204fd673702Schristos.Er EINVAL
205f6b43a0cSwizand the functions return 0.
2063cfdfcefSdholland.Pp
2073cfdfcefSdhollandIf no error occurs,
2083cfdfcefSdholland.Va errno
2093cfdfcefSdhollandis left unchanged.
2103cfdfcefSdhollandThis behavior (which is unlike most library functions) is guaranteed
2113cfdfcefSdhollandby the pertinent standards.
212c5caadcfSyamt.Sh EXAMPLES
2133cfdfcefSdhollandBecause the return value of
214c5caadcfSyamt.Fn strtoul
2153cfdfcefSdhollandcannot be used unambiguously to detect an error,
2163cfdfcefSdholland.Va errno
2173cfdfcefSdhollandis left unchanged after a successful call.
2183cfdfcefSdhollandTo ensure that a string is a valid number (i.e., in range and containing no
2193cfdfcefSdhollandtrailing characters), clear
2203cfdfcefSdholland.Va errno
2213cfdfcefSdhollandbeforehand explicitly, then check it afterwards:
222c5caadcfSyamt.Bd -literal -offset indent
223c5caadcfSyamtchar *ep;
224c5caadcfSyamtunsigned long ulval;
225c5caadcfSyamt
226c5caadcfSyamt\&...
227c5caadcfSyamt
228c5caadcfSyamterrno = 0;
229*01869ca4Swizulval = strtoul(buf, &ep, 10);
2302d9978a0Sriastradhif (ep == buf)
231c5caadcfSyamt	goto not_a_number;
2322d9978a0Sriastradhif (*ep != '\e0')
2332d9978a0Sriastradh	goto trailing_garbage;
2342d9978a0Sriastradhif (errno) {
2352d9978a0Sriastradh	assert(errno == ERANGE);
2362d9978a0Sriastradh	assert(ulval == ULONG_MAX);
237c5caadcfSyamt	goto out_of_range;
2382d9978a0Sriastradh}
239c5caadcfSyamt.Ed
240c5caadcfSyamt.Pp
241c5caadcfSyamtThis example will accept
242c5caadcfSyamt.Dq 12
243c5caadcfSyamtbut not
244c5caadcfSyamt.Dq 12foo
245c5caadcfSyamtor
246c5caadcfSyamt.Dq 12\en .
247c5caadcfSyamtIf trailing whitespace is acceptable, further checks must be done on
248c5caadcfSyamt.Va *ep ;
249c5caadcfSyamtalternately, use
250c5caadcfSyamt.Xr sscanf 3 .
25161f28255Scgd.Sh ERRORS
252a66bb899Sjtc.Bl -tag -width Er
253fd673702Schristos.It Bq Er EINVAL
254fd673702SchristosThe
255fd673702Schristos.Ar base
256f6b43a0cSwizis not between 2 and 36 and does not contain the special value 0.
25761f28255Scgd.It Bq Er ERANGE
25861f28255ScgdThe given string was out of range; the value converted has been clamped.
25961f28255Scgd.El
26061f28255Scgd.Sh SEE ALSO
26135d355f3Schristos.Xr atof 3 ,
26235d355f3Schristos.Xr atoi 3 ,
26335d355f3Schristos.Xr atol 3 ,
26435d355f3Schristos.Xr atoll 3 ,
26535d355f3Schristos.Xr strtod 3 ,
266fbb651a2Schristos.Xr strtoi 3 ,
267593de75eSwiz.Xr strtoimax 3 ,
268fe3c9ceaSkleink.Xr strtol 3 ,
26935d355f3Schristos.Xr strtoll 3 ,
270593de75eSwiz.Xr strtou 3
27161f28255Scgd.Sh STANDARDS
27261f28255ScgdThe
27361f28255Scgd.Fn strtoul
27461f28255Scgdfunction
27561f28255Scgdconforms to
27661f28255Scgd.St -ansiC .
277e4d7c2e3SkleinkThe
278e4d7c2e3Skleink.Fn strtoull
279fe3c9ceaSkleinkand
280fe3c9ceaSkleink.Fn strtoumax
281fe3c9ceaSkleinkfunctions conform to
282dca9ccd3Swiz.St -isoC-99 .
28335d355f3Schristos.Pp
284fbb651a2SchristosThe
28535d355f3Schristos.Fn strtouq
28635d355f3Schristosfunction is a
28735d355f3Schristos.Bx
28835d355f3Schristoslegacy function equivalent to
28935d355f3Schristos.Fn strtoull
29035d355f3Schristosand should not be used in a new code.
29161f28255Scgd.Sh BUGS
29261f28255ScgdIgnores the current locale.
293