-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrm.c
More file actions
55 lines (55 loc) · 1.46 KB
/
Copy pathrm.c
File metadata and controls
55 lines (55 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define _POSIX_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#undef _POSIX_SOURCE
#include <unistd.h>
//rm -i 1.txt
//rm 1.txt
//rm -v 1.txt
void rem(char* filename, int op_v){
if (access(filename, F_OK) == 0 ) {
int status = remove(filename);
/*Check if file has been properly deleted*/
if (op_v == 1){
if(status == 0){
printf("Deleted successfully.\n");fflush(stdout);
}
else{
printf("Unable to delete!\n");fflush(stdout);
}
}
else if (status!=0){
printf("Unable to delete!\n");fflush(stdout);
}
return;
}
else {
if (op_v == 1){printf("Sorry! File doesn't exist\n");}
return;
}
}
int main(int argc, char* argv[]){
if (argc == 1){printf("Filename not provided!");return 0;}
if (strcmp(argv[1], "-i") == 0){
char* perm[1];
printf("Remove file '%s'? ", argv[1]);fflush(stdout);
fscanf(stdin, "%c", perm[0]);
if ((strcmp(perm[0], "y") == 0) || (strcmp(perm[0], "Y") == 0)){
rem(argv[2], 0);
return 0;
}
else{
printf("Not removing...\n");
fflush(stdout);
return 0;
}
}
else if (strcmp(argv[1], "-v") == 0){
rem(argv[2], 1);
return 0;
}
fflush(stdout);
rem(argv[1], 0);
return 0;
}