-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchmod.c
More file actions
173 lines (146 loc) · 4.01 KB
/
chmod.c
File metadata and controls
173 lines (146 loc) · 4.01 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "types.h"
#include "stat.h"
#include "fcntl.h"
#include "user.h"
#define ADD_ALL_READ 292
#define ADD_ALL_WRITE 146
#define ADD_ALL_EXEC 73
#define ADD_U_READ 256
#define ADD_U_WRITE 128
#define ADD_U_EXEC 64
#define ADD_O_READ 4
#define ADD_O_WRITE 2
#define ADD_O_EXEC 1
#define REM_ALL_READ 219
#define REM_ALL_WRITE 365
#define REM_ALL_EXEC 438
#define REM_U_READ 255
#define REM_U_WRITE 383
#define REM_U_EXEC 447
#define REM_O_READ 507
#define REM_O_WRITE 509
#define REM_O_EXEC 510
int pow(int x, unsigned int y)
{
if (y == 0)
return 1;
else if ((y % 2) == 0)
return pow (x, y / 2) * pow (x, y / 2);
else
return x * pow (x, y / 2) * pow (x, y / 2);
}
long long octtodec(int oct)
{
int dec = 0, i = 0;
while(oct != 0)
{
dec += (oct%10) * pow(8,i);
++i;
oct/=10;
}
i = 1;
return dec;
}
int
main(int argc, char *argv[])
{
if (argc < 3) {
printf("chmod: invalid arg count\n");
exit();
}
// chmod mode file ...
if (strstr(argv[1], "+") == NULL && strstr(argv[1], "-") == NULL) {
int mode = atoi(argv[1]);
if (mode < 0) {
printf("chmod: invalid mode\n");
exit();
}
int i;
for (i = 2; i < argc; i ++) {
chmod(argv[i], octtodec(mode));
}
exit();
}
if (strlen(argv[1]) != 3) {
printf("chmod: invalid mode format\n");
exit();
}
char whom = argv[1][0];
char symbol = argv[1][1];
char perm = argv[1][2];
if (whom != 'u' && whom != 'o' && whom != 'a') {
printf("chmod: invalid whom\n");
exit();
}
if (symbol != '+' && symbol != '-') {
printf("chmod: invalid operator\n");
exit();
}
if (perm != 'r' && perm != 'w' && perm != 'x') {
printf("chmod: invalid permission\n");
exit();
}
int i, fd, mode;
struct stat st;
for (i = 2; i < argc; i ++) {
if((fd = open(argv[i], 0)) < 0) {
printf("chmod: cannot open %s\n", argv[i]);
exit();
}
if(fstat(fd, &st) < 0){
printf("chmod: cannot stat %s\n", argv[i]);
close(fd);
exit();
}
mode = st.mode;
if (symbol == '+') {
if (whom == 'a') {
if (perm == 'r')
mode |= ADD_ALL_READ;
else if (perm == 'w')
mode |= ADD_ALL_WRITE;
else if (perm == 'x')
mode |= ADD_ALL_EXEC;
} else if (whom == 'u') {
if (perm == 'r')
mode |= ADD_U_READ;
else if (perm == 'w')
mode |= ADD_U_WRITE;
else if (perm == 'x')
mode |= ADD_U_EXEC;
} else if (whom == 'o') {
if (perm == 'r')
mode |= ADD_O_READ;
else if (perm == 'w')
mode |= ADD_O_WRITE;
else if (perm == 'x')
mode |= ADD_O_EXEC;
}
} else if (symbol == '-') {
if (whom == 'a') {
if (perm == 'r')
mode &= REM_ALL_READ;
else if (perm == 'w')
mode &= REM_ALL_WRITE;
else if (perm == 'x')
mode &= REM_ALL_EXEC;
} else if (whom == 'u') {
if (perm == 'r')
mode &= REM_U_READ;
else if (perm == 'w')
mode &= REM_U_WRITE;
else if (perm == 'x')
mode &= REM_U_EXEC;
} else if (whom == 'o') {
if (perm == 'r')
mode &= REM_O_READ;
else if (perm == 'w')
mode &= REM_O_WRITE;
else if (perm == 'x')
mode &= REM_O_EXEC;
}
}
chmod(argv[i], mode);
}
exit();
}