Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/stdlib/strtonum.3
| .\" Copyright (c) 2004 Ted Unangst | .\" Copyright (c) 2004 Ted Unangst | ||||
| .\" Copyright 2023 Oxide Computer Company | |||||
| .\" | .\" | ||||
| .\" Permission to use, copy, modify, and distribute this software for any | .\" Permission to use, copy, modify, and distribute this software for any | ||||
| .\" purpose with or without fee is hereby granted, provided that the above | .\" purpose with or without fee is hereby granted, provided that the above | ||||
| .\" copyright notice and this permission notice appear in all copies. | .\" copyright notice and this permission notice appear in all copies. | ||||
| .\" | .\" | ||||
| .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||
| .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||
| .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||
| .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||
| .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||
| .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||
| .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||
| .\" | .\" | ||||
| .\" $OpenBSD: strtonum.3,v 1.13 2006/04/25 05:15:42 tedu Exp $ | .\" $OpenBSD: strtonum.3,v 1.13 2006/04/25 05:15:42 tedu Exp $ | ||||
| .\" | .\" | ||||
| .Dd April 29, 2004 | .Dd December 16, 2025 | ||||
emaste: You (or whoever pushes the commit) can set this to the current date when it goes in | |||||
| .Dt STRTONUM 3 | .Dt STRTONUM 3 | ||||
| .Os | .Os | ||||
| .Sh NAME | .Sh NAME | ||||
| .Nm strtonum | .Nm strtonum , | ||||
| .Nm strtonumx | |||||
| .Nd "reliably convert string value to an integer" | .Nd "reliably convert string value to an integer" | ||||
| .Sh SYNOPSIS | .Sh SYNOPSIS | ||||
| .In stdlib.h | .In stdlib.h | ||||
| .Ft long long | .Ft long long | ||||
| .Fo strtonum | .Fo strtonum | ||||
| .Fa "const char *nptr" | .Fa "const char *nptr" | ||||
| .Fa "long long minval" | .Fa "long long minval" | ||||
| .Fa "long long maxval" | .Fa "long long maxval" | ||||
| .Fa "const char **errstr" | .Fa "const char **errstr" | ||||
| .Fc | .Fc | ||||
| .Ft long long | |||||
| .Fo strtonumx | |||||
| .Fa "const char *nptr" | |||||
| .Fa "long long minval" | |||||
| .Fa "long long maxval" | |||||
| .Fa "const char **errstr" | |||||
| .Fa "int base" | |||||
| .Fc | |||||
| .Sh DESCRIPTION | .Sh DESCRIPTION | ||||
| The | The | ||||
| .Fn strtonum | .Fn strtonum | ||||
| function converts the string in | and | ||||
| .Fn strtonumx | |||||
| functions convert the string in | |||||
| .Fa nptr | .Fa nptr | ||||
| to a | to a | ||||
| .Vt "long long" | .Vt "long long" | ||||
| value. | value. | ||||
| The | These functions were designed to facilitate safe, robust programming and | ||||
| .Fn strtonum | overcome the shortcomings of the | ||||
| function was designed to facilitate safe, robust programming | |||||
| and overcome the shortcomings of the | |||||
| .Xr atoi 3 | .Xr atoi 3 | ||||
| and | and | ||||
| .Xr strtol 3 | .Xr strtol 3 | ||||
| family of interfaces. | family of interfaces. | ||||
| .Pp | .Pp | ||||
| The string may begin with an arbitrary amount of whitespace | The string may begin with an arbitrary amount of whitespace | ||||
| (as determined by | .Pq as determined by Xr isspace 3 | ||||
| .Xr isspace 3 ) | |||||
| followed by a single optional | followed by a single optional | ||||
| .Ql + | .Ql + | ||||
| or | or | ||||
| .Ql - | .Ql - | ||||
| sign. | sign. | ||||
| .Pp | .Pp | ||||
| The remainder of the string is converted to a | The remainder of the string is converted to a | ||||
| .Vt "long long" | .Vt "long long" | ||||
| value according to base 10. | value according to base 10 | ||||
| .Pq for Fn strtonum | |||||
| or the provided base | |||||
| .Pq for Fn strtonumx . | |||||
| .Pp | .Pp | ||||
| The value obtained is then checked against the provided | The value obtained is then checked against the provided | ||||
| .Fa minval | .Fa minval | ||||
| and | and | ||||
| .Fa maxval | .Fa maxval | ||||
| bounds. | bounds. | ||||
| If | If | ||||
| .Fa errstr | .Fa errstr | ||||
| is non-null, | is non-null, | ||||
| .Fn strtonum | .Fn strtonum | ||||
| stores an error string in | and | ||||
| .Fn strtonumx | |||||
| store an error string in | |||||
| .Fa *errstr | .Fa *errstr | ||||
| indicating the failure. | indicating the failure. | ||||
| .Pp | |||||
| For | |||||
| .Fn strtonumx | |||||
| the value of | |||||
| .Ar base | |||||
| is interpreted in the same way as described in | |||||
| .Xr strtoll 3 . | |||||
| In particular, if the value of | |||||
| .Ar base | |||||
| is 0, then the expected form of | |||||
| .Ar nptr | |||||
| is that of a decimal constant, octal constant or hexadecimal constant, any of | |||||
| which may be preceded by a + or - sign. | |||||
| .Sh RETURN VALUES | .Sh RETURN VALUES | ||||
| The | The | ||||
| .Fn strtonum | .Fn strtonum | ||||
| function returns the result of the conversion, | and | ||||
| .Fn strtonumx | |||||
| functions return the result of the conversion, | |||||
| unless the value would exceed the provided bounds or is invalid. | unless the value would exceed the provided bounds or is invalid. | ||||
| On error, 0 is returned, | On error, 0 is returned, | ||||
| .Va errno | .Va errno | ||||
| is set, and | is set, and | ||||
| .Fa errstr | .Fa errstr | ||||
| will point to an error message. | will point to an error message. | ||||
| On success, | On success, | ||||
| .Fa *errstr | .Fa *errstr | ||||
| will be set to | will be set to | ||||
| .Dv NULL ; | .Dv NULL ; | ||||
| this fact can be used to differentiate | this fact can be used to differentiate | ||||
| a successful return of 0 from an error. | a successful return of 0 from an error. | ||||
| .Sh EXAMPLES | .Sh EXAMPLES | ||||
| Using | Using | ||||
| .Fn strtonum | .Fn strtonum | ||||
| and | |||||
| .Fn strtonumx | |||||
| correctly is meant to be simpler than the alternative functions. | correctly is meant to be simpler than the alternative functions. | ||||
| .Bd -literal -offset indent | .Bd -literal -offset indent | ||||
| int iterations; | int iterations; | ||||
| const char *errstr; | const char *errstr; | ||||
| iterations = strtonum(optarg, 1, 64, &errstr); | iterations = strtonum(optarg, 1, 64, &errstr); | ||||
| if (errstr != NULL) | if (errstr != NULL) | ||||
| errx(1, "number of iterations is %s: %s", errstr, optarg); | errx(1, "number of iterations is %s: %s", errstr, optarg); | ||||
| .Ed | .Ed | ||||
| .Pp | .Pp | ||||
| The above example will guarantee that the value of iterations is between | The above example will guarantee that the value of iterations is between | ||||
| 1 and 64 (inclusive). | 1 and 64 (inclusive). | ||||
| .Sh ERRORS | .Sh ERRORS | ||||
| .Bl -tag -width Er | .Bl -tag -width Er | ||||
| .It Bq Er ERANGE | .It Bq Er ERANGE | ||||
| The given string was out of range. | The given string was out of range. | ||||
| .It Bq Er EINVAL | .It Bq Er EINVAL | ||||
| The given string did not consist solely of digit characters. | The given string did not consist solely of digit characters | ||||
| .Pq for Fn strtonum , | |||||
| or characters which are valid in the given base | |||||
| .Pq for Fn strtonumx . | |||||
| .It Bq Er EINVAL | .It Bq Er EINVAL | ||||
| The supplied | The supplied | ||||
| .Fa minval | .Fa minval | ||||
| was larger than | was larger than | ||||
| .Fa maxval . | .Fa maxval . | ||||
| .El | .El | ||||
| .Pp | .Pp | ||||
| If an error occurs, | If an error occurs, | ||||
| .Fa errstr | .Fa errstr | ||||
| will be set to one of the following strings: | will be set to one of the following strings: | ||||
| .Pp | .Pp | ||||
| .Bl -tag -width ".Li too large" -compact | .Bl -tag -width ".Li too large" -compact | ||||
| .It Li "too large" | .It Qq too large | ||||
| The result was larger than the provided maximum value. | The result was larger than the provided maximum value. | ||||
| .It Li "too small" | .It Qq too small | ||||
| The result was smaller than the provided minimum value. | The result was smaller than the provided minimum value. | ||||
| .It Li invalid | .It Qq invalid | ||||
| The string did not consist solely of digit characters. | The string did not consist solely of characters valid in the specified base | ||||
| .Pq or base 10 for Fn strtonum . | |||||
| .It Qq unparsable; invalid base specified | |||||
| The specified base was outside the permitted range. | |||||
| .El | .El | ||||
| .Sh SEE ALSO | .Sh SEE ALSO | ||||
| .Xr atof 3 , | .Xr atof 3 , | ||||
| .Xr atoi 3 , | .Xr atoi 3 , | ||||
| .Xr atol 3 , | .Xr atol 3 , | ||||
| .Xr atoll 3 , | .Xr atoll 3 , | ||||
| .Xr sscanf 3 , | .Xr sscanf 3 , | ||||
| .Xr strtod 3 , | .Xr strtod 3 , | ||||
| Show All 10 Lines | |||||
| and | and | ||||
| .Xr strtol 3 , | .Xr strtol 3 , | ||||
| are either impossible or difficult to use safely. | are either impossible or difficult to use safely. | ||||
| .Sh HISTORY | .Sh HISTORY | ||||
| The | The | ||||
| .Fn strtonum | .Fn strtonum | ||||
| function first appeared in | function first appeared in | ||||
| .Ox 3.6 . | .Ox 3.6 . | ||||
| The | |||||
| .Fn strtonumx | |||||
| function first appeared in illumos in 2023. | |||||
Not Done Inline ActionsWe prefer to use version numbers here. ziaee: We prefer to use version numbers here. | |||||
Done Inline Actionsillumos doesn't have versions or version numbers. If it helps I can try to find the exact date the commit went into illumos-gate. rosenfeld_grumpf.hope-2000.org: illumos doesn't have versions or version numbers. If it helps I can try to find the exact date… | |||||
You (or whoever pushes the commit) can set this to the current date when it goes in