-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.c
68 lines (57 loc) · 1.44 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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void rm(char *arr[])
{
if (arr[2] != NULL)
{
arr[2][strcspn(arr[2], "\n")] = 0;
if (!strcmp(arr[1], "-v"))
{
if (remove(arr[2]))
{
printf("rm: cannot remove '%s': No such file or directory\n", arr[2]);
}
else
{
printf("removed '%s'\n", arr[2]);
}
}
else if (!strcmp(arr[1], "-i"))
{
char arrr[1000];
printf("rm: remove regular file '%s'? ", arr[2]);
fgets(arrr, sizeof(arrr), stdin);
if (((int)arrr[0] == 121) || ((int)arrr[0] == 89) || !strcmp(&arrr[0], "y\n") || !strcmp(&arrr[0], "Y\n"))
{
if (remove(arr[2]))
{
printf("rm: cannot remove '%s': No such file or directory\n", arr[2]);
}
}
}
else
{
printf("rm: invalid option %s\n", arr[1]);
}
}
else if (arr[1] != NULL)
{
arr[1][strcspn(arr[1], "\n")] = 0;
if (remove(arr[1]))
{
printf("rm: cannot remove '%s': No such file or directory\n", arr[1]);
}
}
else
{
printf("rm: missing operand\n");
}
}
int main(int argc, char *argv[])
{
char *arr[] = {argv[1], argv[2], argv[3]};
rm(arr);
return 0;
}