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