stat-time: use extern-inline
[gnulib.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
26 _GL_INLINE_HEADER_BEGIN
27
28 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
29    struct timespec, if available.  If not, then STAT_TIMESPEC_NS (ST,
30    ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
31    if available.  ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
32    for access, status change, data modification, or birth (creation)
33    time respectively.
34
35    These macros are private to stat-time.h.  */
36 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
37 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
38 #  define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
39 # else
40 #  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
41 # endif
42 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
43 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
44 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
45 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
46 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
47 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
48 #endif
49
50 #ifndef _GL_STAT_TIME_INLINE
51 # define _GL_STAT_TIME_INLINE _GL_INLINE
52 #endif
53
54 /* Return the nanosecond component of *ST's access time.  */
55 _GL_STAT_TIME_INLINE long int
56 get_stat_atime_ns (struct stat const *st)
57 {
58 # if defined STAT_TIMESPEC
59   return STAT_TIMESPEC (st, st_atim).tv_nsec;
60 # elif defined STAT_TIMESPEC_NS
61   return STAT_TIMESPEC_NS (st, st_atim);
62 # else
63   return 0;
64 # endif
65 }
66
67 /* Return the nanosecond component of *ST's status change time.  */
68 _GL_STAT_TIME_INLINE long int
69 get_stat_ctime_ns (struct stat const *st)
70 {
71 # if defined STAT_TIMESPEC
72   return STAT_TIMESPEC (st, st_ctim).tv_nsec;
73 # elif defined STAT_TIMESPEC_NS
74   return STAT_TIMESPEC_NS (st, st_ctim);
75 # else
76   return 0;
77 # endif
78 }
79
80 /* Return the nanosecond component of *ST's data modification time.  */
81 _GL_STAT_TIME_INLINE long int
82 get_stat_mtime_ns (struct stat const *st)
83 {
84 # if defined STAT_TIMESPEC
85   return STAT_TIMESPEC (st, st_mtim).tv_nsec;
86 # elif defined STAT_TIMESPEC_NS
87   return STAT_TIMESPEC_NS (st, st_mtim);
88 # else
89   return 0;
90 # endif
91 }
92
93 /* Return the nanosecond component of *ST's birth time.  */
94 _GL_STAT_TIME_INLINE long int
95 get_stat_birthtime_ns (struct stat const *st)
96 {
97 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
98   return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
99 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
100   return STAT_TIMESPEC_NS (st, st_birthtim);
101 # else
102   /* Avoid a "parameter unused" warning.  */
103   (void) st;
104   return 0;
105 # endif
106 }
107
108 /* Return *ST's access time.  */
109 _GL_STAT_TIME_INLINE struct timespec
110 get_stat_atime (struct stat const *st)
111 {
112 #ifdef STAT_TIMESPEC
113   return STAT_TIMESPEC (st, st_atim);
114 #else
115   struct timespec t;
116   t.tv_sec = st->st_atime;
117   t.tv_nsec = get_stat_atime_ns (st);
118   return t;
119 #endif
120 }
121
122 /* Return *ST's status change time.  */
123 _GL_STAT_TIME_INLINE struct timespec
124 get_stat_ctime (struct stat const *st)
125 {
126 #ifdef STAT_TIMESPEC
127   return STAT_TIMESPEC (st, st_ctim);
128 #else
129   struct timespec t;
130   t.tv_sec = st->st_ctime;
131   t.tv_nsec = get_stat_ctime_ns (st);
132   return t;
133 #endif
134 }
135
136 /* Return *ST's data modification time.  */
137 _GL_STAT_TIME_INLINE struct timespec
138 get_stat_mtime (struct stat const *st)
139 {
140 #ifdef STAT_TIMESPEC
141   return STAT_TIMESPEC (st, st_mtim);
142 #else
143   struct timespec t;
144   t.tv_sec = st->st_mtime;
145   t.tv_nsec = get_stat_mtime_ns (st);
146   return t;
147 #endif
148 }
149
150 /* Return *ST's birth time, if available; otherwise return a value
151    with tv_sec and tv_nsec both equal to -1.  */
152 _GL_STAT_TIME_INLINE struct timespec
153 get_stat_birthtime (struct stat const *st)
154 {
155   struct timespec t;
156
157 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
158      || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
159   t = STAT_TIMESPEC (st, st_birthtim);
160 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
161   t.tv_sec = st->st_birthtime;
162   t.tv_nsec = st->st_birthtimensec;
163 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
164   /* Native Windows platforms (but not Cygwin) put the "file creation
165      time" in st_ctime (!).  See
166      <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>.  */
167   t.tv_sec = st->st_ctime;
168   t.tv_nsec = 0;
169 #else
170   /* Birth time is not supported.  */
171   t.tv_sec = -1;
172   t.tv_nsec = -1;
173   /* Avoid a "parameter unused" warning.  */
174   (void) st;
175 #endif
176
177 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
178      || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
179      || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
180   /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
181      using zero.  Attempt to work around this problem.  Alas, this can
182      report failure even for valid time stamps.  Also, NetBSD
183      sometimes returns junk in the birth time fields; work around this
184      bug if it is detected.  */
185   if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
186     {
187       t.tv_sec = -1;
188       t.tv_nsec = -1;
189     }
190 #endif
191
192   return t;
193 }
194
195 _GL_INLINE_HEADER_END
196
197 #endif