-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathopen_trait.c
55 lines (43 loc) · 1.14 KB
/
open_trait.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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
struct stat s;
int fd;
int ret;
if (argc != 3) {
fprintf(stderr, "<%s> filename mode\n", argv[0]);
fprintf(stderr, "mode is one of user, user_group, user_others\n");
return -1;
}
mode_t mask = 0;
mask = umask(mask);
printf("creation of old mask %o\n", mask);
mode_t mode = 0;
if (!strcmp(argv[2], "user")) {
mode = S_IRUSR | S_IWUSR;
} else if (!strcmp(argv[2], "user_group")) {
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
} else if (!strcmp(argv[2], "user_others")) {
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
}
if (mode == 0) {
return -1;
}
printf("mode bits %o\n", mode);
fd = open(argv[1], O_CREAT | O_WRONLY | O_TRUNC, mode);
if (fd < 0) {
return -1;
}
ret = stat(argv[1], &s);
if (ret < 0) {
return -1;
}
printf("file %s opened, mode %o\n", argv[1], s.st_mode & 0xfff);
close(fd);
return 0;
}