xref: /haiku/src/system/libroot/posix/sys/chmod.c (revision 239222b2369c39dc52df52b0a7cdd6cc0a91bc92)
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 <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 		B_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 		B_STAT_MODE);
45 
46 	RETURN_AND_SET_ERRNO(status);
47 }
48