diff options
author | tedu <> | 2004-05-03 17:09:24 +0000 |
---|---|---|
committer | tedu <> | 2004-05-03 17:09:24 +0000 |
commit | 797f5f15358d4c8b5d139e9695af908f64130909 (patch) | |
tree | c424d06054645d9a2533814fa88b2a4d49890d58 /src/lib/libc/stdlib/strtonum.3 | |
parent | 93ef721c4aced997f082c99d8f900fa2b74a7f02 (diff) | |
download | openbsd-797f5f15358d4c8b5d139e9695af908f64130909.tar.gz openbsd-797f5f15358d4c8b5d139e9695af908f64130909.tar.bz2 openbsd-797f5f15358d4c8b5d139e9695af908f64130909.zip |
strtonum, a nicer version of strtoll, by millert and myself.
ok deraadt@ millert@
Diffstat (limited to 'src/lib/libc/stdlib/strtonum.3')
-rw-r--r-- | src/lib/libc/stdlib/strtonum.3 | 114 |
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 | ||
51 | The | ||
52 | .Fn strtonum | ||
53 | function converts the string in | ||
54 | .Fa nptr | ||
55 | to an | ||
56 | .Li unsigned long long | ||
57 | value. | ||
58 | The conversion is done according to base 10. | ||
59 | Negative values may be obtained by casting the result. | ||
60 | .Pp | ||
61 | The string may begin with an arbitrary amount of whitespace | ||
62 | (as determined by | ||
63 | .Xr isspace 3 ) | ||
64 | followed by a single optional | ||
65 | .Ql + | ||
66 | or | ||
67 | .Ql - | ||
68 | sign. | ||
69 | .Pp | ||
70 | The remainder of the string is converted to an | ||
71 | .Li unsigned long long | ||
72 | value in the obvious manner, | ||
73 | stopping at the first character which is not a valid digit | ||
74 | in the given base. | ||
75 | .Pp | ||
76 | The value obtained is then checked against the provided | ||
77 | .Fa minval | ||
78 | and | ||
79 | .Fa maxval | ||
80 | bounds. | ||
81 | If | ||
82 | .Fa errstr | ||
83 | is non-null, | ||
84 | .Fn strtonum | ||
85 | stores an error string in | ||
86 | .Fa *endptr | ||
87 | indicating the failure. | ||
88 | .Sh RETURN VALUES | ||
89 | The | ||
90 | .Fn strtol | ||
91 | function returns the result of the conversion, | ||
92 | unless the value would underflow or overflow. | ||
93 | On error, 0 is returned. | ||
94 | .Sh ERRORS | ||
95 | .Bl -tag -width Er | ||
96 | .It Bq Er ERANGE | ||
97 | The given string was out of range. | ||
98 | .It Bq Er EINVAL | ||
99 | The 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 | ||
112 | is an | ||
113 | .Ox | ||
114 | extension. | ||