-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.c
56 lines (49 loc) · 949 Bytes
/
rm.c
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
56
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define sz 100
int main(int argc, char **argv)
{
errno = 0;
char *flag = argv[1], *args = argv[2];
struct stat file_stat;
if(strlen(args) == 0){
printf("%s\n", "Error: File name not given!");
return 0;
}
if(strlen(flag) == 0){
int check = unlink(args);
if(check == -1){
perror("Error");
}
}
else if(strlen(flag) == 1){
if(flag[0] == 'd'){ //to remove directory
int check = remove(args);
if(check == -1){
perror("Error");
}
}
else if(flag[0] == 'v'){
int check = unlink(args);
if(check == -1){
perror("Error");
printf("\n");
}
else{
printf("Removed \"%s\"", args);
}
}
else
printf("\nError: Invalid flag!\n");
}
else
printf("%s\n", "Error: Invalid flag!");
return 0;
}