diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtoul.3')
| -rw-r--r-- | src/lib/libc/stdlib/strtoul.3 | 147 |
1 files changed, 107 insertions, 40 deletions
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 index db551b0141..4f6d6a51f9 100644 --- a/src/lib/libc/stdlib/strtoul.3 +++ b/src/lib/libc/stdlib/strtoul.3 | |||
| @@ -13,11 +13,7 @@ | |||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
| 15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
| 16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
| 22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
| 23 | .\" | 19 | .\" |
| @@ -33,21 +29,27 @@ | |||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
| 35 | .\" | 31 | .\" |
| 36 | .\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 | 32 | .\" $OpenBSD: strtoul.3,v 1.15 2003/06/02 20:18:38 millert Exp $ |
| 37 | .\" $Id: strtoul.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 38 | .\" | 33 | .\" |
| 39 | .Dd June 25, 1992 | 34 | .Dd June 25, 1992 |
| 40 | .Dt STRTOUL 3 | 35 | .Dt STRTOUL 3 |
| 41 | .Os | 36 | .Os |
| 42 | .Sh NAME | 37 | .Sh NAME |
| 43 | .Nm strtoul, strtouq | 38 | .Nm strtoul , |
| 44 | .Nd convert a string to an unsigned long or uquad_t integer | 39 | .Nm strtoull , |
| 40 | .Nm strtouq | ||
| 41 | .Nd "convert a string to an unsigned long or unsigned long long integer" | ||
| 45 | .Sh SYNOPSIS | 42 | .Sh SYNOPSIS |
| 46 | .Fd #include <stdlib.h> | 43 | .Fd #include <stdlib.h> |
| 47 | .Fd #include <limits.h> | 44 | .Fd #include <limits.h> |
| 48 | .Ft unsigned long | 45 | .Ft unsigned long |
| 49 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | 46 | .Fn strtoul "const char *nptr" "char **endptr" "int base" |
| 50 | 47 | .Pp | |
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Fd #include <limits.h> | ||
| 50 | .Ft unsigned long long | ||
| 51 | .Fn strtoull "const char *nptr" "char **endptr" "int base" | ||
| 52 | .Pp | ||
| 51 | .Fd #include <sys/types.h> | 53 | .Fd #include <sys/types.h> |
| 52 | .Fd #include <stdlib.h> | 54 | .Fd #include <stdlib.h> |
| 53 | .Fd #include <limits.h> | 55 | .Fd #include <limits.h> |
| @@ -56,26 +58,34 @@ | |||
| 56 | .Sh DESCRIPTION | 58 | .Sh DESCRIPTION |
| 57 | The | 59 | The |
| 58 | .Fn strtoul | 60 | .Fn strtoul |
| 59 | function | 61 | function converts the string in |
| 60 | converts the string in | ||
| 61 | .Fa nptr | 62 | .Fa nptr |
| 62 | to an | 63 | to an |
| 63 | .Em unsigned long | 64 | .Li unsigned long |
| 64 | value. | 65 | value. |
| 65 | The | 66 | The |
| 66 | .Fn strtouq | 67 | .Fn strtoull |
| 67 | function | 68 | function converts the string in |
| 68 | converts the string in | ||
| 69 | .Fa nptr | 69 | .Fa nptr |
| 70 | to a | 70 | to an |
| 71 | .Em u_quad_t | 71 | .Li unsigned long long |
| 72 | value. | 72 | value. |
| 73 | The | ||
| 74 | .Fn strtouq | ||
| 75 | function is a deprecated equivalent of | ||
| 76 | .Fn strtoull | ||
| 77 | and is provided for backwards compatibility with legacy programs. | ||
| 73 | The conversion is done according to the given | 78 | The conversion is done according to the given |
| 74 | .Fa base , | 79 | .Fa base , |
| 75 | which must be between 2 and 36 inclusive, | 80 | which must be a number between 2 and 36 inclusive |
| 76 | or be the special value 0. | 81 | or the special value 0. |
| 82 | If the string in | ||
| 83 | .Fa nptr | ||
| 84 | represents a negative number, it will be converted to its unsigned equivalent. | ||
| 85 | This behavior is consistent with what happens when a signed integer type is | ||
| 86 | cast to its unsigned counterpart. | ||
| 77 | .Pp | 87 | .Pp |
| 78 | The string may begin with an arbitrary amount of white space | 88 | The string may begin with an arbitrary amount of whitespace |
| 79 | (as determined by | 89 | (as determined by |
| 80 | .Xr isspace 3 ) | 90 | .Xr isspace 3 ) |
| 81 | followed by a single optional | 91 | followed by a single optional |
| @@ -85,26 +95,22 @@ or | |||
| 85 | sign. | 95 | sign. |
| 86 | If | 96 | If |
| 87 | .Fa base | 97 | .Fa base |
| 88 | is zero or 16, | 98 | is zero or 16, the string may then include a |
| 89 | the string may then include a | ||
| 90 | .Ql 0x | 99 | .Ql 0x |
| 91 | prefix, | 100 | prefix, and the number will be read in base 16; otherwise, a zero |
| 92 | and the number will be read in base 16; otherwise, a zero | ||
| 93 | .Fa base | 101 | .Fa base |
| 94 | is taken as 10 (decimal) unless the next character is | 102 | is taken as 10 (decimal) unless the next character is |
| 95 | .Ql 0 , | 103 | .Ql 0 , |
| 96 | in which case it is taken as 8 (octal). | 104 | in which case it is taken as 8 (octal). |
| 97 | .Pp | 105 | .Pp |
| 98 | The remainder of the string is converted to an | 106 | The remainder of the string is converted to an |
| 99 | .Em unsigned long | 107 | .Li unsigned long |
| 100 | value in the obvious manner, | 108 | value in the obvious manner, stopping at the end of the string |
| 101 | stopping at the end of the string | ||
| 102 | or at the first character that does not produce a valid digit | 109 | or at the first character that does not produce a valid digit |
| 103 | in the given base. | 110 | in the given base. |
| 104 | (In bases above 10, the letter | 111 | (In bases above 10, the letter |
| 105 | .Ql A | 112 | .Ql A |
| 106 | in either upper or lower case | 113 | in either upper or lower case represents 10, |
| 107 | represents 10, | ||
| 108 | .Ql B | 114 | .Ql B |
| 109 | represents 11, and so forth, with | 115 | represents 11, and so forth, with |
| 110 | .Ql Z | 116 | .Ql Z |
| @@ -112,7 +118,7 @@ representing 35.) | |||
| 112 | .Pp | 118 | .Pp |
| 113 | If | 119 | If |
| 114 | .Fa endptr | 120 | .Fa endptr |
| 115 | is non nil, | 121 | is non-null, |
| 116 | .Fn strtoul | 122 | .Fn strtoul |
| 117 | stores the address of the first invalid character in | 123 | stores the address of the first invalid character in |
| 118 | .Fa *endptr . | 124 | .Fa *endptr . |
| @@ -134,12 +140,18 @@ on return, the entire string was valid.) | |||
| 134 | .Sh RETURN VALUES | 140 | .Sh RETURN VALUES |
| 135 | The | 141 | The |
| 136 | .Fn strtoul | 142 | .Fn strtoul |
| 137 | function | 143 | function returns the result of the conversion, |
| 138 | returns either the result of the conversion | 144 | unless the value would overflow, in which case |
| 139 | or, if there was a leading minus sign, | 145 | .Dv ULONG_MAX |
| 140 | the negation of the result of the conversion, | 146 | is returned and |
| 141 | unless the original (non-negated) value would overflow; | 147 | .Va errno |
| 142 | in the latter case, | 148 | is set to |
| 149 | .Er ERANGE . | ||
| 150 | If there was a leading minus sign, | ||
| 151 | .Fn strtoul | ||
| 152 | returns the (unsigned) negation of the absolute value of the number, unless | ||
| 153 | the absolute value would overflow. | ||
| 154 | In this case, | ||
| 143 | .Fn strtoul | 155 | .Fn strtoul |
| 144 | returns | 156 | returns |
| 145 | .Dv ULONG_MAX | 157 | .Dv ULONG_MAX |
| @@ -147,18 +159,73 @@ and sets the global variable | |||
| 147 | .Va errno | 159 | .Va errno |
| 148 | to | 160 | to |
| 149 | .Er ERANGE . | 161 | .Er ERANGE . |
| 162 | .Pp | ||
| 163 | The | ||
| 164 | .Fn strtoull | ||
| 165 | function has identical return values except that | ||
| 166 | .Dv ULLONG_MAX | ||
| 167 | is used to indicate overflow. | ||
| 168 | .Pp | ||
| 169 | There is no way to determine if | ||
| 170 | .Fn strtoul | ||
| 171 | has processed a negative number (and returned an unsigned value) short of | ||
| 172 | examining the string in | ||
| 173 | .Fa nptr | ||
| 174 | directly. | ||
| 175 | .Sh EXAMPLES | ||
| 176 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
| 177 | trailing characters) requires clearing | ||
| 178 | .Va errno | ||
| 179 | beforehand explicitly since | ||
| 180 | .Va errno | ||
| 181 | is not changed on a successful call to | ||
| 182 | .Fn strtoul , | ||
| 183 | and the return value of | ||
| 184 | .Fn strtoul | ||
| 185 | cannot be used unambiguously to signal an error: | ||
| 186 | .Bd -literal -offset indent | ||
| 187 | char *ep; | ||
| 188 | unsigned long ulval; | ||
| 189 | |||
| 190 | \&... | ||
| 191 | |||
| 192 | errno = 0; | ||
| 193 | ulval = strtoul(buf, &ep, 10); | ||
| 194 | if (buf[0] == '\e0' || *ep != '\e0') | ||
| 195 | goto not_a_number; | ||
| 196 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
| 197 | goto out_of_range; | ||
| 198 | .Ed | ||
| 199 | .Pp | ||
| 200 | This example will accept | ||
| 201 | .Dq 12 | ||
| 202 | but not | ||
| 203 | .Dq 12foo | ||
| 204 | or | ||
| 205 | .Dq 12\en . | ||
| 206 | If trailing whitespace is acceptable, further checks must be done on | ||
| 207 | .Va *ep ; | ||
| 208 | alternately, use | ||
| 209 | .Xr sscanf 3 . | ||
| 150 | .Sh ERRORS | 210 | .Sh ERRORS |
| 151 | .Bl -tag -width Er | 211 | .Bl -tag -width Er |
| 152 | .It Bq Er ERANGE | 212 | .It Bq Er ERANGE |
| 153 | The given string was out of range; the value converted has been clamped. | 213 | The given string was out of range; the value converted has been clamped. |
| 154 | .El | 214 | .El |
| 155 | .Sh SEE ALSO | 215 | .Sh SEE ALSO |
| 216 | .Xr sscanf 3 , | ||
| 156 | .Xr strtol 3 | 217 | .Xr strtol 3 |
| 157 | .Sh STANDARDS | 218 | .Sh STANDARDS |
| 158 | The | 219 | The |
| 159 | .Fn strtoul | 220 | .Fn strtoul |
| 160 | function | 221 | and |
| 161 | conforms to | 222 | .Fn strtoull |
| 162 | .St -ansiC . | 223 | functions conform to |
| 224 | .St -ansiC-99 . | ||
| 225 | The | ||
| 226 | .Fn strtouq | ||
| 227 | function is a | ||
| 228 | .Bx | ||
| 229 | extension and is provided for backwards compatibility with legacy programs. | ||
| 163 | .Sh BUGS | 230 | .Sh BUGS |
| 164 | Ignores the current locale. | 231 | Ignores the current locale. |
