Blame


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