release 1.2.5
[musl.git] / src / stat / fchmodat.c
blob92c9d1b0e4a2ca140f0c554d99d60fe4ac1363b0
1 #include <sys/stat.h>
2 #include <fcntl.h>
3 #include <errno.h>
4 #include "syscall.h"
6 int fchmodat(int fd, const char *path, mode_t mode, int flag)
8 if (!flag) return syscall(SYS_fchmodat, fd, path, mode);
10 int ret = __syscall(SYS_fchmodat2, fd, path, mode, flag);
11 if (ret != -ENOSYS) return __syscall_ret(ret);
13 if (flag != AT_SYMLINK_NOFOLLOW)
14 return __syscall_ret(-EINVAL);
16 struct stat st;
17 int fd2;
18 char proc[15+3*sizeof(int)];
20 if (fstatat(fd, path, &st, flag))
21 return -1;
22 if (S_ISLNK(st.st_mode))
23 return __syscall_ret(-EOPNOTSUPP);
25 if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY|O_CLOEXEC)) < 0) {
26 if (fd2 == -ELOOP)
27 return __syscall_ret(-EOPNOTSUPP);
28 return __syscall_ret(fd2);
31 __procfdname(proc, fd2);
32 ret = stat(proc, &st);
33 if (!ret) {
34 if (S_ISLNK(st.st_mode)) ret = __syscall_ret(-EOPNOTSUPP);
35 else ret = syscall(SYS_fchmodat, AT_FDCWD, proc, mode);
38 __syscall(SYS_close, fd2);
39 return ret;