fchmodat.c 1.0 KB
Newer Older
R
Rich Felker 已提交
1
#include <sys/stat.h>
2 3
#include <fcntl.h>
#include <errno.h>
W
w00349915 已提交
4 5
#include <unsupported_api.h>

R
Rich Felker 已提交
6
#include "syscall.h"
7
#include "kstat.h"
R
Rich Felker 已提交
8 9 10

int fchmodat(int fd, const char *path, mode_t mode, int flag)
{
W
w00349915 已提交
11
	unsupported_api(__FUNCTION__);
12 13 14 15 16
	if (!flag) return syscall(SYS_fchmodat, fd, path, mode, flag);

	if (flag != AT_SYMLINK_NOFOLLOW)
		return __syscall_ret(-EINVAL);

17
	struct kstat st;
18 19 20 21 22 23 24 25
	int ret, fd2;
	char proc[15+3*sizeof(int)];

	if ((ret = __syscall(SYS_fstatat, fd, path, &st, flag)))
		return __syscall_ret(ret);
	if (S_ISLNK(st.st_mode))
		return __syscall_ret(-EOPNOTSUPP);

26
	if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY|O_CLOEXEC)) < 0) {
27 28 29 30 31
		if (fd2 == -ELOOP)
			return __syscall_ret(-EOPNOTSUPP);
		return __syscall_ret(fd2);
	}

32
	__procfdname(proc, fd2);
33
	ret = __syscall(SYS_fstatat, AT_FDCWD, proc, &st, 0);
34 35 36 37
	if (!ret) {
		if (S_ISLNK(st.st_mode)) ret = -EOPNOTSUPP;
		else ret = __syscall(SYS_fchmodat, AT_FDCWD, proc, mode);
	}
38 39 40

	__syscall(SYS_close, fd2);
	return __syscall_ret(ret);
R
Rich Felker 已提交
41
}