From 1c0d96520032b0ee22442cb3b6ad74c798b0af78 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 16 Sep 2019 16:24:34 -0700 Subject: rm.c: use FLAG(). --- toys/posix/rm.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/toys/posix/rm.c b/toys/posix/rm.c index 5d5cb604..8874b54f 100644 --- a/toys/posix/rm.c +++ b/toys/posix/rm.c @@ -25,20 +25,19 @@ config RM static int do_rm(struct dirtree *try) { - int fd = dirtree_parentfd(try), flags = toys.optflags; - int dir = S_ISDIR(try->st.st_mode), or = 0, using = 0; + int fd=dirtree_parentfd(try), dir=S_ISDIR(try->st.st_mode), or=0, using=0; // Skip . and .. (yes, even explicitly on the command line: posix says to) if (isdotdot(try->name)) return 0; // Intentionally fail non-recursive attempts to remove even an empty dir // (via wrong flags to unlinkat) because POSIX says to. - if (dir && !(flags & (FLAG_r|FLAG_R))) goto skip; + if (dir && !(toys.optflags & (FLAG_r|FLAG_R))) goto skip; // This is either the posix section 2(b) prompt or the section 3 prompt. - if (!(flags & FLAG_f) + if (!FLAG(f) && (!S_ISLNK(try->st.st_mode) && faccessat(fd, try->name, W_OK, 0))) or++; - if (!(dir && try->again) && ((or && isatty(0)) || (flags & FLAG_i))) { + if (!(dir && try->again) && ((or && isatty(0)) || FLAG(i))) { char *s = dirtree_path(try, 0); fprintf(stderr, "rm %s%s%s", or ? "ro " : "", dir ? "dir " : "", s); @@ -52,12 +51,12 @@ static int do_rm(struct dirtree *try) using = AT_REMOVEDIR; // Handle chmod 000 directories when -f if (faccessat(fd, try->name, R_OK, 0)) { - if (toys.optflags & FLAG_f) wfchmodat(fd, try->name, 0700); + if (FLAG(f)) wfchmodat(fd, try->name, 0700); else goto skip; } if (!try->again) return DIRTREE_COMEAGAIN; if (try->symlink) goto skip; - if (flags & FLAG_i) { + if (FLAG(i)) { char *s = dirtree_path(try, 0); // This is the section 2(d) prompt. (Yes, posix says to prompt twice.) @@ -70,7 +69,7 @@ static int do_rm(struct dirtree *try) skip: if (!unlinkat(fd, try->name, using)) { - if (flags & FLAG_v) { + if (FLAG(v)) { char *s = dirtree_path(try, 0); printf("%s%s '%s'\n", toys.which->name, dir ? "dir" : "", s); free(s); @@ -89,7 +88,7 @@ void rm_main(void) char **s; // Can't use <1 in optstring because zero arguments with -f isn't an error - if (!toys.optc && !(toys.optflags & FLAG_f)) help_exit("Needs 1 argument"); + if (!toys.optc && !FLAG(f)) help_exit("Needs 1 argument"); for (s = toys.optargs; *s; s++) { if (!strcmp(*s, "/")) { @@ -99,8 +98,7 @@ void rm_main(void) // Files that already don't exist aren't errors for -f, so try a quick // unlink now to see if it succeeds or reports that it didn't exist. - if ((toys.optflags & FLAG_f) && (!unlink(*s) || errno == ENOENT)) - continue; + if (FLAG(f) && (!unlink(*s) || errno == ENOENT)) continue; // There's a race here where a file removed between the above check and // dirtree's stat would report the nonexistence as an error, but that's -- cgit v1.2.3