From 78fd8698cc9f23fc38b3150e61ea902057ed8c06 Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Tue, 24 May 2011 13:04:56 +0100 Subject: [PATCH] fix rm -r --- rm.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rm.c b/rm.c index 4aa00a5..fe6e2ce 100644 --- a/rm.c +++ b/rm.c @@ -39,9 +39,17 @@ void rm(const char *path) if(remove(path) == 0) return; if(errno == ENOTEMPTY && rflag) { + char *buf; + long size; struct dirent *d; DIR *dp; + if((size = pathconf(".", _PC_PATH_MAX)) < 0) + size = BUFSIZ; + if(!(buf = malloc(size))) + eprintf("malloc:"); + if(!getcwd(buf, size)) + eprintf("getcwd:"); if(!(dp = opendir(path))) eprintf("opendir %s:", path); if(chdir(path) != 0) @@ -51,8 +59,8 @@ void rm(const char *path) rm(d->d_name); closedir(dp); - if(chdir("..") != 0) - eprintf("chdir:"); + if(chdir(buf) != 0) + eprintf("chdir %s:", buf); if(remove(path) == 0) return; }