This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.c
73 lines (67 loc) · 1.56 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
extern int optind;
int main(int argc, char *argv[])
{
const char *PROGNAME = argv[0];
int force = 0;
int interact = 0;
int dir = 0;
int verbose = 0;
int opt;
while ((opt = getopt(argc, argv, "fidv")) != -1) {
switch (opt) {
case 'f':
force = 1;
break;
case 'i':
interact = 1;
break;
case 'd':
dir = 1;
break;
case 'v':
verbose = 1;
break;
default: /* ? */
return -1;
}
}
argc -= optind;
argv += optind;
for (; *argv; argv++) {
struct stat statbuf;
if (stat(*argv, &statbuf) == -1) {
if (!force) {
perror(PROGNAME);
}
continue;
}
if (S_ISDIR(statbuf.st_mode) && !dir) {
if (!force) {
fprintf(stderr, "%s: cannot remove '%s': Is a directory\n",
PROGNAME, *argv);
}
continue;
}
if (interact) {
printf("%s: remove '%s'? (y/N) ", PROGNAME, *argv);
char c;
scanf(" %c", &c);
if (c != 'y') {
continue;
}
}
if (remove(*argv) == -1) {
if (!force) {
perror(PROGNAME);
}
}
if (verbose) {
printf("%s: removed '%s'\n", PROGNAME, *argv);
}
}
return 0;
}