-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchmodarx.c
More file actions
executable file
·48 lines (40 loc) · 906 Bytes
/
chmodarx.c
File metadata and controls
executable file
·48 lines (40 loc) · 906 Bytes
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
/*
reproduce chmod a+rX
enable read persmissions on dirs and files
enable execute permission for directories
enable execute permission on files with x on any other category
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
void error(char *reason) {
puts(reason);
exit(EXIT_FAILURE);
}
void update_permissions(char *file) {
struct stat fstat;
mode_t mode, allx;
if (stat(file, &fstat) == -1) {
error("stat\n");
}
mode = fstat.st_mode;
mode |= (S_IRUSR | S_IRGRP | S_IROTH);
allx = S_IXUSR | S_IXGRP | S_IXOTH;
if (S_ISDIR(fstat.st_mode) || fstat.st_mode & allx) {
mode |= allx;
}
if (chmod(file, mode) == -1) {
error("chmod\n");
}
}
int main(int argc, char **argv) {
if (argc < 2) {
error("usage: chmodarx <file> ...\n");
}
for (int i = 1; i < argc; i++) {
update_permissions(argv[i]);
}
exit(EXIT_SUCCESS);
}