master
1/* Get file status. Linux version.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19/* zig patch: fixes to make this file compile */
20
21#include <inttypes.h>
22#include <sys/stat.h>
23#include <kernel_stat.h>
24#include <sysdep.h>
25#include <string.h>
26
27#if !XSTAT_IS_XSTAT64
28
29static inline bool
30in_time_t_range (__time64_t t)
31{
32 time_t s = t;
33 return s == t;
34}
35
36static inline struct timespec
37valid_timespec64_to_timespec (const struct __timespec64 ts64)
38{
39 struct timespec ts;
40
41 ts.tv_sec = (time_t) ts64.tv_sec;
42 ts.tv_nsec = ts64.tv_nsec;
43
44 return ts;
45}
46
47int
48__fstatat (int fd, const char *file, struct stat *buf, int flag)
49{
50 struct __stat64_t64 st64;
51 int r = __fstatat64_time64 (fd, file, &st64, flag);
52 if (r == 0)
53 {
54 if (! in_ino_t_range (st64.st_ino)
55 || ! in_off_t_range (st64.st_size)
56 || ! in_blkcnt_t_range (st64.st_blocks)
57 || ! in_time_t_range (st64.st_atim.tv_sec)
58 || ! in_time_t_range (st64.st_mtim.tv_sec)
59 || ! in_time_t_range (st64.st_ctim.tv_sec))
60 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
61
62 /* Clear internal pad and reserved fields. */
63 memset (buf, 0, sizeof (*buf));
64
65 buf->st_dev = st64.st_dev;
66 buf->st_ino = st64.st_ino;
67 buf->st_mode = st64.st_mode;
68 buf->st_nlink = st64.st_nlink;
69 buf->st_uid = st64.st_uid;
70 buf->st_gid = st64.st_gid;
71 buf->st_rdev = st64.st_rdev;
72 buf->st_size = st64.st_size;
73 buf->st_blksize = st64.st_blksize;
74 buf->st_blocks = st64.st_blocks;
75 buf->st_atim = valid_timespec64_to_timespec (st64.st_atim);
76 buf->st_mtim = valid_timespec64_to_timespec (st64.st_mtim);
77 buf->st_ctim = valid_timespec64_to_timespec (st64.st_ctim);
78 }
79 return r;
80}
81
82weak_alias (__fstatat, fstatat)
83#endif