xref: /haiku/src/system/libroot/posix/unistd/chown.c (revision e7be020ce59cd8a50dcb9a782b3b15cfa769396c)
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 <errno_private.h>
13 #include <syscalls.h>
14 #include <syscall_utils.h>
15 
16 
17 int
18 chown(const char *path, uid_t owner, gid_t group)
19 {
20 	struct stat stat;
21 	status_t status;
22 
23 	stat.st_uid = owner;
24 	stat.st_gid = group;
25 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
26 		B_STAT_UID | B_STAT_GID);
27 
28 	RETURN_AND_SET_ERRNO(status);
29 }
30 
31 
32 int
33 lchown(const char *path, uid_t owner, gid_t group)
34 {
35 	struct stat stat;
36 	status_t status;
37 
38 	stat.st_uid = owner;
39 	stat.st_gid = group;
40 	status = _kern_write_stat(-1, path, false, &stat, sizeof(struct stat),
41 		B_STAT_UID | B_STAT_GID);
42 
43 	RETURN_AND_SET_ERRNO(status);
44 }
45 
46 
47 int
48 fchown(int fd, uid_t owner, gid_t group)
49 {
50 	struct stat stat;
51 	status_t status;
52 
53 	stat.st_uid = owner;
54 	stat.st_gid = group;
55 	status = _kern_write_stat(fd, NULL, false, &stat, sizeof(struct stat),
56 		B_STAT_UID | B_STAT_GID);
57 
58 	RETURN_AND_SET_ERRNO(status);
59 }
60 
61 
62 int
63 fchownat(int fd, const char* path, uid_t owner, gid_t group, int flag)
64 {
65 	struct stat stat;
66 	status_t status;
67 
68 	stat.st_uid = owner;
69 	stat.st_gid = group;
70 	status = _kern_write_stat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) == 0, &stat,
71 		sizeof(struct stat), B_STAT_UID | B_STAT_GID);
72 
73 	RETURN_AND_SET_ERRNO(status);
74 }
75