2016-11-25 17:18:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the UCB release of Plan 9. It is subject to the license
|
|
|
|
* terms in the LICENSE file found in the top-level directory of this
|
|
|
|
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
|
|
|
|
* part of the UCB release of Plan 9, including this file, may be copied,
|
|
|
|
* modified, propagated, or distributed except according to the terms contained
|
|
|
|
* in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
|
|
|
|
char errbuf[ERRMAX];
|
|
|
|
int ignerr = 0;
|
2016-12-26 01:16:31 +01:00
|
|
|
int printerr = 0;
|
2016-11-25 17:18:40 +01:00
|
|
|
|
|
|
|
void
|
2016-12-26 01:16:31 +01:00
|
|
|
err(long e, char *f)
|
2016-11-25 17:18:40 +01:00
|
|
|
{
|
2016-12-26 01:16:31 +01:00
|
|
|
if(printerr){
|
|
|
|
print("%s %ulld\n", f, e);
|
|
|
|
} else if(!ignerr){
|
2016-11-25 17:18:40 +01:00
|
|
|
errbuf[0] = '\0';
|
|
|
|
errstr(errbuf, sizeof errbuf);
|
|
|
|
fprint(2, "rm: %s: %s\n", f, errbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* f is a non-empty directory. Remove its contents and then it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rmdir(char *f)
|
|
|
|
{
|
|
|
|
char *name;
|
2016-12-26 01:16:31 +01:00
|
|
|
long e;
|
2016-11-25 17:18:40 +01:00
|
|
|
int fd, i, j, n, ndir, nname;
|
|
|
|
Dir *dirbuf;
|
|
|
|
|
|
|
|
fd = open(f, OREAD);
|
|
|
|
if(fd < 0){
|
2016-12-26 01:16:31 +01:00
|
|
|
err(-1, f);
|
2016-11-25 17:18:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = dirreadall(fd, &dirbuf);
|
|
|
|
close(fd);
|
|
|
|
if(n < 0){
|
2016-12-26 01:16:31 +01:00
|
|
|
err(-1, "dirreadall");
|
2016-11-25 17:18:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nname = strlen(f)+1+STATMAX+1; /* plenty! */
|
|
|
|
name = malloc(nname);
|
|
|
|
if(name == 0){
|
2016-12-26 01:16:31 +01:00
|
|
|
err(-1, "memory allocation");
|
2016-11-25 17:18:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndir = 0;
|
|
|
|
for(i=0; i<n; i++){
|
|
|
|
snprint(name, nname, "%s/%s", f, dirbuf[i].name);
|
2016-12-26 01:16:31 +01:00
|
|
|
if((e = remove(name)) != -1)
|
2016-11-25 17:18:40 +01:00
|
|
|
dirbuf[i].qid.type = QTFILE; /* so we won't recurse */
|
|
|
|
else{
|
|
|
|
if(dirbuf[i].qid.type & QTDIR)
|
|
|
|
ndir++;
|
|
|
|
else
|
2016-12-26 01:16:31 +01:00
|
|
|
err(e, name);
|
2016-11-25 17:18:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ndir)
|
|
|
|
for(j=0; j<n; j++)
|
|
|
|
if(dirbuf[j].qid.type & QTDIR){
|
|
|
|
snprint(name, nname, "%s/%s", f, dirbuf[j].name);
|
|
|
|
rmdir(name);
|
|
|
|
}
|
2016-12-26 01:16:31 +01:00
|
|
|
if((e = remove(f)) == -1)
|
|
|
|
err(e, f);
|
2016-11-25 17:18:40 +01:00
|
|
|
free(name);
|
|
|
|
free(dirbuf);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int recurse;
|
2016-12-26 01:16:31 +01:00
|
|
|
long e;
|
2016-11-25 17:18:40 +01:00
|
|
|
char *f;
|
|
|
|
Dir *db;
|
|
|
|
|
|
|
|
ignerr = 0;
|
|
|
|
recurse = 0;
|
|
|
|
ARGBEGIN{
|
|
|
|
case 'r':
|
|
|
|
recurse = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
ignerr = 1;
|
|
|
|
break;
|
2016-12-26 01:16:31 +01:00
|
|
|
case 'e':
|
|
|
|
printerr = 1;
|
|
|
|
break;
|
2016-11-25 17:18:40 +01:00
|
|
|
default:
|
2016-12-26 01:16:31 +01:00
|
|
|
fprint(2, "usage: rm [-fr|-e] file ...\n");
|
2016-11-25 17:18:40 +01:00
|
|
|
exits("usage");
|
|
|
|
}ARGEND
|
2016-12-26 01:16:31 +01:00
|
|
|
if(printerr)
|
|
|
|
ignerr = 0;
|
2016-11-25 17:18:40 +01:00
|
|
|
for(i=0; i<argc; i++){
|
|
|
|
f = argv[i];
|
2016-12-26 01:16:31 +01:00
|
|
|
e = remove(f);
|
|
|
|
if(e != -1 && (!printerr || e == 0))
|
2016-11-25 17:18:40 +01:00
|
|
|
continue;
|
|
|
|
db = nil;
|
|
|
|
if(recurse && (db=dirstat(f))!=nil && (db->qid.type&QTDIR))
|
|
|
|
rmdir(f);
|
|
|
|
else
|
2016-12-26 01:16:31 +01:00
|
|
|
err(e, f);
|
2016-11-25 17:18:40 +01:00
|
|
|
free(db);
|
|
|
|
}
|
|
|
|
exits(errbuf);
|
|
|
|
}
|