add i flag in rm

This commit is contained in:
Your Name 2024-03-19 10:42:20 +03:00
parent 9f12d068fc
commit e33ad2a512
1 changed files with 38 additions and 8 deletions

View File

@ -4,6 +4,7 @@
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include "make_path.h"
#include "get_stat.h"
#include "recurse.h"
@ -11,13 +12,39 @@
char *f_flag = "rm";
char r_flag;
char v_flag;
int verbose(const char *path) {
if (v_flag) {
fprintf(stderr, "rm: remove %s? [y/n] ", path);
fflush(stderr);
fflush(stdin);
int c = 0;
int key = 0;
while ((c = fgetc(stdin)) != EOF && c != '\n') {
if (c == 'y')
key = 1;
}
return key;
}
return 1;
}
void handle_error(const char *path) {
if (f_flag)
fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
}
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));
if (verbose(path) && remove(path) < 0) {
handle_error(path);
return 1;
}
@ -26,10 +53,9 @@ int rm(const char *path, void *p) {
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));
if (verbose(path) && rmdir(path) < 0) {
handle_error(path);
return 1;
}
@ -38,7 +64,7 @@ int rmd(const char *path, void *p) {
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "frR")) != -1) {
while ((opt = getopt(argc, argv, "frRi")) != -1) {
switch (opt) {
case 'f':
f_flag = NULL;
@ -48,8 +74,12 @@ int main(int argc, char **argv) {
r_flag = 1;
break;
case 'i':
v_flag = 1;
break;
default:
printf("rm [rf] [file1 file2...]\n\t-f Force\n\t-r Recursive\n");
printf("rm [rif] [file1 file2...]\n\t-f Force\n\t-r Recursive\n\t-i Print prompt before remove\n");
return 0;
}
}