2011-05-23 20:52:28 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void rm(const char *);
|
|
|
|
|
2011-05-24 07:32:33 -04:00
|
|
|
static bool fflag = false;
|
|
|
|
static bool rflag = false;
|
2011-05-23 20:52:28 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
while((c = getopt(argc, argv, "fr")) != -1)
|
|
|
|
switch(c) {
|
|
|
|
case 'f':
|
2011-05-24 07:32:33 -04:00
|
|
|
fflag = true;
|
2011-05-23 20:52:28 -04:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
for(; optind < argc; optind++)
|
|
|
|
rm(argv[optind]);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rm(const char *path)
|
|
|
|
{
|
|
|
|
if(remove(path) == 0)
|
|
|
|
return;
|
|
|
|
if(errno == ENOTEMPTY && rflag) {
|
2011-05-24 08:04:56 -04:00
|
|
|
char *buf;
|
|
|
|
long size;
|
2011-05-23 20:52:28 -04:00
|
|
|
struct dirent *d;
|
|
|
|
DIR *dp;
|
|
|
|
|
2011-05-24 08:04:56 -04:00
|
|
|
if((size = pathconf(".", _PC_PATH_MAX)) < 0)
|
|
|
|
size = BUFSIZ;
|
|
|
|
if(!(buf = malloc(size)))
|
|
|
|
eprintf("malloc:");
|
|
|
|
if(!getcwd(buf, size))
|
|
|
|
eprintf("getcwd:");
|
2011-05-23 20:52:28 -04:00
|
|
|
if(!(dp = opendir(path)))
|
|
|
|
eprintf("opendir %s:", path);
|
|
|
|
if(chdir(path) != 0)
|
|
|
|
eprintf("chdir %s:", path);
|
|
|
|
while((d = readdir(dp)))
|
|
|
|
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
|
|
|
|
rm(d->d_name);
|
|
|
|
|
|
|
|
closedir(dp);
|
2011-05-24 08:04:56 -04:00
|
|
|
if(chdir(buf) != 0)
|
|
|
|
eprintf("chdir %s:", buf);
|
2011-05-23 20:52:28 -04:00
|
|
|
if(remove(path) == 0)
|
|
|
|
return;
|
|
|
|
}
|
2011-05-24 07:32:33 -04:00
|
|
|
if(!fflag)
|
|
|
|
eprintf("remove %s:", path);
|
2011-05-23 20:52:28 -04:00
|
|
|
}
|