Fix the MS-Windows build broken by 2012-06-22T21:17:42Z!eggert@cs.ucla.edu.
[bpt/emacs.git] / lib / stat-time.h
1 /* stat-related time functions.
2
3 Copyright (C) 2005, 2007, 2009-2012 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 /* Written by Paul Eggert. */
19
20 #ifndef STAT_TIME_H
21 #define STAT_TIME_H 1
22
23 #include <sys/stat.h>
24 #include <time.h>
25 #include <sys/time.h>
26
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)
32 time respectively.
33
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)
38 # else
39 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
40 # endif
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)
47 #endif
48
49 /* Return the nanosecond component of *ST's access time. */
50 static inline long int
51 get_stat_atime_ns (struct stat const *st)
52 {
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);
57 # else
58 return 0;
59 # endif
60 }
61
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)
65 {
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);
70 # else
71 return 0;
72 # endif
73 }
74
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)
78 {
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);
83 # else
84 return 0;
85 # endif
86 }
87
88 /* Return the nanosecond component of *ST's birth time. */
89 static inline long int
90 get_stat_birthtime_ns (struct stat const *st)
91 {
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);
96 # else
97 /* Avoid a "parameter unused" warning. */
98 (void) st;
99 return 0;
100 # endif
101 }
102
103 /* Return *ST's access time. */
104 static inline struct timespec
105 get_stat_atime (struct stat const *st)
106 {
107 #ifdef STAT_TIMESPEC
108 return STAT_TIMESPEC (st, st_atim);
109 #else
110 struct timespec t;
111 t.tv_sec = st->st_atime;
112 t.tv_nsec = get_stat_atime_ns (st);
113 return t;
114 #endif
115 }
116
117 /* Return *ST's status change time. */
118 static inline struct timespec
119 get_stat_ctime (struct stat const *st)
120 {
121 #ifdef STAT_TIMESPEC
122 return STAT_TIMESPEC (st, st_ctim);
123 #else
124 struct timespec t;
125 t.tv_sec = st->st_ctime;
126 t.tv_nsec = get_stat_ctime_ns (st);
127 return t;
128 #endif
129 }
130
131 /* Return *ST's data modification time. */
132 static inline struct timespec
133 get_stat_mtime (struct stat const *st)
134 {
135 #ifdef STAT_TIMESPEC
136 return STAT_TIMESPEC (st, st_mtim);
137 #else
138 struct timespec t;
139 t.tv_sec = st->st_mtime;
140 t.tv_nsec = get_stat_mtime_ns (st);
141 return t;
142 #endif
143 }
144
145 /* Return *ST's birth time, if available; otherwise return a value
146 with tv_sec and tv_nsec both equal to -1. */
147 static inline struct timespec
148 get_stat_birthtime (struct stat const *st)
149 {
150 struct timespec t;
151
152 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
153 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
154 t = STAT_TIMESPEC (st, st_birthtim);
155 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
156 t.tv_sec = st->st_birthtime;
157 t.tv_nsec = st->st_birthtimensec;
158 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
159 /* Native Windows platforms (but not Cygwin) put the "file creation
160 time" in st_ctime (!). See
161 <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
162 t.tv_sec = st->st_ctime;
163 t.tv_nsec = 0;
164 #else
165 /* Birth time is not supported. */
166 t.tv_sec = -1;
167 t.tv_nsec = -1;
168 /* Avoid a "parameter unused" warning. */
169 (void) st;
170 #endif
171
172 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
173 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
174 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
175 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
176 using zero. Attempt to work around this problem. Alas, this can
177 report failure even for valid time stamps. Also, NetBSD
178 sometimes returns junk in the birth time fields; work around this
179 bug if it is detected. */
180 if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
181 {
182 t.tv_sec = -1;
183 t.tv_nsec = -1;
184 }
185 #endif
186
187 return t;
188 }
189
190 #endif