1STRTOU(3bsd)                         LOCAL                        STRTOU(3bsd)
2

NAME

4     strtou — convert a string to an uintmax_t integer
5

LIBRARY

7     Utility functions from BSD systems (libbsd, -lbsd)
8

SYNOPSIS

10     #include <inttypes.h>
11     (See libbsd(7) for include usage.)
12
13     uintmax_t
14     strtou(const char * restrict nptr, char ** restrict endptr, int base,
15         uintmax_t lo, uintmax_t hi, int *rstatus);
16

DESCRIPTION

18     The strtou() function converts the string in nptr to an uintmax_t value.
19     The strtou() function uses internally strtoumax(3) and ensures that the
20     result is always in the range [ lo .. hi ].  In adddition it always
21     places 0 on success or a conversion status in the rstatus argument,
22     avoiding the errno gymnastics the other functions require.  The rstatus
23     argument can be NULL if conversion status is to be ignored.
24
25     The string may begin with an arbitrary amount of white space (as deter‐
26     mined by isspace(3)) followed by a single optional ‘+’ or ‘-’ sign.  If
27     base is zero or 16, the string may then include a ‘0x’ or ‘0X’ prefix,
28     and the number will be read in base 16; otherwise, a zero base is taken
29     as 10 (decimal) unless the next character is ‘0’, in which case it is
30     taken as 8 (octal).
31
32     The remainder of the string is converted to an uintmax_t value in the
33     obvious manner, stopping at the end of the string or at the first charac‐
34     ter that does not produce a valid digit in the given base.  (In bases
35     above 10, the letter ‘A’ in either upper or lower case represents 10, ‘B’
36     represents 11, and so forth, with ‘Z’ representing 35.)
37
38     If endptr is non-nil, strtou() stores the address of the first invalid
39     character in *endptr.  If there were no digits at all, however, strtou()
40     stores the original value of nptr in *endptr.  (Thus, if *nptr is not
41     ‘\0’ but **endptr is ‘\0’ on return, the entire string was valid.)
42

RETURN VALUES

44     The strtou() function always returns the closest value in the range spec‐
45     ified by the lo and hi arguments.
46
47     The errno value is guaranteed to be left unchanged.
48
49     Errors are stored as the conversion status in the rstatus argument.
50

EXAMPLES

52     The following example will always return a number in [1..99] range no
53     matter what the input is, and warn if the conversion failed.
54
55           int e;
56           uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e);
57           if (e)
58                   warnc(e, "conversion of `%s' to a number failed, using %ju",
59                       buf, lval);
60

ERRORS

62     [ECANCELED]        The string did not contain any characters that were
63                        converted.
64
65     [EINVAL]           The base is not between 2 and 36 and does not contain
66                        the special value 0.
67
68     [ENOTSUP]          The string contained non-numeric characters that did
69                        not get converted.  In this case, endptr points to the
70                        first unconverted character.
71
72     [ERANGE]           The given string was out of range; the value converted
73                        has been clamped; or the range given was invalid, i.e.
74                        lo > hi.
75

SEE ALSO

77     atof(3), atoi(3), atol(3), atoll(3), strtod(3), strtoi(3bsd),
78     strtoimax(3), strtol(3), strtoll(3), strtoul(3), strtoull(3),
79     strtoumax(3)
80

STANDARDS

82     The strtou() function is a NetBSD extension.
83

HISTORY

85     The strtou() function first appeared in NetBSD 7.0.  OpenBSD introduced
86     the strtonum(3bsd) function for the same purpose, but the interface makes
87     it impossible to properly differentiate illegal returns.
88

BUGS

90     Ignores the current locale.
91
92BSD                            November 13, 2015                           BSD
Impressum