1 /* stat-related time functions.
3 Copyright (C) 2005, 2007 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 <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
26 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
27 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
28 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
29 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
30 for access, status change, data modification, or birth (creation)
33 These macros are private to stat-time.h. */
34 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
35 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
36 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
38 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
40 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
41 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
42 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
43 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
44 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
45 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
48 /* Return the nanosecond component of *ST's access time. */
49 static inline long int
50 get_stat_atime_ns (struct stat const *st)
52 # if defined STAT_TIMESPEC
53 return STAT_TIMESPEC (st, st_atim).tv_nsec;
54 # elif defined STAT_TIMESPEC_NS
55 return STAT_TIMESPEC_NS (st, st_atim);
61 /* Return the nanosecond component of *ST's status change time. */
62 static inline long int
63 get_stat_ctime_ns (struct stat const *st)
65 # if defined STAT_TIMESPEC
66 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
67 # elif defined STAT_TIMESPEC_NS
68 return STAT_TIMESPEC_NS (st, st_ctim);
74 /* Return the nanosecond component of *ST's data modification time. */
75 static inline long int
76 get_stat_mtime_ns (struct stat const *st)
78 # if defined STAT_TIMESPEC
79 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
80 # elif defined STAT_TIMESPEC_NS
81 return STAT_TIMESPEC_NS (st, st_mtim);
87 /* Return the nanosecond component of *ST's birth time. */
88 static inline long int
89 get_stat_birthtime_ns (struct stat const *st)
91 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
92 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
93 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
94 return STAT_TIMESPEC_NS (st, st_birthtim);
100 /* Return *ST's access time. */
101 static inline struct timespec
102 get_stat_atime (struct stat const *st)
105 return STAT_TIMESPEC (st, st_atim);
108 t.tv_sec = st->st_atime;
109 t.tv_nsec = get_stat_atime_ns (st);
114 /* Return *ST's status change time. */
115 static inline struct timespec
116 get_stat_ctime (struct stat const *st)
119 return STAT_TIMESPEC (st, st_ctim);
122 t.tv_sec = st->st_ctime;
123 t.tv_nsec = get_stat_ctime_ns (st);
128 /* Return *ST's data modification time. */
129 static inline struct timespec
130 get_stat_mtime (struct stat const *st)
133 return STAT_TIMESPEC (st, st_mtim);
136 t.tv_sec = st->st_mtime;
137 t.tv_nsec = get_stat_mtime_ns (st);
142 /* Return *ST's birth time, if available; otherwise return a value
143 with negative tv_nsec. */
144 static inline struct timespec
145 get_stat_birthtime (struct stat const *st)
149 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
150 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
151 t = STAT_TIMESPEC (st, st_birthtim);
152 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
153 t.tv_sec = st->st_birthtime;
154 t.tv_nsec = st->st_birthtimensec;
155 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
156 /* Woe32 native platforms (but not Cygwin) put the "file creation
157 time" in st_ctime (!). See
158 <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
159 t.tv_sec = st->st_ctime;
162 /* Birth time is not supported. Set tv_sec to avoid undefined behavior. */
167 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
168 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
169 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
170 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
171 using zero. Attempt to work around this problem. Alas, this can
172 report failure even for valid time stamps. Also, NetBSD
173 sometimes returns junk in the birth time fields; work around this
174 bug if it it is detected. There's no need to detect negative
175 tv_nsec junk as negative tv_nsec already indicates an error. */
176 if (t.tv_sec == 0 || 1000000000 <= t.tv_nsec)