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