.\" .\" Copyright (c) 2004 Ted Unangst .\" Copyright 2023 Oxide Computer Company .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above .\" copyright notice and this permission notice appear in all copies. .\" .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .Dd August 19, 2023 .Dt STRTONUM 3C .Os .Sh NAME .Nm strtonum , .Nm strtonumx .Nd reliably convert string value to an integer .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In stdlib.h .Ft long long .Fo strtonum .Fa "const char *nptr" .Fa "long long minval" .Fa "long long maxval" .Fa "const char **errstr" .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 The .Fn strtonum and .Fn strtonumx functions convert the string in .Fa nptr to a .Li long long value. These functions were designed to facilitate safe, robust programming and overcome the shortcomings of the .Xr atoi 3C and .Xr strtol 3C family of interfaces. .Pp The string may begin with an arbitrary amount of whitespace .Pq as determined by Xr isspace 3C followed by a single optional .Ql + or .Ql - sign. .Pp The remainder of the string is converted to a .Li long long value according to base 10 .Pq for Fn strtonum or the provided base .Pq for Fn strtonumx . .Pp The value obtained is then checked against the provided .Fa minval and .Fa maxval bounds. If .Fa errstr is non-null, .Fn strtonum and .Fn strtonumx store an error string in .Fa errstr indicating the failure. .Pp For .Fn strtonumx the value of .Ar base is interpreted in the same way as described in .Xr strtoll 3C . 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 The .Fn strtonum function returns the result of the conversion, unless the value would exceed the provided bounds or is invalid. On error, 0 is returned, .Va errno is set, and .Fa errstr will point to an error message. .Fa errstr will be set to .Dv NULL on success; this fact can be used to differentiate a successful return of 0 from an error. .Sh EXAMPLES Using .Fn strtonum correctly is meant to be simpler than the alternative functions. .Bd -literal -offset indent int iterations; const char *errstr; iterations = strtonum(optarg, 1, 64, &errstr); if (errstr != NULL) errx(1, "number of iterations is %s: %s", errstr, optarg); .Ed .Pp The above example will guarantee that the value of iterations is between 1 and 64 .Pq inclusive . .Sh ERRORS The .Fn strtonum and .Fn strtonumx functions will fail if: .Bl -tag -width Er .It Er ERANGE The value to be returned falls outside of the specified range. .It Er EINVAL .Ar minval was larger than .Ar maxval . .El .Pp The .Fn strtonum function will fail if: .Bl -tag -width Er .It Er EINVAL The given string did not consist solely of digit characters. .El .Pp The .Fn strtonumx function will fail if: .Bl -tag -width Er .It Er EINVAL The specified base was invalid, or the given string did not consist solely of characters which are valid in that base. .El .Pp If an error occurs, .Fa errstr will be set to one of the following strings: .Pp .Bl -tag -width "too largeXX" -compact .It Qq too large The result was larger than the provided maximum value. .It Qq too small The result was smaller than the provided minimum value. .It Qq invalid 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 .Sh INTERFACE STABILITY .Sy Committed . .Sh MT-LEVEL .Sy Safe . .Sh SEE ALSO .Xr atof 3C , .Xr atoi 3C , .Xr atol 3C , .Xr atoll 3C , .Xr sscanf 3C , .Xr strtod 3C , .Xr strtol 3C , .Xr strtoll 3C , .Xr strtoul 3C .Sh STANDARDS .Fn strtonum is an .Ox extension. The existing alternatives, such as .Xr atoi 3C and .Xr strtol 3C , are either impossible or difficult to use safely. .Pp .Fn strtonumx is an illumos extension.