walk

A re-creation of the 9front utility walk(1) for UNIX
Log | Files | Refs

commit 615771b7133324240e4aba669c273dbb9ef541cd
parent ee4f23354ec62a595e260ce92dd0fda4f9d839ac
Author: Jacob R. Edwards <n/a>
Date:   Mon,  5 Dec 2022 12:26:39 -0600

Fix -n flag

One issue was that if there was an error parsing the value, only a
warning would be issued and program execution would continue.

Another was that empty fields in the value were not handled (mind
and maxd) and caused an error to be reported.

Diffstat:
Mwalk.c | 35++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/walk.c b/walk.c @@ -58,16 +58,28 @@ getnum(char *s, char **ep) int parse_n(char *arg, int *min, int *max) { - char *ep; + int n; - *min = getnum(arg, &ep); - if (*min < 0) - return 1; + if (*arg == ',') { + *min = 0; + } else { + *min = getnum(arg, &arg); + if (*min < 0) + return 1; + } - if (*ep == ',') { - *max = getnum(ep + 1, &ep); + if (*arg == ',') { + if (!arg[1]) { + *max = -1; + return 0; + } + *max = getnum(arg + 1, &arg); + if (*arg) { + errno = EINVAL; + return 1; + } return *max < 0; - } else if (!*ep) { + } else if (!*arg) { *max = *min; *min = 0; return 0; @@ -180,6 +192,7 @@ main(int argc, char *argv[]) int show; char *path; int offset; + int depth; enum { DIR = 1, NOTDIR } type; @@ -210,7 +223,7 @@ main(int argc, char *argv[]) break; case 'n': if (parse_n(optarg, &min, &max)) - perror(optarg); + die(optarg); break; case 'e': if (optarg[strcspn(optarg, "MqDT")]) { @@ -263,7 +276,11 @@ main(int argc, char *argv[]) if (strcmp(ent->fts_name, ".") == 0 || strcmp(ent->fts_name, "..") == 0) continue; - if (min >= ent->fts_level && (max < 0 || ent->fts_level >= max)) + + depth = ent->fts_level - 1; + if (max >= 0 && depth > max) + continue; + if (depth < min) continue; if (exec)