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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <stdlib.h>
int rm(char *);
int removedir(const char * const);
bool rflag = false,
iflag = false,
fflag = false;
int main(int argc, char *argv[]) {
char arg = 0;
while ((arg = getopt(argc, argv, "Rrfi")) != -1) {
switch (arg) {
case 'r':
case 'R':
rflag = true;
break;
case 'f':
fflag = true;
iflag = false;
break;
case 'i':
iflag = true;
fflag = false;
break;
default:
printf("usage: rm [-f | -i] [-r] file ...\n");
break;
}
}
while (--argc > 0) {
if ((*++argv)[0] != '-') {
rm(*argv);
}
}
return 0;
}
int rm(char *path) {
char c = 0;
if (iflag) {
fprintf(stderr, "remove %s? [y/N] ", path);
c = getchar();
if (c != 'Y' && c != 'y') {
return -1;
}
}
struct stat sbuf;
if (lstat(path, &sbuf) != 0 && !fflag) {
perror("rm");
return -1;
}
if (!fflag) {
uid_t uid = getuid();
if (sbuf.st_uid != uid) {
fprintf(stderr, "remove %s? [y/N] ", path);
c = getchar();
if (c != 'Y' && c != 'y') {
return -1;
}
}
}
if (S_ISDIR(sbuf.st_mode) && rflag) {
if (removedir(path) != 0) {
return -1;
}
if (rmdir(path) != 0) {
perror("rm");
return -1;
}
} else if (S_ISDIR(sbuf.st_mode)) {
fprintf(stderr, "rm: %s: %s\n", path, strerror(EISDIR));
return -1;
} else {
if (unlink(path) != 0) {
perror("rm");
}
}
return 0;
}
int removedir(const char * const path) {
DIR *dir;
int status = 0;
if ((dir = opendir(path)) == NULL) {
perror("rm");
return -1;
}
struct dirent *entry = NULL;
while ((entry = readdir(dir)) != NULL) {
char *name = entry->d_name;
if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0) {
size_t len = strlen(path) + strlen(name) + 3;
char *newpath = (char *)malloc(len);
if (newpath == NULL) {
perror("rm");
exit(EXIT_FAILURE);
}
strncpy(newpath, path, len);
strncat(newpath, "/", (len -= strlen(path)));
strncat(newpath, name, --len);
status = rm(newpath);
free((void *)newpath);
}
if (status != 0) {
break;
}
}
if (closedir(dir) != 0) {
perror("rm");
return -1;
}
return status;
}
|