work around linux's lack of flags argument to fchmodat syscall
[musl] / src / stat / fchmodat.c
1 #include <sys/stat.h>
2 #include <fcntl.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include "syscall.h"
6
7 int fchmodat(int fd, const char *path, mode_t mode, int flag)
8 {
9         if (!flag) return syscall(SYS_fchmodat, fd, path, mode, flag);
10
11         if (flag != AT_SYMLINK_NOFOLLOW)
12                 return __syscall_ret(-EINVAL);
13
14         struct stat st;
15         int ret, fd2;
16         char proc[15+3*sizeof(int)];
17
18         if ((ret = __syscall(SYS_fstatat, fd, path, &st, flag)))
19                 return __syscall_ret(ret);
20         if (S_ISLNK(st.st_mode))
21                 return __syscall_ret(-EOPNOTSUPP);
22
23         if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY)) < 0) {
24                 if (fd2 == -ELOOP)
25                         return __syscall_ret(-EOPNOTSUPP);
26                 return __syscall_ret(fd2);
27         }
28
29         snprintf(proc, sizeof proc, "/proc/self/fd/%d", fd2);
30         if (!(ret = __syscall(SYS_stat, proc, &st)) && !S_ISLNK(st.st_mode))
31                 ret = __syscall(SYS_chmod, proc, mode);
32
33         __syscall(SYS_close, fd2);
34         return __syscall_ret(ret);
35 }