commit a5ae8166d9b9e054ad18ae5be117290b22233469
parent c9a452e99bc16bf8b4086425c6534f7a17f6484e
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 20 Dec 2020 14:06:34 -0800
Use strtoul rather than a costom function
Diffstat:
M | main.c | | | 31 | ++++++++++--------------------- |
1 file changed, 10 insertions(+), 21 deletions(-)
diff --git a/main.c b/main.c
@@ -149,21 +149,6 @@ too_big:
return 0;
}
-int
-strtoui(const char *s)
-{
- int i;
-
- i = 0;
- while (isdigit(*s)) {
- i = i * 10 + (*s - '0');
- ++s;
- }
- if (*s != '\0')
- return -1;
- return i;
-}
-
/* `path` old path, `newpath` new path. Result put into `path`. */
void
newpath(char *path, const char *newpath)
@@ -368,18 +353,22 @@ cgoto(int argc, const char **argv, int depth, const char *tmpdir, const char *ho
int
cback(int argc, const char **argv)
{
- int back;
+ char *ep;
+ unsigned long back;
if (argc == 0)
- return 1;
+ return 1; /* back `1`, not an error */
- back = strtoui(argv[0]);
- if (back < 0) {
- warn("invalid number '%s'", argv[0]);
+ back = strtoul(argv[0], &ep, 0);
+ if (*ep != '\0' || (back == 0 && errno == EINVAL)) {
+ warnc(EINVAL, "'%s'", argv[0]);
+ return 0;
+ } else if (back > INT_MAX) {
+ warnc(ERANGE, "'%s'", argv[0]);
return 0;
}
- return back;
+ return back;
}
int