This source file includes following definitions.
- get_stat_atime_ns
- get_stat_ctime_ns
- get_stat_mtime_ns
- get_stat_birthtime_ns
- get_stat_atime
- get_stat_ctime
- get_stat_mtime
- get_stat_birthtime
- stat_time_normalize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #ifndef STAT_TIME_H
21 #define STAT_TIME_H 1
22
23
24
25 #if !_GL_CONFIG_H_INCLUDED
26 #error "Please include config.h first."
27 #endif
28
29 #include <errno.h>
30 #include <stdckdint.h>
31 #include <stddef.h>
32 #include <sys/stat.h>
33 #include <time.h>
34
35 _GL_INLINE_HEADER_BEGIN
36 #ifndef _GL_STAT_TIME_INLINE
37 # define _GL_STAT_TIME_INLINE _GL_INLINE
38 #endif
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45
46
47
48
49
50
51
52 #if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
53 # if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
54 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
55 # else
56 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
57 # endif
58 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
59 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
60 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
61 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
62 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
63 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
64 #endif
65
66
67 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
68 get_stat_atime_ns (struct stat const *st)
69 {
70 # if defined STAT_TIMESPEC
71 return STAT_TIMESPEC (st, st_atim).tv_nsec;
72 # elif defined STAT_TIMESPEC_NS
73 return STAT_TIMESPEC_NS (st, st_atim);
74 # else
75 return 0;
76 # endif
77 }
78
79
80 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
81 get_stat_ctime_ns (struct stat const *st)
82 {
83 # if defined STAT_TIMESPEC
84 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
85 # elif defined STAT_TIMESPEC_NS
86 return STAT_TIMESPEC_NS (st, st_ctim);
87 # else
88 return 0;
89 # endif
90 }
91
92
93 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
94 get_stat_mtime_ns (struct stat const *st)
95 {
96 # if defined STAT_TIMESPEC
97 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
98 # elif defined STAT_TIMESPEC_NS
99 return STAT_TIMESPEC_NS (st, st_mtim);
100 # else
101 return 0;
102 # endif
103 }
104
105
106 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
107 get_stat_birthtime_ns (_GL_UNUSED struct stat const *st)
108 {
109 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
110 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
111 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
112 return STAT_TIMESPEC_NS (st, st_birthtim);
113 # else
114 return 0;
115 # endif
116 }
117
118
119 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
120 get_stat_atime (struct stat const *st)
121 {
122 #ifdef STAT_TIMESPEC
123 return STAT_TIMESPEC (st, st_atim);
124 #else
125 return (struct timespec) { .tv_sec = st->st_atime,
126 .tv_nsec = get_stat_atime_ns (st) };
127 #endif
128 }
129
130
131 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
132 get_stat_ctime (struct stat const *st)
133 {
134 #ifdef STAT_TIMESPEC
135 return STAT_TIMESPEC (st, st_ctim);
136 #else
137 return (struct timespec) { .tv_sec = st->st_ctime,
138 .tv_nsec = get_stat_ctime_ns (st) };
139 #endif
140 }
141
142
143 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
144 get_stat_mtime (struct stat const *st)
145 {
146 #ifdef STAT_TIMESPEC
147 return STAT_TIMESPEC (st, st_mtim);
148 #else
149 return (struct timespec) { .tv_sec = st->st_mtime,
150 .tv_nsec = get_stat_mtime_ns (st) };
151 #endif
152 }
153
154
155
156 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
157 get_stat_birthtime (_GL_UNUSED struct stat const *st)
158 {
159 struct timespec t;
160
161 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
162 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
163 t = STAT_TIMESPEC (st, st_birthtim);
164 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
165 t = (struct timespec) { .tv_sec = st->st_birthtime,
166 .tv_nsec = st->st_birthtimensec };
167 #elif defined _WIN32 && ! defined __CYGWIN__
168
169
170
171 # if _GL_WINDOWS_STAT_TIMESPEC
172 t = st->st_ctim;
173 # else
174 t = (struct timespec) { .tv_sec = st->st_ctime };
175 # endif
176 #else
177
178 t = (struct timespec) { .tv_sec = -1, .tv_nsec = -1 };
179 #endif
180
181 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
182 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
183 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
184
185
186
187
188
189 if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
190 t = (struct timespec) { .tv_sec = -1, .tv_nsec = -1 };
191 #endif
192
193 return t;
194 }
195
196
197
198
199
200
201 _GL_STAT_TIME_INLINE int
202 stat_time_normalize (int result, _GL_UNUSED struct stat *st)
203 {
204 #if defined __sun && defined STAT_TIMESPEC
205 if (result == 0)
206 {
207 long int timespec_hz = 1000000000;
208 short int const ts_off[] = { offsetof (struct stat, st_atim),
209 offsetof (struct stat, st_mtim),
210 offsetof (struct stat, st_ctim) };
211 int i;
212 for (i = 0; i < sizeof ts_off / sizeof *ts_off; i++)
213 {
214 struct timespec *ts = (struct timespec *) ((char *) st + ts_off[i]);
215 long int q = ts->tv_nsec / timespec_hz;
216 long int r = ts->tv_nsec % timespec_hz;
217 if (r < 0)
218 {
219 r += timespec_hz;
220 q--;
221 }
222 ts->tv_nsec = r;
223
224
225 if (ckd_add (&ts->tv_sec, q, ts->tv_sec))
226 {
227 errno = EOVERFLOW;
228 return -1;
229 }
230 }
231 }
232 #endif
233 return result;
234 }
235
236 #ifdef __cplusplus
237 }
238 #endif
239
240 _GL_INLINE_HEADER_END
241
242 #endif