Blame


1 8951cb19 2022-07-01 op /*
2 8951cb19 2022-07-01 op * Copyright (c) 2004 Ted Unangst and Todd Miller
3 8951cb19 2022-07-01 op * All rights reserved.
4 8951cb19 2022-07-01 op *
5 8951cb19 2022-07-01 op * Permission to use, copy, modify, and distribute this software for any
6 8951cb19 2022-07-01 op * purpose with or without fee is hereby granted, provided that the above
7 8951cb19 2022-07-01 op * copyright notice and this permission notice appear in all copies.
8 8951cb19 2022-07-01 op *
9 8951cb19 2022-07-01 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8951cb19 2022-07-01 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8951cb19 2022-07-01 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8951cb19 2022-07-01 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8951cb19 2022-07-01 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8951cb19 2022-07-01 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8951cb19 2022-07-01 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 8951cb19 2022-07-01 op */
17 8951cb19 2022-07-01 op
18 8951cb19 2022-07-01 op #include <errno.h>
19 8951cb19 2022-07-01 op #include <limits.h>
20 8951cb19 2022-07-01 op #include <stdlib.h>
21 8951cb19 2022-07-01 op
22 8951cb19 2022-07-01 op #define INVALID 1
23 8951cb19 2022-07-01 op #define TOOSMALL 2
24 8951cb19 2022-07-01 op #define TOOLARGE 3
25 8951cb19 2022-07-01 op
26 8951cb19 2022-07-01 op long long
27 8951cb19 2022-07-01 op strtonum(const char *numstr, long long minval, long long maxval,
28 8951cb19 2022-07-01 op const char **errstrp)
29 8951cb19 2022-07-01 op {
30 8951cb19 2022-07-01 op long long ll = 0;
31 8951cb19 2022-07-01 op int error = 0;
32 8951cb19 2022-07-01 op char *ep;
33 8951cb19 2022-07-01 op struct errval {
34 8951cb19 2022-07-01 op const char *errstr;
35 8951cb19 2022-07-01 op int err;
36 8951cb19 2022-07-01 op } ev[4] = {
37 8951cb19 2022-07-01 op { NULL, 0 },
38 8951cb19 2022-07-01 op { "invalid", EINVAL },
39 8951cb19 2022-07-01 op { "too small", ERANGE },
40 8951cb19 2022-07-01 op { "too large", ERANGE },
41 8951cb19 2022-07-01 op };
42 8951cb19 2022-07-01 op
43 8951cb19 2022-07-01 op ev[0].err = errno;
44 8951cb19 2022-07-01 op errno = 0;
45 8951cb19 2022-07-01 op if (minval > maxval) {
46 8951cb19 2022-07-01 op error = INVALID;
47 8951cb19 2022-07-01 op } else {
48 8951cb19 2022-07-01 op ll = strtoll(numstr, &ep, 10);
49 8951cb19 2022-07-01 op if (numstr == ep || *ep != '\0')
50 8951cb19 2022-07-01 op error = INVALID;
51 8951cb19 2022-07-01 op else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
52 8951cb19 2022-07-01 op error = TOOSMALL;
53 8951cb19 2022-07-01 op else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
54 8951cb19 2022-07-01 op error = TOOLARGE;
55 8951cb19 2022-07-01 op }
56 8951cb19 2022-07-01 op if (errstrp != NULL)
57 8951cb19 2022-07-01 op *errstrp = ev[error].errstr;
58 8951cb19 2022-07-01 op errno = ev[error].err;
59 8951cb19 2022-07-01 op if (error)
60 8951cb19 2022-07-01 op ll = 0;
61 8951cb19 2022-07-01 op
62 8951cb19 2022-07-01 op return (ll);
63 8951cb19 2022-07-01 op }