init version.
[bush.git] / include / stat-time.h
blobe04cc619ee2333fc873ee79fae573cda86575a85
1 /* stat-related time functions.
3 Copyright (C) 2005, 2007, 2009-2012 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. */
20 #ifndef STAT_TIME_H
21 #define STAT_TIME_H 1
23 #include <sys/stat.h>
25 #if defined (TIME_H_DEFINES_STRUCT_TIMESPEC)
26 # include <time.h>
27 #elif defined (SYS_TIME_H_DEFINES_STRUCT_TIMESPEC)
28 # include <sys/time.h>
29 #elif defined (PTHREAD_H_DEFINES_STRUCT_TIMESPEC)
30 # include <pthread.h>
31 #endif
33 #ifndef HAVE_STRUCT_TIMESPEC
34 struct timespec
36 time_t tv_sec;
37 long int tv_nsec;
39 #endif
41 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
42 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
43 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
44 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
45 for access, status change, data modification, or birth (creation)
46 time respectively.
48 These macros are private to stat-time.h. */
49 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
50 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
51 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
52 # else
53 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
54 # endif
55 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
56 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
57 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
58 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
59 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
60 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
61 #endif
63 /* Return the nanosecond component of *ST's access time. */
64 static inline long int
65 get_stat_atime_ns (struct stat const *st)
67 # if defined STAT_TIMESPEC
68 return STAT_TIMESPEC (st, st_atim).tv_nsec;
69 # elif defined STAT_TIMESPEC_NS
70 return STAT_TIMESPEC_NS (st, st_atim);
71 # else
72 return 0;
73 # endif
76 /* Return the nanosecond component of *ST's status change time. */
77 static inline long int
78 get_stat_ctime_ns (struct stat const *st)
80 # if defined STAT_TIMESPEC
81 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
82 # elif defined STAT_TIMESPEC_NS
83 return STAT_TIMESPEC_NS (st, st_ctim);
84 # else
85 return 0;
86 # endif
89 /* Return the nanosecond component of *ST's data modification time. */
90 static inline long int
91 get_stat_mtime_ns (struct stat const *st)
93 # if defined STAT_TIMESPEC
94 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
95 # elif defined STAT_TIMESPEC_NS
96 return STAT_TIMESPEC_NS (st, st_mtim);
97 # else
98 return 0;
99 # endif
102 /* Return the nanosecond component of *ST's birth time. */
103 static inline long int
104 get_stat_birthtime_ns (struct stat const *st)
106 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
107 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
108 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
109 return STAT_TIMESPEC_NS (st, st_birthtim);
110 # else
111 /* Avoid a "parameter unused" warning. */
112 (void) st;
113 return 0;
114 # endif
117 /* Return *ST's access time. */
118 static inline struct timespec
119 get_stat_atime (struct stat const *st)
121 #ifdef STAT_TIMESPEC
122 return STAT_TIMESPEC (st, st_atim);
123 #else
124 struct timespec t;
125 t.tv_sec = st->st_atime;
126 t.tv_nsec = get_stat_atime_ns (st);
127 return t;
128 #endif
131 /* Return *ST's status change time. */
132 static inline struct timespec
133 get_stat_ctime (struct stat const *st)
135 #ifdef STAT_TIMESPEC
136 return STAT_TIMESPEC (st, st_ctim);
137 #else
138 struct timespec t;
139 t.tv_sec = st->st_ctime;
140 t.tv_nsec = get_stat_ctime_ns (st);
141 return t;
142 #endif
145 /* Return *ST's data modification time. */
146 static inline struct timespec
147 get_stat_mtime (struct stat const *st)
149 #ifdef STAT_TIMESPEC
150 return STAT_TIMESPEC (st, st_mtim);
151 #else
152 struct timespec t;
153 t.tv_sec = st->st_mtime;
154 t.tv_nsec = get_stat_mtime_ns (st);
155 return t;
156 #endif
159 static inline int
160 timespec_cmp (struct timespec a, struct timespec b)
162 return (a.tv_sec < b.tv_sec
163 ? -1
164 : (a.tv_sec > b.tv_sec
166 : (int) (a.tv_nsec - b.tv_nsec)));
169 /* Return *ST's birth time, if available; otherwise return a value
170 with tv_sec and tv_nsec both equal to -1. */
171 static inline struct timespec
172 get_stat_birthtime (struct stat const *st)
174 struct timespec t;
176 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
177 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
178 t = STAT_TIMESPEC (st, st_birthtim);
179 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
180 t.tv_sec = st->st_birthtime;
181 t.tv_nsec = st->st_birthtimensec;
182 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
183 /* Native Windows platforms (but not Cygwin) put the "file creation
184 time" in st_ctime (!). See
185 <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
186 t.tv_sec = st->st_ctime;
187 t.tv_nsec = 0;
188 #else
189 /* Birth time is not supported. */
190 t.tv_sec = -1;
191 t.tv_nsec = -1;
192 /* Avoid a "parameter unused" warning. */
193 (void) st;
194 #endif
196 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
197 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
198 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
199 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
200 using zero. Attempt to work around this problem. Alas, this can
201 report failure even for valid time stamps. Also, NetBSD
202 sometimes returns junk in the birth time fields; work around this
203 bug if it is detected. */
204 if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
206 t.tv_sec = -1;
207 t.tv_nsec = -1;
209 #endif
211 return t;
214 #endif