Blame


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