]> git.cworth.org Git - tar/blob - gnu/stat-time.h
upstream: Fix extraction of device nodes.
[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-2010 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 /* 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 /* Return the nanosecond component of *ST's access time.  */
51 static inline long int
52 get_stat_atime_ns (struct stat const *st)
53 {
54 # if defined STAT_TIMESPEC
55   return STAT_TIMESPEC (st, st_atim).tv_nsec;
56 # elif defined STAT_TIMESPEC_NS
57   return STAT_TIMESPEC_NS (st, st_atim);
58 # else
59   return 0;
60 # endif
61 }
62
63 /* Return the nanosecond component of *ST's status change time.  */
64 static inline long int
65 get_stat_ctime_ns (struct stat const *st)
66 {
67 # if defined STAT_TIMESPEC
68   return STAT_TIMESPEC (st, st_ctim).tv_nsec;
69 # elif defined STAT_TIMESPEC_NS
70   return STAT_TIMESPEC_NS (st, st_ctim);
71 # else
72   return 0;
73 # endif
74 }
75
76 /* Return the nanosecond component of *ST's data modification time.  */
77 static inline long int
78 get_stat_mtime_ns (struct stat const *st)
79 {
80 # if defined STAT_TIMESPEC
81   return STAT_TIMESPEC (st, st_mtim).tv_nsec;
82 # elif defined STAT_TIMESPEC_NS
83   return STAT_TIMESPEC_NS (st, st_mtim);
84 # else
85   return 0;
86 # endif
87 }
88
89 /* Return the nanosecond component of *ST's birth time.  */
90 static inline long int
91 get_stat_birthtime_ns (struct stat const *st)
92 {
93 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
94   return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
95 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
96   return STAT_TIMESPEC_NS (st, st_birthtim);
97 # else
98   /* Avoid a "parameter unused" warning.  */
99   (void) st;
100   return 0;
101 # endif
102 }
103
104 /* Return *ST's access time.  */
105 static inline struct timespec
106 get_stat_atime (struct stat const *st)
107 {
108 #ifdef STAT_TIMESPEC
109   return STAT_TIMESPEC (st, st_atim);
110 #else
111   struct timespec t;
112   t.tv_sec = st->st_atime;
113   t.tv_nsec = get_stat_atime_ns (st);
114   return t;
115 #endif
116 }
117
118 /* Return *ST's status change time.  */
119 static inline struct timespec
120 get_stat_ctime (struct stat const *st)
121 {
122 #ifdef STAT_TIMESPEC
123   return STAT_TIMESPEC (st, st_ctim);
124 #else
125   struct timespec t;
126   t.tv_sec = st->st_ctime;
127   t.tv_nsec = get_stat_ctime_ns (st);
128   return t;
129 #endif
130 }
131
132 /* Return *ST's data modification time.  */
133 static inline struct timespec
134 get_stat_mtime (struct stat const *st)
135 {
136 #ifdef STAT_TIMESPEC
137   return STAT_TIMESPEC (st, st_mtim);
138 #else
139   struct timespec t;
140   t.tv_sec = st->st_mtime;
141   t.tv_nsec = get_stat_mtime_ns (st);
142   return t;
143 #endif
144 }
145
146 /* Return *ST's birth time, if available; otherwise return a value
147    with negative tv_nsec.  */
148 static inline struct timespec
149 get_stat_birthtime (struct stat const *st)
150 {
151   struct timespec t;
152
153 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
154      || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
155   t = STAT_TIMESPEC (st, st_birthtim);
156 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
157   t.tv_sec = st->st_birthtime;
158   t.tv_nsec = st->st_birthtimensec;
159 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
160   /* Woe32 native platforms (but not Cygwin) put the "file creation
161      time" in st_ctime (!).  See
162      <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>.  */
163   t.tv_sec = st->st_ctime;
164   t.tv_nsec = 0;
165 #else
166   /* Birth time is not supported.  Set tv_sec to avoid undefined behavior.  */
167   t.tv_sec = -1;
168   t.tv_nsec = -1;
169   /* Avoid a "parameter unused" warning.  */
170   (void) st;
171 #endif
172
173 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
174      || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
175      || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
176   /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
177      using zero.  Attempt to work around this problem.  Alas, this can
178      report failure even for valid time stamps.  Also, NetBSD
179      sometimes returns junk in the birth time fields; work around this
180      bug if it it is detected.  There's no need to detect negative
181      tv_nsec junk as negative tv_nsec already indicates an error.  */
182   if (t.tv_sec == 0 || 1000000000 <= t.tv_nsec)
183     t.tv_nsec = -1;
184 #endif
185
186   return t;
187 }
188
189 #endif