aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rm/rm.c
blob: 7e83f5f4b85d32993f6999510a13e31dccd53992 (plain)
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
#include <u.h>
#include <libn.h>
#include <sys/stat.h>

/*
 * globals
 */

static char errbuf[1024];
static int  numerrs;
static struct
{
    uchar i : 1;
    uchar f : 1;
    uchar r : 1;
} flag;

/*
 * utilities
 */

static
void
errors(char *fmt, ...)
{
    va_list args;

    if(flag.f)
        return;

    va_start(args, fmt);
    vsnprintf(errbuf, arrlen(errbuf), fmt, args);
    va_end(args);

    fprintf(stderr, "rm: %s\n", errbuf);
    numerrs++;
}

static
int
yes(void)
{
    int i, b;
    i = b = getchar();
    while(b != '\n' && b != EOF)
        b = getchar();

    return(i== 'y');
}

static
int
rm(char *path, int level)
{
    int d;
    struct stat buf;
    if(stat(path, &buf)) {
        errors("%s: non-existent", path);
        return 1;
    }

    if((buf.st_mode&S_IFMT) == S_IFDIR) {
        if(!flag.r) {
            errors("%s: is directory", path);
            return 1;
        }
        if(access(path, 02) < 0) {
            errors("%s: not permitted", path);
            return 1;
        }
        if(flag.i && level != 0) {
            printf("directory %s: ", path);
            if(!yes())
                return 0;
        }
        if((d=
    }
}

/*
 * main point of entry
 */

static
void
usage(void)
{
    fprintf(stderr, "usage: rm [-ifr] file ...\n");
    exit(1);
}

int
main(int argc, char *argv[])
{
    int   i;
    char *s;

    ARGBEGIN{
    case 'i':
        flag.i = 1;
        break;
    case 'r':
        flag.r = 1;
        break;
    case 'f':
        flag.f = 1;
        break;
    default:
        usage();
    }ARGEND;

    for(i = 0; i < argc; i++){
        s = argv[i];
        if (rm(s, 0) != -1)
            continue;
    }

    exit(numerrs);
}