* MODULES.html.sh (File system functions): Add stat-time.
[pspp] / 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 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
35 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
36 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
37 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
38 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
39 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
40 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
41 #endif
42
43 /* Return the nanosecond component of *ST's access time.  */
44 static inline long int
45 get_stat_atime_ns (struct stat const *st)
46 {
47 # if defined STAT_TIMESPEC
48   return STAT_TIMESPEC (st, st_atim).tv_nsec;
49 # elif defined STAT_TIMESPEC_NS
50   return STAT_TIMESPEC_NS (st, st_atim);
51 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
52   return st->st_spare1 * 1000;
53 # else
54   return 0;
55 # endif
56 }
57
58 /* Return the nanosecond component of *ST's status change time.  */
59 static inline long int
60 get_stat_ctime_ns (struct stat const *st)
61 {
62 # if defined STAT_TIMESPEC
63   return STAT_TIMESPEC (st, st_ctim).tv_nsec;
64 # elif defined STAT_TIMESPEC_NS
65   return STAT_TIMESPEC_NS (st, st_ctim);
66 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
67   return st->st_spare3 * 1000;
68 # else
69   return 0;
70 # endif
71 }
72
73 /* Return the nanosecond component of *ST's data modification time.  */
74 static inline long int
75 get_stat_mtime_ns (struct stat const *st)
76 {
77 # if defined STAT_TIMESPEC
78   return STAT_TIMESPEC (st, st_mtim).tv_nsec;
79 # elif defined STAT_TIMESPEC_NS
80   return STAT_TIMESPEC_NS (st, st_mtim);
81 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
82   return st->st_spare2 * 1000;
83 # else
84   return 0;
85 # endif
86 }
87
88 /* Return *ST's access time.  */
89 static inline struct timespec
90 get_stat_atime (struct stat const *st)
91 {
92 #ifdef STAT_TIMESPEC
93   return STAT_TIMESPEC (st, st_atim);
94 #else
95   struct timespec t;
96   t.tv_sec = st->st_atime;
97   t.tv_nsec = get_stat_atime_ns (st);
98   return t;
99 #endif
100 }
101
102 /* Return *ST's status change time.  */
103 static inline struct timespec
104 get_stat_ctime (struct stat const *st)
105 {
106 #ifdef STAT_TIMESPEC
107   return STAT_TIMESPEC (st, st_ctim);
108 #else
109   struct timespec t;
110   t.tv_sec = st->st_ctime;
111   t.tv_nsec = get_stat_ctime_ns (st);
112   return t;
113 #endif
114 }
115
116 /* Return *ST's data modification time.  */
117 static inline struct timespec
118 get_stat_mtime (struct stat const *st)
119 {
120 #ifdef STAT_TIMESPEC
121   return STAT_TIMESPEC (st, st_mtim);
122 #else
123   struct timespec t;
124   t.tv_sec = st->st_mtime;
125   t.tv_nsec = get_stat_mtime_ns (st);
126   return t;
127 #endif
128 }
129
130 /* Set *ST's access time.  */
131 static inline void
132 set_stat_atime (struct stat *st, struct timespec t)
133 {
134 #ifdef STAT_TIMESPEC
135   STAT_TIMESPEC (st, st_atim) = t;
136 #else
137   st->st_atime = t.tv_sec;
138 # if defined STAT_TIMESPEC_NS
139   STAT_TIMESPEC_NS (st, st_atim) = t.tv_nsec;
140 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
141   st->st_spare1 = t.tv_nsec / 1000;
142 # endif
143 #endif
144 }
145
146 /* Set *ST's status change time.  */
147 static inline void
148 set_stat_ctime (struct stat *st, struct timespec t)
149 {
150 #ifdef STAT_TIMESPEC
151   STAT_TIMESPEC (st, st_ctim) = t;
152 #else
153   st->st_ctime = t.tv_sec;
154 # if defined STAT_TIMESPEC_NS
155   STAT_TIMESPEC_NS (st, st_ctim) = t.tv_nsec;
156 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
157   st->st_spare1 = t.tv_nsec / 1000;
158 # endif
159 #endif
160 }
161
162 /* Set *ST's data modification time.  */
163 static inline void
164 set_stat_mtime (struct stat *st, struct timespec t)
165 {
166 #ifdef STAT_TIMESPEC
167   STAT_TIMESPEC (st, st_mtim) = t;
168 #else
169   st->st_mtime = t.tv_sec;
170 # if defined STAT_TIMESPEC_NS
171   STAT_TIMESPEC_NS (st, st_mtim) = t.tv_nsec;
172 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
173   st->st_spare1 = t.tv_nsec / 1000;
174 # endif
175 #endif
176 }
177
178 #endif