Blame


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