#include #include char errbuf[ERRLEN]; Dir *dirbuf; long ndirbuf = 0; int ignerr = 0; void err(char *f) { if(!ignerr){ errstr(errbuf); fprint(2, "rm: %s: %s\n", f, errbuf); } } /* * Read a whole directory before removing anything as the holes formed * by removing affect the read offset. */ long readdirect(int fd) { enum { N = 32 }; long m, n; m = 1; /* prime the loop */ for(n=0; m>0; n+=m/sizeof(Dir)){ if(n == ndirbuf){ dirbuf = realloc(dirbuf, (ndirbuf+N)*sizeof(Dir)); if(dirbuf == 0){ err("memory allocation"); exits(errbuf); } ndirbuf += N; } m = dirread(fd, dirbuf+n, (ndirbuf-n)*sizeof(Dir)); } return n; } /* * f is a non-empty directory. Remove its contents and then it. */ void rmdir(char *f) { char *name; Dir *db; int fd, i, j, n, ndir; fd = open(f, OREAD); if(fd < 0){ err(f); return; } n = readdirect(fd); close(fd); name = malloc(strlen(f)+1+NAMELEN); if(name == 0){ err("memory allocation"); return; } ndir = 0; for(i=0; i