summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/strtonum.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/strtonum.3')
-rw-r--r--src/lib/libc/stdlib/strtonum.3114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3
new file mode 100644
index 0000000000..31aae9075b
--- /dev/null
+++ b/src/lib/libc/stdlib/strtonum.3
@@ -0,0 +1,114 @@
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: strtonum.3,v 1.1 2004/05/03 17:09:24 tedu Exp $
33.\"
34.Dd April 29, 2004
35.Dt STRTONUM 3
36.Os
37.Sh NAME
38.Nm strtonum
39.Nd "reliably convert string value to an integer"
40.Sh SYNOPSIS
41.Fd #include <stdlib.h>
42.Fd #include <limits.h>
43.Ft unsigned long long
44.Fo strtonum
45.Fa "const char *nptr"
46.Fa "long long minval"
47.Fa "unsigned long long maxval"
48.Fa "const char **errstr"
49.Fc
50.Sh DESCRIPTION
51The
52.Fn strtonum
53function converts the string in
54.Fa nptr
55to an
56.Li unsigned long long
57value.
58The conversion is done according to base 10.
59Negative values may be obtained by casting the result.
60.Pp
61The string may begin with an arbitrary amount of whitespace
62(as determined by
63.Xr isspace 3 )
64followed by a single optional
65.Ql +
66or
67.Ql -
68sign.
69.Pp
70The remainder of the string is converted to an
71.Li unsigned long long
72value in the obvious manner,
73stopping at the first character which is not a valid digit
74in the given base.
75.Pp
76The value obtained is then checked against the provided
77.Fa minval
78and
79.Fa maxval
80bounds.
81If
82.Fa errstr
83is non-null,
84.Fn strtonum
85stores an error string in
86.Fa *endptr
87indicating the failure.
88.Sh RETURN VALUES
89The
90.Fn strtol
91function returns the result of the conversion,
92unless the value would underflow or overflow.
93On error, 0 is returned.
94.Sh ERRORS
95.Bl -tag -width Er
96.It Bq Er ERANGE
97The given string was out of range.
98.It Bq Er EINVAL
99The given string did not consist solely of a valid number.
100.El
101.Sh SEE ALSO
102.Xr atof 3 ,
103.Xr atoi 3 ,
104.Xr atol 3 ,
105.Xr atoll 3 ,
106.Xr sscanf 3 ,
107.Xr strtol 3 ,
108.Xr strtod 3 ,
109.Xr strtoul 3
110.Sh STANDARDS
111.Fn strtonum
112is an
113.Ox
114extension.