Import upstream version 1.28
[debian/tar] / gnu / stat-time.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* stat-related time functions.
4
5    Copyright (C) 2005, 2007, 2009-2014 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* Written by Paul Eggert.  */
21
22 #ifndef STAT_TIME_H
23 #define STAT_TIME_H 1
24
25 #include <sys/stat.h>
26 #include <time.h>
27
28 #ifndef _GL_INLINE_HEADER_BEGIN
29  #error "Please include config.h first."
30 #endif
31 _GL_INLINE_HEADER_BEGIN
32 #ifndef _GL_STAT_TIME_INLINE
33 # define _GL_STAT_TIME_INLINE _GL_INLINE
34 #endif
35
36 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
37    struct timespec, if available.  If not, then STAT_TIMESPEC_NS (ST,
38    ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
39    if available.  ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
40    for access, status change, data modification, or birth (creation)
41    time respectively.
42
43    These macros are private to stat-time.h.  */
44 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
45 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
46 #  define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
47 # else
48 #  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
49 # endif
50 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
51 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
52 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
53 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
54 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
55 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
56 #endif
57
58 /* Return the nanosecond component of *ST's access time.  */
59 _GL_STAT_TIME_INLINE long int
60 get_stat_atime_ns (struct stat const *st)
61 {
62 # if defined STAT_TIMESPEC
63   return STAT_TIMESPEC (st, st_atim).tv_nsec;
64 # elif defined STAT_TIMESPEC_NS
65   return STAT_TIMESPEC_NS (st, st_atim);
66 # else
67   return 0;
68 # endif
69 }
70
71 /* Return the nanosecond component of *ST's status change time.  */
72 _GL_STAT_TIME_INLINE long int
73 get_stat_ctime_ns (struct stat const *st)
74 {
75 # if defined STAT_TIMESPEC
76   return STAT_TIMESPEC (st, st_ctim).tv_nsec;
77 # elif defined STAT_TIMESPEC_NS
78   return STAT_TIMESPEC_NS (st, st_ctim);
79 # else
80   return 0;
81 # endif
82 }
83
84 /* Return the nanosecond component of *ST's data modification time.  */
85 _GL_STAT_TIME_INLINE long int
86 get_stat_mtime_ns (struct stat const *st)
87 {
88 # if defined STAT_TIMESPEC
89   return STAT_TIMESPEC (st, st_mtim).tv_nsec;
90 # elif defined STAT_TIMESPEC_NS
91   return STAT_TIMESPEC_NS (st, st_mtim);
92 # else
93   return 0;
94 # endif
95 }
96
97 /* Return the nanosecond component of *ST's birth time.  */
98 _GL_STAT_TIME_INLINE long int
99 get_stat_birthtime_ns (struct stat const *st)
100 {
101 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
102   return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
103 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
104   return STAT_TIMESPEC_NS (st, st_birthtim);
105 # else
106   /* Avoid a "parameter unused" warning.  */
107   (void) st;
108   return 0;
109 # endif
110 }
111
112 /* Return *ST's access time.  */
113 _GL_STAT_TIME_INLINE struct timespec
114 get_stat_atime (struct stat const *st)
115 {
116 #ifdef STAT_TIMESPEC
117   return STAT_TIMESPEC (st, st_atim);
118 #else
119   struct timespec t;
120   t.tv_sec = st->st_atime;
121   t.tv_nsec = get_stat_atime_ns (st);
122   return t;
123 #endif
124 }
125
126 /* Return *ST's status change time.  */
127 _GL_STAT_TIME_INLINE struct timespec
128 get_stat_ctime (struct stat const *st)
129 {
130 #ifdef STAT_TIMESPEC
131   return STAT_TIMESPEC (st, st_ctim);
132 #else
133   struct timespec t;
134   t.tv_sec = st->st_ctime;
135   t.tv_nsec = get_stat_ctime_ns (st);
136   return t;
137 #endif
138 }
139
140 /* Return *ST's data modification time.  */
141 _GL_STAT_TIME_INLINE struct timespec
142 get_stat_mtime (struct stat const *st)
143 {
144 #ifdef STAT_TIMESPEC
145   return STAT_TIMESPEC (st, st_mtim);
146 #else
147   struct timespec t;
148   t.tv_sec = st->st_mtime;
149   t.tv_nsec = get_stat_mtime_ns (st);
150   return t;
151 #endif
152 }
153
154 /* Return *ST's birth time, if available; otherwise return a value
155    with tv_sec and tv_nsec both equal to -1.  */
156 _GL_STAT_TIME_INLINE struct timespec
157 get_stat_birthtime (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.tv_sec = st->st_birthtime;
166   t.tv_nsec = st->st_birthtimensec;
167 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
168   /* Native Windows platforms (but not Cygwin) put the "file creation
169      time" in st_ctime (!).  See
170      <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>.  */
171   t.tv_sec = st->st_ctime;
172   t.tv_nsec = 0;
173 #else
174   /* Birth time is not supported.  */
175   t.tv_sec = -1;
176   t.tv_nsec = -1;
177   /* Avoid a "parameter unused" warning.  */
178   (void) st;
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   /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
185      using zero.  Attempt to work around this problem.  Alas, this can
186      report failure even for valid time stamps.  Also, NetBSD
187      sometimes returns junk in the birth time fields; work around this
188      bug if it is detected.  */
189   if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
190     {
191       t.tv_sec = -1;
192       t.tv_nsec = -1;
193     }
194 #endif
195
196   return t;
197 }
198
199 _GL_INLINE_HEADER_END
200
201 #endif