STRTOU(3)

NAME
strtou, strtou_l - convert a string value to a unitmax_t integer

LIBRARY
libc

SYNOPSIS
#include <inttypes.h>
uintmax_t
strtou(const char * restrict nptr, char ** restrict endptr, int base, uintmax_t lo, uintmax_t hi, int *rstatus);
#include <locale.h>
uintmax_t
strtou_l(const char * restrict nptr, char ** restrict endptr, int base, uintmax_t lo, uintmax_t hi, int *rstatus, locale_t loc);

DESCRIPTION
The strtou() generates the uintmax_t result value equivalent to the numeric string in nptr. The strtou() function internally uses strtoumax(3) and then ensures that the result is in the range [lo .. hi]. In addition it places a conversion status indicator, 0 if fully successful, in the integer addressed by the rstatus argument, if that is not NULL, allowing the errno gymnastics that other similar functions require to be avoided. The rstatus argument can be NULL if the conversion status is to be ignored.

The operation of strtou() is unspecified if lo is greater than hi.

The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional ‘+’ or ‘-’ sign. If base is zero or 16, the string may then include a ‘0x’ or ‘0X’ prefix, after which there must immediately follow at least one hexadecimal digit, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is ‘0’, in which case it is taken as 8 (octal).

The remainder of the string is converted to the uintmax_t result in the obvious manner, stopping at the end of the string or at the first character which is not a valid digit in the given base. (In bases above 10, the letter ‘A’ in either upper or lower case represents 10, ‘B’ represents 11, and so forth, with ‘Z’ representing 35.)

If endptr is not NULL, strtou() stores the address of the first character after those which were converted in *endptr. If there were no digits at all, however, or if the base is invalid, strtou() stores the original value of nptr in *endptr. (Thus, if *nptr is not ‘\0’ but **endptr is ‘\0’ on return, the entire string was valid.) Note that converting an out of range value has no impact upon the value placed into *endptr.

The strtou_l() function is identical, except uses the locale given by loc rather than the current locale, when determining what is white space to be skipped before the conversion begins.

RETURN VALUES
The strtou() function, returns the converted value, or the closest value in the range specified by the lo and hi arguments, if the value converted was outside that range. If lo is equal to hi and no overriding error occurs, that value will always be returned.

The errno value from <errno.h>, is guaranteed to be left unchanged.

Errors are stored as the conversion status error indicator, taken from a subset of the values from <errno.h> in the rstatus argument, if that was not given as a NULL pointer. See the ERRORS section below for the possible values.

EXAMPLES
The following example will always return a number in [1..99] range no matter what the input is, and warn if the conversion failed.

    int e;
    uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e);
    if (e)
    	warnc(e, "conversion of `%s' to a number failed, using %ju",
    	    buf, lval);

ERRORS

[ECANCELED]
    The string did not contain any characters that were converted. If given endptr will be set to nptr.

[EINVAL]
    The base is not between 2 and 36 and nor is it the special value 0. If given endptr will be set to nptr.

[ENOTSUP]
    The string contained non-numeric characters that did not get converted. In this case, endptr points to the first unconverted character.

[ERANGE]
    The given string was out of range; the value converted has been clamped. In this case, endptr points to the terminating ‘\0’ if the nptr string was fully converted, or to the first unconverted character otherwise.

The validity of the provided base is checked first, and if that fails, no further processing is attempted. The range check is more important than the unconverted characters check, and is given priority. If a program needs to know if there were unconverted characters when an out of range number has been provided, it needs to supply and test endptr.

SEE ALSO
atof(3), atoi(3), atol(3), atoll(3), strtod(3), strtoi(3), strtoumax(3), strtol(3), strtoll(3), strtoul(3), strtoull(3), warnc(3)

STANDARDS
The strtou() and strtou_l() functions are a NetBSD extension.

HISTORY
The strtou() function first appeared in NetBSD 7. OpenBSD introduced the strtonum(3) function for the same purpose, but its interface makes it impossible to properly differentiate error conditions.

BUGS
Ignores the current locale while doing the numeric conversion, only ASCII letters and digits are allowed, and no grouping characters.