-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
50 lines (41 loc) · 682 Bytes
/
main.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
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
static const char* filename = "./merged/a";
int
open_file(int flag)
{
int fd = openat(AT_FDCWD, filename, flag);
if (!~fd) {
perror("openat");
return 1;
}
return close(fd);
}
int
change_owner()
{
if (!~chown(filename, getuid(), getgid())) {
perror("chown");
return 1;
}
return 0;
}
int
main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s (r|w|c)\n", argv[0]);
return 1;
}
switch (*argv[1]) {
case 'r':
return open_file(O_RDONLY);
case 'w':
return open_file(O_RDWR);
case 'c': // chown
return change_owner();
}
printf("unknown flag '%s'\n", argv[1]);
return 1;
}