]> git.cworth.org Git - gzip/blob - lib/stat-time.h
Imported Debian patch 1.3.9-1
[gzip] / lib / stat-time.h
1 /* stat-related time functions.
2
3    Copyright (C) 2005 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #ifndef STAT_TIME_H
22 #define STAT_TIME_H 1
23
24 #include "timespec.h"
25
26 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
27    struct timespec, if available.  If not, then STAT_TIMESPEC_NS (ST,
28    ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
29    if available.  ST_XTIM can be st_atim, st_ctim, or st_mtim for
30    access, status change, or data modification time, respectively.
31
32    These macros are private to stat-time.h.  */
33 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
34 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
35 #  define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
36 # else
37 #  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
38 # endif
39 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
40 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
41 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
42 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
43 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
44 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
45 #endif
46
47 /* Return the nanosecond component of *ST's access time.  */
48 static inline long int
49 get_stat_atime_ns (struct stat const *st)
50 {
51 # if defined STAT_TIMESPEC
52   return STAT_TIMESPEC (st, st_atim).tv_nsec;
53 # elif defined STAT_TIMESPEC_NS
54   return STAT_TIMESPEC_NS (st, st_atim);
55 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
56   return st->st_spare1 * 1000;
57 # else
58   return 0;
59 # endif
60 }
61
62 /* Return the nanosecond component of *ST's status change time.  */
63 static inline long int
64 get_stat_ctime_ns (struct stat const *st)
65 {
66 # if defined STAT_TIMESPEC
67   return STAT_TIMESPEC (st, st_ctim).tv_nsec;
68 # elif defined STAT_TIMESPEC_NS
69   return STAT_TIMESPEC_NS (st, st_ctim);
70 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
71   return st->st_spare3 * 1000;
72 # else
73   return 0;
74 # endif
75 }
76
77 /* Return the nanosecond component of *ST's data modification time.  */
78 static inline long int
79 get_stat_mtime_ns (struct stat const *st)
80 {
81 # if defined STAT_TIMESPEC
82   return STAT_TIMESPEC (st, st_mtim).tv_nsec;
83 # elif defined STAT_TIMESPEC_NS
84   return STAT_TIMESPEC_NS (st, st_mtim);
85 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
86   return st->st_spare2 * 1000;
87 # else
88   return 0;
89 # endif
90 }
91
92 /* Return *ST's access time.  */
93 static inline struct timespec
94 get_stat_atime (struct stat const *st)
95 {
96 #ifdef STAT_TIMESPEC
97   return STAT_TIMESPEC (st, st_atim);
98 #else
99   struct timespec t;
100   t.tv_sec = st->st_atime;
101   t.tv_nsec = get_stat_atime_ns (st);
102   return t;
103 #endif
104 }
105
106 /* Return *ST's status change time.  */
107 static inline struct timespec
108 get_stat_ctime (struct stat const *st)
109 {
110 #ifdef STAT_TIMESPEC
111   return STAT_TIMESPEC (st, st_ctim);
112 #else
113   struct timespec t;
114   t.tv_sec = st->st_ctime;
115   t.tv_nsec = get_stat_ctime_ns (st);
116   return t;
117 #endif
118 }
119
120 /* Return *ST's data modification time.  */
121 static inline struct timespec
122 get_stat_mtime (struct stat const *st)
123 {
124 #ifdef STAT_TIMESPEC
125   return STAT_TIMESPEC (st, st_mtim);
126 #else
127   struct timespec t;
128   t.tv_sec = st->st_mtime;
129   t.tv_nsec = get_stat_mtime_ns (st);
130   return t;
131 #endif
132 }
133
134 #endif