Blame


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