gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit 7e4632aeee13de4ef16f0f2c026d2fee46516815
parent 5b1f240d9331ea54ff8e99259c6788b67a47ce62
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 23 Dec 2020 13:05:26 -0800

Improve strtorange()

Remove tests against UINT_MAX and ULONG_MAX made redundent by `min`
and `max`.

Vastly simplify error handling.

Diffstat:
Mmain.c | 22+++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/main.c b/main.c @@ -133,20 +133,20 @@ exists(const char *path) int strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s) { - unsigned long n; char *ep; + unsigned long n; - n = strtoul(s, &ep, 0); - if (*s == '\0' || *ep != '\0') { - warnc(EINVAL, "not a number '%s'", s); - return -1; - } else if ((errno == ERANGE && n == ULONG_MAX) || - n < min || n > max || n > UINT_MAX) { - warnc(ERANGE, "'%s'", s); - return -1; + n = strtoul(s, &ep, 10); + if (*s == '\0' || *ep != '\0') + errno = EINVAL; + else if (n < min || n > max) + errno = ERANGE; + else { + *r = n; + return 0; } - *r = n; - return 0; + warn("'%s'", s); + return 1; } void