decouple struct stat from kernel type
[musl] / src / stat / fstatat.c
1 #define _BSD_SOURCE
2 #include <sys/stat.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include "syscall.h"
7 #include "kstat.h"
8
9 int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag)
10 {
11         int ret;
12         struct kstat kst;
13
14         if (flag==AT_EMPTY_PATH && fd>=0 && !*path) {
15                 ret = __syscall(SYS_fstat, fd, st);
16                 if (ret==-EBADF && __syscall(SYS_fcntl, fd, F_GETFD)>=0) {
17                         ret = __syscall(SYS_fstatat, fd, path, st, flag);
18                         if (ret==-EINVAL) {
19                                 char buf[15+3*sizeof(int)];
20                                 __procfdname(buf, fd);
21 #ifdef SYS_stat
22                                 ret = __syscall(SYS_stat, buf, st);
23 #else
24                                 ret = __syscall(SYS_fstatat, AT_FDCWD, buf, st, 0);
25 #endif
26                         }
27                 }
28         }
29 #ifdef SYS_lstat
30         else if ((fd == AT_FDCWD || *path=='/') && flag==AT_SYMLINK_NOFOLLOW)
31                 ret = __syscall(SYS_lstat, path, &kst);
32 #endif
33 #ifdef SYS_stat
34         else if ((fd == AT_FDCWD || *path=='/') && !flag)
35                 ret = __syscall(SYS_stat, path, &kst);
36 #endif
37         else ret = __syscall(SYS_fstatat, fd, path, &kst, flag);
38
39         if (ret) return __syscall_ret(ret);
40
41         *st = (struct stat){
42                 .st_dev = kst.st_dev,
43                 .st_ino = kst.st_ino,
44                 .st_mode = kst.st_mode,
45                 .st_nlink = kst.st_nlink,
46                 .st_uid = kst.st_uid,
47                 .st_gid = kst.st_gid,
48                 .st_rdev = kst.st_rdev,
49                 .st_size = kst.st_size,
50                 .st_blksize = kst.st_blksize,
51                 .st_blocks = kst.st_blocks,
52                 .st_atim.tv_sec = kst.st_atime_sec,
53                 .st_atim.tv_nsec = kst.st_atime_nsec,
54                 .st_mtim.tv_sec = kst.st_mtime_sec,
55                 .st_mtim.tv_nsec = kst.st_mtime_nsec,
56                 .st_ctim.tv_sec = kst.st_ctime_sec,
57                 .st_ctim.tv_nsec = kst.st_ctime_nsec,
58         };
59
60         return 0;
61 }
62
63 weak_alias(fstatat, fstatat64);