-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.c
79 lines (77 loc) · 1.76 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
extern int errno;
int fd;
struct stat st;
int main(int noOfArguements, char *args[])
{
if(noOfArguements==1)
printf("rm: missing operand \n" );
else{
int flagV = 0, flagI = 0;
for (int a = 0; a < noOfArguements; a++)
{
if (strcmp(args[a], "-i") == 0)
flagI = 1;
if (strcmp(args[a], "-v") == 0)
flagV = 1;
}
int ini=1;
while(ini<noOfArguements)
{
if (strcmp(args[ini], "-i") == 0 || strcmp(args[ini], "-v") == 0)
{
ini++;
continue;
}
else if (stat(args[ini], &st) == 0 && S_ISREG(st.st_mode)){
if(flagI==1)
{ char str[20];
printf("rm: remove regular file %s? ",args[ini] );
scanf("%s", str);
if(strcmp(str,"y")==0 || strcmp(str,"Y")==0)
{
if(remove(args[ini])==0)
{
if(flagV==1)
printf("removed %s \n",args[ini] );
}
else
{
printf("Error no %d \n",errno);
perror("rm");
}
int len=strlen(str);
for(int abc=0;abc<len;abc++)
str[abc]='\0';
}
}
if(flagI==0)
{
if(remove(args[ini])==0)
{
if(flagV==1)
printf("removed %s \n",args[ini] );
}
else
{
printf("Error no %d \n",errno);
perror("rm");
}
}
ini++;
}
else{
printf("cannot remove %s \n",args[ini] );
ini++;
}
}
}
return 0;
}