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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert. */
27 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
28 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
29 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
30 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
31 for access, status change, data modification, or birth (creation)
34 These macros are private to stat-time.h. */
35 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
36 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
37 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
39 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
41 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
42 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
43 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
44 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
45 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
46 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
49 /* Return the nanosecond component of *ST's access time. */
50 static inline long int
51 get_stat_atime_ns (struct stat const *st)
53 # if defined STAT_TIMESPEC
54 return STAT_TIMESPEC (st, st_atim).tv_nsec;
55 # elif defined STAT_TIMESPEC_NS
56 return STAT_TIMESPEC_NS (st, st_atim);
62 /* Return the nanosecond component of *ST's status change time. */
63 static inline long int
64 get_stat_ctime_ns (struct stat const *st)
66 # if defined STAT_TIMESPEC
67 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
68 # elif defined STAT_TIMESPEC_NS
69 return STAT_TIMESPEC_NS (st, st_ctim);
75 /* Return the nanosecond component of *ST's data modification time. */
76 static inline long int
77 get_stat_mtime_ns (struct stat const *st)
79 # if defined STAT_TIMESPEC
80 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
81 # elif defined STAT_TIMESPEC_NS
82 return STAT_TIMESPEC_NS (st, st_mtim);
88 /* Return the nanosecond component of *ST's birth time. */
89 static inline long int
90 get_stat_birthtime_ns (struct stat const *st)
92 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
93 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
94 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
95 return STAT_TIMESPEC_NS (st, st_birthtim);
101 /* Return *ST's access time. */
102 static inline struct timespec
103 get_stat_atime (struct stat const *st)
106 return STAT_TIMESPEC (st, st_atim);
109 t.tv_sec = st->st_atime;
110 t.tv_nsec = get_stat_atime_ns (st);
115 /* Return *ST's status change time. */
116 static inline struct timespec
117 get_stat_ctime (struct stat const *st)
120 return STAT_TIMESPEC (st, st_ctim);
123 t.tv_sec = st->st_ctime;
124 t.tv_nsec = get_stat_ctime_ns (st);
129 /* Return *ST's data modification time. */
130 static inline struct timespec
131 get_stat_mtime (struct stat const *st)
134 return STAT_TIMESPEC (st, st_mtim);
137 t.tv_sec = st->st_mtime;
138 t.tv_nsec = get_stat_mtime_ns (st);
143 /* Return *ST's birth time, if available; otherwise return a value
144 with negative tv_nsec. */
145 static inline struct timespec
146 get_stat_birthtime (struct stat const *st)
150 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
151 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
152 t = STAT_TIMESPEC (st, st_birthtim);
153 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
154 t.tv_sec = st->st_birthtime;
155 t.tv_nsec = st->st_birthtimensec;
156 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
157 /* Woe32 native platforms (but not Cygwin) put the "file creation
158 time" in st_ctime (!). See
159 <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
160 t.tv_sec = st->st_ctime;
163 /* Birth time is not supported. Set tv_sec to avoid undefined behavior. */
168 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
169 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
170 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
171 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
172 using zero. Attempt to work around this problem. Alas, this can
173 report failure even for valid time stamps. Also, NetBSD
174 sometimes returns junk in the birth time fields; work around this
175 bug if it it is detected. There's no need to detect negative
176 tv_nsec junk as negative tv_nsec already indicates an error. */
177 if (t.tv_sec == 0 || 1000000000 <= t.tv_nsec)