* src/font.c (Ffont_info): Doc fix. (Bug#29682)
[emacs.git] / lib / stat-time.h
blob1cf821992ed1d7d27f76255779557f5d33954942
1 /* stat-related time functions.
3 Copyright (C) 2005, 2007, 2009-2017 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 <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #ifndef STAT_TIME_H
21 #define STAT_TIME_H 1
23 #include "intprops.h"
25 #include <errno.h>
26 #include <stddef.h>
27 #include <sys/stat.h>
28 #include <time.h>
30 #ifndef _GL_INLINE_HEADER_BEGIN
31 #error "Please include config.h first."
32 #endif
33 _GL_INLINE_HEADER_BEGIN
34 #ifndef _GL_STAT_TIME_INLINE
35 # define _GL_STAT_TIME_INLINE _GL_INLINE
36 #endif
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
43 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
44 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
45 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
46 for access, status change, data modification, or birth (creation)
47 time respectively.
49 These macros are private to stat-time.h. */
50 #if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
51 # if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
52 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
53 # else
54 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
55 # endif
56 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
57 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
58 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
59 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
60 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
61 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
62 #endif
64 /* Return the nanosecond component of *ST's access time. */
65 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
66 get_stat_atime_ns (struct stat const *st)
68 # if defined STAT_TIMESPEC
69 return STAT_TIMESPEC (st, st_atim).tv_nsec;
70 # elif defined STAT_TIMESPEC_NS
71 return STAT_TIMESPEC_NS (st, st_atim);
72 # else
73 return 0;
74 # endif
77 /* Return the nanosecond component of *ST's status change time. */
78 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
79 get_stat_ctime_ns (struct stat const *st)
81 # if defined STAT_TIMESPEC
82 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
83 # elif defined STAT_TIMESPEC_NS
84 return STAT_TIMESPEC_NS (st, st_ctim);
85 # else
86 return 0;
87 # endif
90 /* Return the nanosecond component of *ST's data modification time. */
91 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
92 get_stat_mtime_ns (struct stat const *st)
94 # if defined STAT_TIMESPEC
95 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
96 # elif defined STAT_TIMESPEC_NS
97 return STAT_TIMESPEC_NS (st, st_mtim);
98 # else
99 return 0;
100 # endif
103 /* Return the nanosecond component of *ST's birth time. */
104 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
105 get_stat_birthtime_ns (struct stat const *st)
107 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
108 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
109 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
110 return STAT_TIMESPEC_NS (st, st_birthtim);
111 # else
112 /* Avoid a "parameter unused" warning. */
113 (void) st;
114 return 0;
115 # endif
118 /* Return *ST's access time. */
119 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
120 get_stat_atime (struct stat const *st)
122 #ifdef STAT_TIMESPEC
123 return STAT_TIMESPEC (st, st_atim);
124 #else
125 struct timespec t;
126 t.tv_sec = st->st_atime;
127 t.tv_nsec = get_stat_atime_ns (st);
128 return t;
129 #endif
132 /* Return *ST's status change time. */
133 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
134 get_stat_ctime (struct stat const *st)
136 #ifdef STAT_TIMESPEC
137 return STAT_TIMESPEC (st, st_ctim);
138 #else
139 struct timespec t;
140 t.tv_sec = st->st_ctime;
141 t.tv_nsec = get_stat_ctime_ns (st);
142 return t;
143 #endif
146 /* Return *ST's data modification time. */
147 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
148 get_stat_mtime (struct stat const *st)
150 #ifdef STAT_TIMESPEC
151 return STAT_TIMESPEC (st, st_mtim);
152 #else
153 struct timespec t;
154 t.tv_sec = st->st_mtime;
155 t.tv_nsec = get_stat_mtime_ns (st);
156 return t;
157 #endif
160 /* Return *ST's birth time, if available; otherwise return a value
161 with tv_sec and tv_nsec both equal to -1. */
162 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
163 get_stat_birthtime (struct stat const *st)
165 struct timespec t;
167 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
168 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
169 t = STAT_TIMESPEC (st, st_birthtim);
170 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
171 t.tv_sec = st->st_birthtime;
172 t.tv_nsec = st->st_birthtimensec;
173 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
174 /* Native Windows platforms (but not Cygwin) put the "file creation
175 time" in st_ctime (!). See
176 <https://msdn.microsoft.com/en-us/library/14h5k7ff(VS.80).aspx>. */
177 # if _GL_WINDOWS_STAT_TIMESPEC
178 t = st->st_ctim;
179 # else
180 t.tv_sec = st->st_ctime;
181 t.tv_nsec = 0;
182 # endif
183 #else
184 /* Birth time is not supported. */
185 t.tv_sec = -1;
186 t.tv_nsec = -1;
187 /* Avoid a "parameter unused" warning. */
188 (void) st;
189 #endif
191 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
192 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
193 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
194 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
195 using zero. Attempt to work around this problem. Alas, this can
196 report failure even for valid timestamps. Also, NetBSD
197 sometimes returns junk in the birth time fields; work around this
198 bug if it is detected. */
199 if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
201 t.tv_sec = -1;
202 t.tv_nsec = -1;
204 #endif
206 return t;
209 /* If a stat-like function returned RESULT, normalize the timestamps
210 in *ST, in case this platform suffers from the Solaris 11 bug where
211 tv_nsec might be negative. Return the adjusted RESULT, setting
212 errno to EOVERFLOW if normalization overflowed. This function
213 is intended to be private to this .h file. */
214 _GL_STAT_TIME_INLINE int
215 stat_time_normalize (int result, struct stat *st)
217 #if defined __sun && defined STAT_TIMESPEC
218 if (result == 0)
220 long int timespec_resolution = 1000000000;
221 short int const ts_off[] = { offsetof (struct stat, st_atim),
222 offsetof (struct stat, st_mtim),
223 offsetof (struct stat, st_ctim) };
224 int i;
225 for (i = 0; i < sizeof ts_off / sizeof *ts_off; i++)
227 struct timespec *ts = (struct timespec *) ((char *) st + ts_off[i]);
228 long int q = ts->tv_nsec / timespec_resolution;
229 long int r = ts->tv_nsec % timespec_resolution;
230 if (r < 0)
232 r += timespec_resolution;
233 q--;
235 ts->tv_nsec = r;
236 /* Overflow is possible, as Solaris 11 stat can yield
237 tv_sec == TYPE_MINIMUM (time_t) && tv_nsec == -1000000000.
238 INT_ADD_WRAPV is OK, since time_t is signed on Solaris. */
239 if (INT_ADD_WRAPV (q, ts->tv_sec, &ts->tv_sec))
241 errno = EOVERFLOW;
242 return -1;
246 #endif
247 return result;
250 #ifdef __cplusplus
252 #endif
254 _GL_INLINE_HEADER_END
256 #endif