From 4e511a6470f2d461d32695a5d5a7f874dd088ff8 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 3 Aug 2019 09:01:21 -0400 Subject: make rm_dir() recursive when pruning --- rmdir.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/rmdir.c b/rmdir.c index c4ee227..8ad3f02 100644 --- a/rmdir.c +++ b/rmdir.c @@ -32,22 +32,18 @@ static int rm_dir(char *path, int prune) { - if (prune) { - char *working = path; - while (strcmp(working, ".") && strcmp(working, "/")) { - if (rmdir(working) != 0) { - fprintf(stderr, "rmdir: %s: %s\n", - working, strerror(errno)); - return 1; - } - working = dirname(working); - } - - } else if (rmdir(path) != 0) { + if (rmdir(path) != 0) { fprintf(stderr, "rmdir: %s: %s\n", path, strerror(errno)); return 1; } + if (prune) { + char *parent = dirname(path); + if (strcmp(parent, ".") && strcmp(parent, "/")) { + return rm_dir(parent, prune); + } + } + return 0; } -- cgit v1.2.1