1 /* stat-related time functions.
3 Copyright (C) 2005, 2007, 2009-2019 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
30 #ifndef _GL_INLINE_HEADER_BEGIN
31 #error "Please include config.h first."
33 _GL_INLINE_HEADER_BEGIN
34 #ifndef _GL_STAT_TIME_INLINE
35 # define _GL_STAT_TIME_INLINE _GL_INLINE
42 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
43 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
44 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
45 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
46 for access, status change, data modification, or birth (creation)
49 These macros are private to stat-time.h. */
50 #if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
51 # if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
52 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
54 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
56 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
57 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
58 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
59 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
60 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
61 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
64 /* Return the nanosecond component of *ST's access time. */
65 _GL_STAT_TIME_INLINE
long int _GL_ATTRIBUTE_PURE
66 get_stat_atime_ns (struct stat
const *st
)
68 # if defined STAT_TIMESPEC
69 return STAT_TIMESPEC (st
, st_atim
).tv_nsec
;
70 # elif defined STAT_TIMESPEC_NS
71 return STAT_TIMESPEC_NS (st
, st_atim
);
77 /* Return the nanosecond component of *ST's status change time. */
78 _GL_STAT_TIME_INLINE
long int _GL_ATTRIBUTE_PURE
79 get_stat_ctime_ns (struct stat
const *st
)
81 # if defined STAT_TIMESPEC
82 return STAT_TIMESPEC (st
, st_ctim
).tv_nsec
;
83 # elif defined STAT_TIMESPEC_NS
84 return STAT_TIMESPEC_NS (st
, st_ctim
);
90 /* Return the nanosecond component of *ST's data modification time. */
91 _GL_STAT_TIME_INLINE
long int _GL_ATTRIBUTE_PURE
92 get_stat_mtime_ns (struct stat
const *st
)
94 # if defined STAT_TIMESPEC
95 return STAT_TIMESPEC (st
, st_mtim
).tv_nsec
;
96 # elif defined STAT_TIMESPEC_NS
97 return STAT_TIMESPEC_NS (st
, st_mtim
);
103 /* Return the nanosecond component of *ST's birth time. */
104 _GL_STAT_TIME_INLINE
long int _GL_ATTRIBUTE_PURE
105 get_stat_birthtime_ns (struct stat
const *st _GL_UNUSED
)
107 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
108 return STAT_TIMESPEC (st
, st_birthtim
).tv_nsec
;
109 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
110 return STAT_TIMESPEC_NS (st
, st_birthtim
);
116 /* Return *ST's access time. */
117 _GL_STAT_TIME_INLINE
struct timespec _GL_ATTRIBUTE_PURE
118 get_stat_atime (struct stat
const *st
)
121 return STAT_TIMESPEC (st
, st_atim
);
124 t
.tv_sec
= st
->st_atime
;
125 t
.tv_nsec
= get_stat_atime_ns (st
);
130 /* Return *ST's status change time. */
131 _GL_STAT_TIME_INLINE
struct timespec _GL_ATTRIBUTE_PURE
132 get_stat_ctime (struct stat
const *st
)
135 return STAT_TIMESPEC (st
, st_ctim
);
138 t
.tv_sec
= st
->st_ctime
;
139 t
.tv_nsec
= get_stat_ctime_ns (st
);
144 /* Return *ST's data modification time. */
145 _GL_STAT_TIME_INLINE
struct timespec _GL_ATTRIBUTE_PURE
146 get_stat_mtime (struct stat
const *st
)
149 return STAT_TIMESPEC (st
, st_mtim
);
152 t
.tv_sec
= st
->st_mtime
;
153 t
.tv_nsec
= get_stat_mtime_ns (st
);
158 /* Return *ST's birth time, if available; otherwise return a value
159 with tv_sec and tv_nsec both equal to -1. */
160 _GL_STAT_TIME_INLINE
struct timespec _GL_ATTRIBUTE_PURE
161 get_stat_birthtime (struct stat
const *st _GL_UNUSED
)
165 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
166 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
167 t
= STAT_TIMESPEC (st
, st_birthtim
);
168 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
169 t
.tv_sec
= st
->st_birthtime
;
170 t
.tv_nsec
= st
->st_birthtimensec
;
171 #elif defined _WIN32 && ! defined __CYGWIN__
172 /* Native Windows platforms (but not Cygwin) put the "file creation
173 time" in st_ctime (!). See
174 <https://msdn.microsoft.com/en-us/library/14h5k7ff(VS.80).aspx>. */
175 # if _GL_WINDOWS_STAT_TIMESPEC
178 t
.tv_sec
= st
->st_ctime
;
182 /* Birth time is not supported. */
187 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
188 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
189 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
190 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
191 using zero. Attempt to work around this problem. Alas, this can
192 report failure even for valid timestamps. Also, NetBSD
193 sometimes returns junk in the birth time fields; work around this
194 bug if it is detected. */
195 if (! (t
.tv_sec
&& 0 <= t
.tv_nsec
&& t
.tv_nsec
< 1000000000))
205 /* If a stat-like function returned RESULT, normalize the timestamps
206 in *ST, in case this platform suffers from the Solaris 11 bug where
207 tv_nsec might be negative. Return the adjusted RESULT, setting
208 errno to EOVERFLOW if normalization overflowed. This function
209 is intended to be private to this .h file. */
210 _GL_STAT_TIME_INLINE
int
211 stat_time_normalize (int result
, struct stat
*st _GL_UNUSED
)
213 #if defined __sun && defined STAT_TIMESPEC
216 long int timespec_hz
= 1000000000;
217 short int const ts_off
[] = { offsetof (struct stat
, st_atim
),
218 offsetof (struct stat
, st_mtim
),
219 offsetof (struct stat
, st_ctim
) };
221 for (i
= 0; i
< sizeof ts_off
/ sizeof *ts_off
; i
++)
223 struct timespec
*ts
= (struct timespec
*) ((char *) st
+ ts_off
[i
]);
224 long int q
= ts
->tv_nsec
/ timespec_hz
;
225 long int r
= ts
->tv_nsec
% timespec_hz
;
232 /* Overflow is possible, as Solaris 11 stat can yield
233 tv_sec == TYPE_MINIMUM (time_t) && tv_nsec == -1000000000.
234 INT_ADD_WRAPV is OK, since time_t is signed on Solaris. */
235 if (INT_ADD_WRAPV (q
, ts
->tv_sec
, &ts
->tv_sec
))
250 _GL_INLINE_HEADER_END