Blame


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