diff options
Diffstat (limited to 'src/lib/libc/stdlib/strtol.3')
| -rw-r--r-- | src/lib/libc/stdlib/strtol.3 | 148 |
1 files changed, 113 insertions, 35 deletions
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 index 808ba90165..3f5375c5e3 100644 --- a/src/lib/libc/stdlib/strtol.3 +++ b/src/lib/libc/stdlib/strtol.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,49 +29,57 @@ | |||
| 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: @(#)strtol.3 5.4 (Berkeley) 6/25/92 | 32 | .\" $OpenBSD: strtol.3,v 1.13 2003/06/02 20:18:38 millert Exp $ |
| 37 | .\" $Id: strtol.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | 33 | .\" |
| 39 | .Dd June 25, 1992 | 34 | .Dd June 25, 1992 |
| 40 | .Dt STRTOL 3 | 35 | .Dt STRTOL 3 |
| 41 | .Os | 36 | .Os |
| 42 | .Sh NAME | 37 | .Sh NAME |
| 43 | .Nm strtol, strtoq | 38 | .Nm strtol , |
| 44 | .Nd convert string value to a long or quad_t integer | 39 | .Nm strtoll , |
| 40 | .Nm strtoq | ||
| 41 | .Nd "convert string value to a long or 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 long | 45 | .Ft long |
| 49 | .Fn strtol "char *nptr" "char **endptr" "int base" | 46 | .Fn strtol "const char *nptr" "char **endptr" "int base" |
| 50 | 47 | .Pp | |
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Fd #include <limits.h> | ||
| 50 | .Ft long long | ||
| 51 | .Fn strtoll "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> |
| 54 | .Ft quad_t | 56 | .Ft quad_t |
| 55 | .Fn strtoq "char *nptr" "char **endptr" "int base" | 57 | .Fn strtoq "const char *nptr" "char **endptr" "int base" |
| 56 | .Sh DESCRIPTION | 58 | .Sh DESCRIPTION |
| 57 | The | 59 | The |
| 58 | .Fn strtol | 60 | .Fn strtol |
| 59 | function | 61 | function converts the string in |
| 60 | converts the string in | ||
| 61 | .Fa nptr | 62 | .Fa nptr |
| 62 | to a | 63 | to a |
| 63 | .Em long | 64 | .Li long |
| 64 | value. | 65 | value. |
| 65 | The | 66 | The |
| 66 | .Fn strtoq | 67 | .Fn strtoll |
| 67 | function | 68 | function converts the string in |
| 68 | converts the string in | ||
| 69 | .Fa nptr | 69 | .Fa nptr |
| 70 | to a | 70 | to a |
| 71 | .Em quad_t | 71 | .Li long long |
| 72 | value. | 72 | value. |
| 73 | The | ||
| 74 | .Fn strtoq | ||
| 75 | function is a deprecated equivalent of | ||
| 76 | .Fn strtoll | ||
| 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 or the special value 0. |
| 76 | or be the special value 0. | ||
| 77 | .Pp | 81 | .Pp |
| 78 | The string may begin with an arbitrary amount of white space | 82 | The string may begin with an arbitrary amount of whitespace |
| 79 | (as determined by | 83 | (as determined by |
| 80 | .Xr isspace 3 ) | 84 | .Xr isspace 3 ) |
| 81 | followed by a single optional | 85 | followed by a single optional |
| @@ -85,25 +89,22 @@ or | |||
| 85 | sign. | 89 | sign. |
| 86 | If | 90 | If |
| 87 | .Fa base | 91 | .Fa base |
| 88 | is zero or 16, | 92 | is zero or 16, the string may then include a |
| 89 | the string may then include a | ||
| 90 | .Ql 0x | 93 | .Ql 0x |
| 91 | prefix, | 94 | 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 | 95 | .Fa base |
| 94 | is taken as 10 (decimal) unless the next character is | 96 | is taken as 10 (decimal) unless the next character is |
| 95 | .Ql 0 , | 97 | .Ql 0 , |
| 96 | in which case it is taken as 8 (octal). | 98 | in which case it is taken as 8 (octal). |
| 97 | .Pp | 99 | .Pp |
| 98 | The remainder of the string is converted to a | 100 | The remainder of the string is converted to a |
| 99 | .Em long | 101 | .Li long |
| 100 | value in the obvious manner, | 102 | value in the obvious manner, |
| 101 | stopping at the first character which is not a valid digit | 103 | stopping at the first character which is not a valid digit |
| 102 | in the given base. | 104 | in the given base. |
| 103 | (In bases above 10, the letter | 105 | (In bases above 10, the letter |
| 104 | .Ql A | 106 | .Ql A |
| 105 | in either upper or lower case | 107 | in either upper or lower case represents 10, |
| 106 | represents 10, | ||
| 107 | .Ql B | 108 | .Ql B |
| 108 | represents 11, and so forth, with | 109 | represents 11, and so forth, with |
| 109 | .Ql Z | 110 | .Ql Z |
| @@ -111,7 +112,7 @@ representing 35.) | |||
| 111 | .Pp | 112 | .Pp |
| 112 | If | 113 | If |
| 113 | .Fa endptr | 114 | .Fa endptr |
| 114 | is non nil, | 115 | is non-null, |
| 115 | .Fn strtol | 116 | .Fn strtol |
| 116 | stores the address of the first invalid character in | 117 | stores the address of the first invalid character in |
| 117 | .Fa *endptr . | 118 | .Fa *endptr . |
| @@ -133,8 +134,7 @@ on return, the entire string was valid.) | |||
| 133 | .Sh RETURN VALUES | 134 | .Sh RETURN VALUES |
| 134 | The | 135 | The |
| 135 | .Fn strtol | 136 | .Fn strtol |
| 136 | function | 137 | function returns the result of the conversion, |
| 137 | returns the result of the conversion, | ||
| 138 | unless the value would underflow or overflow. | 138 | unless the value would underflow or overflow. |
| 139 | If an underflow occurs, | 139 | If an underflow occurs, |
| 140 | .Fn strtol | 140 | .Fn strtol |
| @@ -148,6 +148,76 @@ In both cases, | |||
| 148 | .Va errno | 148 | .Va errno |
| 149 | is set to | 149 | is set to |
| 150 | .Er ERANGE . | 150 | .Er ERANGE . |
| 151 | .Pp | ||
| 152 | The | ||
| 153 | .Fn strtoll | ||
| 154 | function has identical return values except that | ||
| 155 | .Dv LLONG_MIN | ||
| 156 | and | ||
| 157 | .Dv LLONG_MAX | ||
| 158 | are used to indicate underflow and overflow respectively. | ||
| 159 | .Sh EXAMPLES | ||
| 160 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
| 161 | trailing characters) requires clearing | ||
| 162 | .Va errno | ||
| 163 | beforehand explicitly since | ||
| 164 | .Va errno | ||
| 165 | is not changed on a successful call to | ||
| 166 | .Fn strtol , | ||
| 167 | and the return value of | ||
| 168 | .Fn strtol | ||
| 169 | cannot be used unambiguously to signal an error: | ||
| 170 | .Bd -literal -offset indent | ||
| 171 | char *ep; | ||
| 172 | long lval; | ||
| 173 | |||
| 174 | \&... | ||
| 175 | |||
| 176 | errno = 0; | ||
| 177 | lval = strtol(buf, &ep, 10); | ||
| 178 | if (buf[0] == '\e0' || *ep != '\e0') | ||
| 179 | goto not_a_number; | ||
| 180 | if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) | ||
| 181 | goto out_of_range; | ||
| 182 | .Ed | ||
| 183 | .Pp | ||
| 184 | This example will accept | ||
| 185 | .Dq 12 | ||
| 186 | but not | ||
| 187 | .Dq 12foo | ||
| 188 | or | ||
| 189 | .Dq 12\en . | ||
| 190 | If trailing whitespace is acceptable, further checks must be done on | ||
| 191 | .Va *ep ; | ||
| 192 | alternately, use | ||
| 193 | .Xr sscanf 3 . | ||
| 194 | .Pp | ||
| 195 | If | ||
| 196 | .Fn strtol | ||
| 197 | is being used instead of | ||
| 198 | .Xr atoi 3 , | ||
| 199 | error checking is further complicated because the desired return value is an | ||
| 200 | .Li int | ||
| 201 | rather than a | ||
| 202 | .Li long ; | ||
| 203 | however, on some architectures integers and long integers are the same size. | ||
| 204 | Thus the following is necessary: | ||
| 205 | .Bd -literal -offset indent | ||
| 206 | char *ep; | ||
| 207 | int ival; | ||
| 208 | long lval; | ||
| 209 | |||
| 210 | \&... | ||
| 211 | |||
| 212 | errno = 0; | ||
| 213 | lval = strtol(buf, &ep, 10); | ||
| 214 | if (buf[0] == '\e0' || *ep != '\e0') | ||
| 215 | goto not_a_number; | ||
| 216 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | ||
| 217 | (lval > INT_MAX || lval < INT_MIN)) | ||
| 218 | goto out_of_range; | ||
| 219 | ival = lval; | ||
| 220 | .Ed | ||
| 151 | .Sh ERRORS | 221 | .Sh ERRORS |
| 152 | .Bl -tag -width Er | 222 | .Bl -tag -width Er |
| 153 | .It Bq Er ERANGE | 223 | .It Bq Er ERANGE |
| @@ -157,13 +227,21 @@ The given string was out of range; the value converted has been clamped. | |||
| 157 | .Xr atof 3 , | 227 | .Xr atof 3 , |
| 158 | .Xr atoi 3 , | 228 | .Xr atoi 3 , |
| 159 | .Xr atol 3 , | 229 | .Xr atol 3 , |
| 230 | .Xr atoll 3 , | ||
| 231 | .Xr sscanf 3 , | ||
| 160 | .Xr strtod 3 , | 232 | .Xr strtod 3 , |
| 161 | .Xr strtoul 3 | 233 | .Xr strtoul 3 |
| 162 | .Sh STANDARDS | 234 | .Sh STANDARDS |
| 163 | The | 235 | The |
| 164 | .Fn strtol | 236 | .Fn strtol |
| 165 | function | 237 | and |
| 166 | conforms to | 238 | .Fn strtoll |
| 167 | .St -ansiC . | 239 | functions conform to |
| 240 | .St -ansiC-99 . | ||
| 241 | The | ||
| 242 | .Fn strtoq | ||
| 243 | function is a | ||
| 244 | .Bx | ||
| 245 | extension and is provided for backwards compatibility with legacy programs. | ||
| 168 | .Sh BUGS | 246 | .Sh BUGS |
| 169 | Ignores the current locale. | 247 | Ignores the current locale. |
