xref: /haiku/src/system/libroot/posix/sys/chmod.c (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
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 <sys/stat.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 chmod(const char *path, mode_t mode)
24 {
25 	struct stat stat;
26 	status_t status;
27 
28 	stat.st_mode = mode;
29 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
30 		FS_WRITE_STAT_MODE);
31 
32 	RETURN_AND_SET_ERRNO(status);
33 }
34 
35 
36 int
37 fchmod(int fd, mode_t mode)
38 {
39 	struct stat stat;
40 	status_t status;
41 
42 	stat.st_mode = mode;
43 	status = _kern_write_stat(fd, NULL, false, &stat, sizeof(struct stat),
44 		FS_WRITE_STAT_MODE);
45 
46 	RETURN_AND_SET_ERRNO(status);
47 }
48