fix rm -r

This commit is contained in:
Connor Lane Smith 2011-05-24 13:04:56 +01:00
parent fbb80983ce
commit 78fd8698cc
1 changed files with 10 additions and 2 deletions

12
rm.c
View File

@ -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;
}