121 lines
1.9 KiB
C
121 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <dirent.h>
|
|
#include <unistd.h>
|
|
#include <ctype.h>
|
|
#include "make_path.h"
|
|
#include "get_stat.h"
|
|
#include "recurse.h"
|
|
#include "unused.h"
|
|
|
|
static char *f_flag = "rm";
|
|
static char r_flag;
|
|
static char i_flag;
|
|
static char v_flag;
|
|
|
|
static int verbose(const char *path) {
|
|
if (i_flag) {
|
|
fprintf(stderr, "rm: remove %s? [y/n] ", path);
|
|
fflush(stderr);
|
|
|
|
int c = 0;
|
|
int key = 0;
|
|
|
|
while ((c = fgetc(stdin)) != EOF && c != '\n') {
|
|
if (c == 'y')
|
|
key = 1;
|
|
}
|
|
|
|
return key;
|
|
}
|
|
|
|
else if (v_flag && f_flag)
|
|
fprintf(stderr, "rm: removing %s\n", path);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static void handle_error(const char *path) {
|
|
if (f_flag)
|
|
fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
|
|
}
|
|
|
|
static int rm(const char *path, void *p) {
|
|
UNUSED(p);
|
|
|
|
if (verbose(path) && remove(path) < 0) {
|
|
handle_error(path);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int rmd(const char *path, void *p) {
|
|
UNUSED(p);
|
|
|
|
if (verbose(path) && rmdir(path) < 0) {
|
|
handle_error(path);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "friv")) != -1) {
|
|
switch (opt) {
|
|
case 'f':
|
|
f_flag = NULL;
|
|
break;
|
|
|
|
case 'r':
|
|
r_flag = 1;
|
|
break;
|
|
|
|
case 'i':
|
|
i_flag = 1;
|
|
break;
|
|
|
|
case 'v':
|
|
v_flag = 1;
|
|
break;
|
|
|
|
default:
|
|
puts("rm [friv] [file1 file2/dir...]\n\t-f Never prompt\n\t-r Recursive\n\t-i Print prompt before remove\n\t-v Verbose");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
argc -= optind;
|
|
argv += optind;
|
|
|
|
if (argc == 0) {
|
|
fprintf(stderr, "rm: missing operand\n");
|
|
return 1;
|
|
}
|
|
|
|
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;
|
|
}
|