xref: /minix3/lib/libc/stdlib/strtou.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1.\"	$NetBSD: strtou.3,v 1.3 2015/07/11 15:51:33 wiz Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     from: @(#)strtoul.3	8.1 (Berkeley) 6/4/93
35.\"
36.\" Created by Kamil Rytarowski, based on ID:
37.\" NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp
38.\"
39.Dd April 30, 2015
40.Dt STRTOU 3
41.Os
42.Sh NAME
43.Nm strtou
44.Nd convert a string to an uintmax_t integer
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In inttypes.h
49.Ft uintmax_t
50.Fo strtou
51.Fa "const char * restrict nptr"
52.Fa "char ** restrict endptr"
53.Fa "int base"
54.Fa "uintmax_t lo"
55.Fa "uintmax_t hi"
56.Fa "int *rstatus"
57.Fc
58.Sh DESCRIPTION
59The
60.Fn strtou
61function converts the string in
62.Fa nptr
63to an
64.Ft uintmax_t
65value.
66The
67.Fn strtou
68function uses internally
69.Xr strtoumax 3
70and ensures that the result is always in the range [
71.Fa lo ..
72.Fa hi
73].
74In adddition it always places
75.Dv 0
76on success or a conversion status in the
77.Fa rstatus
78argument, avoiding the
79.Dv errno
80gymnastics the other functions require.
81The
82.Fa rstatus
83argument can be
84.Dv NULL
85if conversion status is to be ignored.
86.Pp
87The string may begin with an arbitrary amount of white space
88(as determined by
89.Xr isspace 3 )
90followed by a single optional
91.Ql +
92or
93.Ql -
94sign.
95If
96.Fa base
97is zero or 16,
98the string may then include a
99.Ql 0x
100prefix,
101and 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 an
108.Em uintmax_t
109value in the obvious manner,
110stopping at the end of the string
111or at the first character that does not produce a valid digit
112in the given base.
113(In bases above 10, the letter
114.Ql A
115in either upper or lower case
116represents 10,
117.Ql B
118represents 11, and so forth, with
119.Ql Z
120representing 35.)
121.Pp
122If
123.Fa endptr
124is non-nil,
125.Fn strtou
126stores the address of the first invalid character in
127.Fa *endptr .
128If there were no digits at all, however,
129.Fn strtou
130stores the original value of
131.Fa nptr
132in
133.Fa *endptr .
134(Thus, if
135.Fa *nptr
136is not
137.Ql \e0
138but
139.Fa **endptr
140is
141.Ql \e0
142on return, the entire string was valid.)
143.Sh RETURN VALUES
144The
145.Fn strtou
146function
147always returns the closest value in the range specified by
148the
149.Fa lo
150and
151.Fa hi
152arguments.
153.Pp
154The
155.Va errno
156value is guaranteed to be left unchanged.
157.Pp
158Errors are stored as the conversion status in the
159.Fa rstatus
160argument.
161.Sh EXAMPLES
162The following example will always return a number in
163.Dv [1..99]
164range no matter what the input is, and warn if the conversion failed.
165.Bd -literal -offset indent
166int e;
167uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e);
168if (e)
169	warnc(e, "conversion of `%s' to a number failed, using %ju",
170	    buf, lval);
171.Ed
172.Sh ERRORS
173.Bl -tag -width Er
174.It Bq Er ECANCELED
175The string did not contain any characters that were converted.
176.It Bq Er EINVAL
177The
178.Ar base
179is not between 2 and 36 and does not contain the special value 0.
180.It Bq Er ENOTSUP
181The string contained non-numeric characters that did not get converted.
182In this case,
183.Fa endptr
184points to the first unconverted character.
185.It Bq Er ERANGE
186The given string was out of range; the value converted has been clamped; or
187the range given was invalid, i.e.
188.Fa lo
189\*[Gt]
190.Fa hi .
191.El
192.Sh SEE ALSO
193.Xr atof 3 ,
194.Xr atoi 3 ,
195.Xr atol 3 ,
196.Xr atoll 3 ,
197.Xr strtod 3 ,
198.Xr strtoi 3 ,
199.Xr strtoimax 3 ,
200.Xr strtol 3 ,
201.Xr strtoll 3 ,
202.Xr strtoul 3 ,
203.Xr strtoull 3 ,
204.Xr strtoumax 3
205.Sh STANDARDS
206The
207.Fn strtou
208function is a
209.Nx
210extension.
211.Sh HISTORY
212The
213.Fn strtou
214function first appeared in
215.Nx 7 .
216.Ox
217introduced the
218.Fn strtonum 3
219function for the same purpose, but the interface makes it impossible to
220properly differentiate illegal returns.
221.Sh BUGS
222Ignores the current locale.
223