diff options
-rw-r--r-- | rm.c | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -45,7 +45,6 @@ static enum { DEFAULT, INTERACTIVE, FORCE } mode = DEFAULT; static int recursive = 0; -static int retval = 0; static int confirm(const char *p) { @@ -83,20 +82,26 @@ static int rm(const char *p, const struct stat *st, int typeflag, struct FTW *f) if (st == NULL) { fprintf(stderr, "rm: %s: unknown error\n", p); - retval = 1; + return 1; + } - } else if (S_ISDIR(st->st_mode)) { + if (S_ISDIR(st->st_mode)) { if (!recursive) { fprintf(stderr, "rm: %s: %s\n", p, strerror(EISDIR)); - retval = 1; - } else if (confirm(p) && rmdir(p) != 0) { + return 1; + } + + if (confirm(p) && rmdir(p) != 0) { fprintf(stderr, "rm: %s: %s\n", p, strerror(errno)); - retval = 1; + return 1; } - } else if (confirm(p) && unlink(p) != 0) { + return 0; + } + + if (confirm(p) && unlink(p) != 0) { fprintf(stderr, "rm: %s: %s\n", p, strerror(errno)); - retval = 1; + return 1; } return 0; @@ -135,6 +140,7 @@ int main(int argc, char *argv[]) return 1; } + int ret = 0; do { char *path = argv[optind]; char base[strlen(path) + 1]; @@ -144,23 +150,26 @@ int main(int argc, char *argv[]) if (!strcmp(b, "/") || !strcmp(b, ".") || !strcmp(b, "..")) { fprintf(stderr, "rm: %s: %s\n", path, strerror(EINVAL)); - retval = 1; + ret = 1; + continue; + } - } else if (lstat(path, &st) != 0) { + if (lstat(path, &st) != 0) { if (mode == FORCE) { continue; } fprintf(stderr, "rm: %s: %s\n", path, strerror(errno)); - retval = 1; - - } else if (recursive) { - nftw(path, rm, OPEN_MAX, FTW_DEPTH | FTW_PHYS); + ret = 1; + continue; + } - } else { - rm(path, &st, 0, NULL); + if (recursive) { + ret |= nftw(path, rm, OPEN_MAX, FTW_DEPTH | FTW_PHYS); + continue; } + ret |= rm(path, &st, 0, NULL); } while (++optind < argc); - return retval; + return ret; } |