Blame


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