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