xref: /haiku/src/system/libroot/posix/unistd/chown.c (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2 ** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 */
5 
6 
7 #include <fs_interface.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 		FS_WRITE_STAT_UID | FS_WRITE_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 		FS_WRITE_STAT_UID | FS_WRITE_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 		FS_WRITE_STAT_UID | FS_WRITE_STAT_GID);
62 
63 	RETURN_AND_SET_ERRNO(status);
64 }
65 
66