Blame


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