xref: /haiku/src/system/libroot/posix/unistd/chown.c (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <errno.h>
8 #include <unistd.h>
9 
10 #include <NodeMonitor.h>
11 
12 #include <syscalls.h>
13 #include <syscall_utils.h>
14 
15 
16 int
17 chown(const char *path, uid_t owner, gid_t group)
18 {
19 	struct stat stat;
20 	status_t status;
21 
22 	stat.st_uid = owner;
23 	stat.st_gid = group;
24 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
25 		B_STAT_UID | B_STAT_GID);
26 
27 	RETURN_AND_SET_ERRNO(status);
28 }
29 
30 
31 int
32 lchown(const char *path, uid_t owner, gid_t group)
33 {
34 	struct stat stat;
35 	status_t status;
36 
37 	stat.st_uid = owner;
38 	stat.st_gid = group;
39 	status = _kern_write_stat(-1, path, false, &stat, sizeof(struct stat),
40 		B_STAT_UID | B_STAT_GID);
41 
42 	RETURN_AND_SET_ERRNO(status);
43 }
44 
45 
46 int
47 fchown(int fd, uid_t owner, gid_t group)
48 {
49 	struct stat stat;
50 	status_t status;
51 
52 	stat.st_uid = owner;
53 	stat.st_gid = group;
54 	status = _kern_write_stat(fd, NULL, false, &stat, sizeof(struct stat),
55 		B_STAT_UID | B_STAT_GID);
56 
57 	RETURN_AND_SET_ERRNO(status);
58 }
59 
60 
61 int
62 fchownat(int fd, const char* path, uid_t owner, gid_t group, int flag)
63 {
64 	struct stat stat;
65 	status_t status;
66 
67 	stat.st_uid = owner;
68 	stat.st_gid = group;
69 	status = _kern_write_stat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) == 0, &stat,
70 		sizeof(struct stat), B_STAT_UID | B_STAT_GID);
71 
72 	RETURN_AND_SET_ERRNO(status);
73 }
74