Manpages - strtonum.3bsd

(See

for include usage.)

The

function converts the string in

to a

value.

The string may begin with an arbitrary amount of whitespace (as determined by

followed by a single optional

or

sign.

The remainder of the string is converted to a

value according to base 10.

The value obtained is then checked against the provided

and

bounds. If

is non-null,

stores an error string in

indicating the failure.

The

function returns the result of the conversion, unless the value would exceed the provided bounds or is invalid. On error, 0 is returned,

is set, and

will point to an error message. On success,

will be set to

this fact can be used to differentiate a successful return of 0 from an error.

Using

correctly is meant to be simpler than the alternative functions.

int iterations; const char *errstr;

iterations = strtonum(optarg, 1, 64, &errstr); if (errstr) errx(1, “number of iterations is %s: %s”, errstr, optarg);

The above example will guarantee that the value of iterations is between 1 and 64 (inclusive).

The given string did not consist solely of digit characters; or

was larger than

The given string was out of range.

If an error occurs,

will be set to one of the following strings:

The result was larger than the provided maximum value.

The result was smaller than the provided minimum value.

The string did not consist solely of digit characters.

is an

extension.

The

function first appeared in

was redesigned in

as

and

The

function was designed to facilitate safe, robust programming and overcome the shortcomings of the

and

family of interfaces, however there are problems with the

API:

will return 0 on failure; 0 might not be in range, so that necessitates an error check even if you want to avoid it

does not differentiate ’illegal’ returns, so we can’t tell the difference between partial and no conversions

returns english strings

can’t set the base, or find where the conversion ended

hardcodes long long integer type

To overcome the shortcomings of

provides

and

Author: dt

Created: 2022-02-20 Sun 20:41