1STRTONUM(3)              BSD Library Functions Manual              STRTONUM(3)
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 <stdlib.h>
11     #include <limits.h>
12
13     long long
14     strtonum(const char *nptr, long long minval, long long maxval,
15         const char **errstr);
16

DESCRIPTION

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

RETURN VALUES

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

EXAMPLES

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

ERRORS

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

SEE ALSO

69     atof(3), atoi(3), atol(3), atoll(3), sscanf(3), strtod(3), strtol(3),
70     strtoul(3)
71

STANDARDS

73     The strtonum() function is a BSD extension.  The existing alternatives,
74     such as atoi(3) and strtol(3), are either impossible or difficult to use
75     safely.
76

HISTORY

78     The strtonum() function first appeared in OpenBSD 3.6.
79
80BSD                             April 29, 2004                             BSD
Impressum