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