85 lines
1.3 KiB
C
85 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <dirent.h>
|
|
#include <unistd.h>
|
|
#include "make_path.h"
|
|
#include "get_stat.h"
|
|
#include "recurse.h"
|
|
#include "unused.h"
|
|
|
|
char *f_flag = "rm";
|
|
unsigned int r_flag;
|
|
|
|
int rm(const char *path, void *p) {
|
|
UNUSED(p);
|
|
if (remove(path) < 0) {
|
|
if (f_flag)
|
|
fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int rmd(const char *path, void *p) {
|
|
UNUSED(p);
|
|
if (rmdir(path) < 0) {
|
|
if (f_flag)
|
|
fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "frR")) != -1) {
|
|
switch (opt) {
|
|
case 'f':
|
|
f_flag = NULL;
|
|
break;
|
|
|
|
case 'r':
|
|
r_flag = 1;
|
|
break;
|
|
|
|
default:
|
|
printf("rm [file1 file2...]\n\t[-f Force] [-r Recursive]\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (argv[optind] == NULL) {
|
|
fprintf(stderr, "rm: missing operand\n");
|
|
return 1;
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
int ret = 0;
|
|
for (int i = 0; i < argc; i++) {
|
|
if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){
|
|
printf("rm: refusing to remove '.' or '..' directory\n");
|
|
break;
|
|
}
|
|
|
|
|
|
if (r_flag) {
|
|
if (mu_recurse(f_flag, 1, argv[i], NULL, rm, rmd))
|
|
ret = 1;
|
|
}
|
|
|
|
else
|
|
if (rm(argv[i], NULL))
|
|
ret = 1;
|
|
}
|
|
|
|
return ret;
|
|
}
|