2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-05-04 11:17:11 +02:00
|
|
|
#define OPT_RECURSIVE 1
|
|
|
|
#define OPT_FORCE 2
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static int usage()
|
|
|
|
{
|
2012-05-04 11:17:11 +02:00
|
|
|
fprintf(stderr,"Usage: rm [-rR] [-f] <target>\n");
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return -1 on failure, with errno set to the first error */
|
2012-05-04 11:17:11 +02:00
|
|
|
static int unlink_recursive(const char* name, int flags)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
|
|
|
int fail = 0;
|
|
|
|
|
|
|
|
/* is it a file or directory? */
|
|
|
|
if (lstat(name, &st) < 0)
|
2012-05-04 11:17:11 +02:00
|
|
|
return ((flags & OPT_FORCE) && errno == ENOENT) ? 0 : -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* a file, so unlink it */
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
return unlink(name);
|
|
|
|
|
|
|
|
/* a directory, so open handle */
|
|
|
|
dir = opendir(name);
|
|
|
|
if (dir == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* recurse over components */
|
|
|
|
errno = 0;
|
|
|
|
while ((de = readdir(dir)) != NULL) {
|
|
|
|
char dn[PATH_MAX];
|
|
|
|
if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
|
|
|
|
continue;
|
|
|
|
sprintf(dn, "%s/%s", name, de->d_name);
|
2012-05-04 11:17:11 +02:00
|
|
|
if (unlink_recursive(dn, flags) < 0) {
|
2013-06-26 07:29:06 +02:00
|
|
|
if (!(flags & OPT_FORCE)) {
|
|
|
|
fail = 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
/* in case readdir or unlink_recursive failed */
|
|
|
|
if (fail || errno < 0) {
|
|
|
|
int save = errno;
|
|
|
|
closedir(dir);
|
|
|
|
errno = save;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close directory handle */
|
|
|
|
if (closedir(dir) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* delete target directory */
|
|
|
|
return rmdir(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rm_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret;
|
2012-05-04 11:17:11 +02:00
|
|
|
int i, c;
|
|
|
|
int flags = 0;
|
2013-06-26 07:29:06 +02:00
|
|
|
int something_failed = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
return usage();
|
|
|
|
|
2012-05-04 11:17:11 +02:00
|
|
|
/* check flags */
|
|
|
|
do {
|
|
|
|
c = getopt(argc, argv, "frR");
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'f':
|
|
|
|
flags |= OPT_FORCE;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
flags |= OPT_RECURSIVE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (optind < 1 || optind >= argc) {
|
|
|
|
usage();
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2012-05-04 11:17:11 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* loop over the file/directory args */
|
2012-05-04 11:17:11 +02:00
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
|
|
|
|
if (flags & OPT_RECURSIVE) {
|
|
|
|
ret = unlink_recursive(argv[i], flags);
|
|
|
|
} else {
|
|
|
|
ret = unlink(argv[i]);
|
2013-01-16 03:43:01 +01:00
|
|
|
if (ret < 0 && errno == ENOENT && (flags & OPT_FORCE)) {
|
|
|
|
continue;
|
2012-05-04 11:17:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "rm failed for %s, %s\n", argv[i], strerror(errno));
|
2013-06-26 07:29:06 +02:00
|
|
|
if (!(flags & OPT_FORCE)) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
something_failed = 1;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 07:29:06 +02:00
|
|
|
return something_failed;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|