Blame


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