-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchown.c
53 lines (51 loc) · 1 KB
/
chown.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
#include "user.h"
#include "errno.h"
#include "pwd.h"
#include "grp.h"
int main(int argc, char** argv)
{
if (argc != 3) {
printf(2, "Usage: chown username file\n");
exit();
}
char* owner = argv[1];
char* group;
if ((group = strchr(argv[1], ':'))) {
*group = 0;
group++;
}
struct passwd* pass = 0;
uid_t uid = -1;
if (owner[0]) {
pass = getpwnam(owner);
if (pass == 0) {
printf(2, "Invalid user: %s\n", owner);
exit();
}
uid = pass->pw_uid;
}
struct group* grp = 0;
gid_t gid = -1;
if (group) {
grp = getgrnam(group);
if (grp == 0) {
printf(2, "Invalid group: %s\n", grp);
exit();
}
gid = grp->gr_gid;
}
if (chown(argv[2], uid, gid) < 0) {
switch (errno) {
case EPERM:
printf(2, "Permission denied\n");
break;
case ENOENT:
printf(2, "No such file or directory\n");
break;
default:
printf(2, "Unexpected errno: %d\n", errno);
break;
}
}
exit();
}