1STRTONUM(3bsd)                       LOCAL                      STRTONUM(3bsd)
2

NAME

4     strtonum — reliably convert string value to an integer
5

LIBRARY

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

SYNOPSIS

10     #include <limits.h>
11     #include <stdlib.h>
12     (See libbsd(7) for include usage.)
13
14     long long
15     strtonum(const char *nptr, long long minval, long long maxval,
16         const char **errstr);
17

DESCRIPTION

19     The strtonum() function converts the string in nptr to a long long value.
20
21     The string may begin with an arbitrary amount of whitespace (as deter‐
22     mined by isspace(3)) followed by a single optional ‘+’ or ‘-’ sign.
23
24     The remainder of the string is converted to a long long value according
25     to base 10.
26
27     The value obtained is then checked against the provided minval and maxval
28     bounds.  If errstr is non-null, strtonum() stores an error string in
29     *errstr indicating the failure.
30

RETURN VALUES

32     The strtonum() function returns the result of the conversion, unless the
33     value would exceed the provided bounds or is invalid.  On error, 0 is
34     returned, errno is set, and errstr will point to an error message.  On
35     success, *errstr will be set to NULL; this fact can be used to differen‐
36     tiate a successful return of 0 from an error.
37

EXAMPLES

39     Using strtonum() correctly is meant to be simpler than the alternative
40     functions.
41
42           int iterations;
43           const char *errstr;
44
45           iterations = strtonum(optarg, 1, 64, &errstr);
46           if (errstr)
47                   errx(1, "number of iterations is %s: %s", errstr, optarg);
48
49     The above example will guarantee that the value of iterations is between
50     1 and 64 (inclusive).
51

ERRORS

53     [EINVAL]           The given string did not consist solely of digit char‐
54                        acters; or minval was larger than maxval.
55
56     [ERANGE]           The given string was out of range.
57
58     If an error occurs, errstr will be set to one of the following strings:
59
60     too large  The result was larger than the provided maximum value.
61     too small  The result was smaller than the provided minimum value.
62     invalid    The string did not consist solely of digit characters.
63

SEE ALSO

65     atof(3), atoi(3), atol(3), atoll(3), sscanf(3), strtod(3), strtoi(3bsd),
66     strtol(3), strtoll(3), strtou(3bsd), strtoul(3), strtoull(3)
67

STANDARDS

69     strtonum() is an OpenBSD extension.
70

HISTORY

72     The strtonum() function first appeared in OpenBSD 3.6.  strtonum() was
73     redesigned in NetBSD 8.0 as strtoi(3bsd) and strtou(3bsd).
74

CAVEATS

76     The strtonum() function was designed to facilitate safe, robust program‐
77     ming and overcome the shortcomings of the atoi(3) and strtol(3) family of
78     interfaces, however there are problems with the strtonum() API:
79
80     -   will return 0 on failure; 0 might not be in range, so that necessi‐
81         tates an error check even if you want to avoid it
82
83     -   does not differentiate 'illegal' returns, so we can't tell the dif‐
84         ference between partial and no conversions
85
86     -   returns english strings
87
88     -   can't set the base, or find where the conversion ended
89
90     -   hardcodes long long integer type
91     To overcome the shortcomings of strtonum() NetBSD provides strtou(3bsd)
92     and strtoi(3bsd).
93
94BSD                            January 18, 2015                            BSD
Impressum