Main Page | Class List | Directories | File List | Class Members | File Members

stat-time.h

Go to the documentation of this file.
00001 /* stat-related time functions.
00002 
00003    Copyright (C) 2005, 2007 Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 /* Written by Paul Eggert.  */
00020 
00021 #ifndef STAT_TIME_H
00022 #define STAT_TIME_H 1
00023 
00024 #include <sys/stat.h>
00025 #include <time.h>
00026 
00027 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
00028    struct timespec, if available.  If not, then STAT_TIMESPEC_NS (ST,
00029    ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
00030    if available.  ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
00031    for access, status change, data modification, or birth (creation)
00032    time respectively.
00033 
00034    These macros are private to stat-time.h.  */
00035 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
00036 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
00037 #  define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
00038 # else
00039 #  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
00040 # endif
00041 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
00042 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
00043 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
00044 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
00045 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
00046 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
00047 #endif
00048 
00049 /* Return the nanosecond component of *ST's access time.  */
00050 static inline long int
00051 get_stat_atime_ns (struct stat const *st)
00052 {
00053 # if defined STAT_TIMESPEC
00054   return STAT_TIMESPEC (st, st_atim).tv_nsec;
00055 # elif defined STAT_TIMESPEC_NS
00056   return STAT_TIMESPEC_NS (st, st_atim);
00057 # else
00058   return 0;
00059 # endif
00060 }
00061 
00062 /* Return the nanosecond component of *ST's status change time.  */
00063 static inline long int
00064 get_stat_ctime_ns (struct stat const *st)
00065 {
00066 # if defined STAT_TIMESPEC
00067   return STAT_TIMESPEC (st, st_ctim).tv_nsec;
00068 # elif defined STAT_TIMESPEC_NS
00069   return STAT_TIMESPEC_NS (st, st_ctim);
00070 # else
00071   return 0;
00072 # endif
00073 }
00074 
00075 /* Return the nanosecond component of *ST's data modification time.  */
00076 static inline long int
00077 get_stat_mtime_ns (struct stat const *st)
00078 {
00079 # if defined STAT_TIMESPEC
00080   return STAT_TIMESPEC (st, st_mtim).tv_nsec;
00081 # elif defined STAT_TIMESPEC_NS
00082   return STAT_TIMESPEC_NS (st, st_mtim);
00083 # else
00084   return 0;
00085 # endif
00086 }
00087 
00088 /* Return the nanosecond component of *ST's birth time.  */
00089 static inline long int
00090 get_stat_birthtime_ns (struct stat const *st)
00091 {
00092 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
00093   return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
00094 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
00095   return STAT_TIMESPEC_NS (st, st_birthtim);
00096 # else
00097   return 0;
00098 # endif
00099 }
00100 
00101 /* Return *ST's access time.  */
00102 static inline struct timespec
00103 get_stat_atime (struct stat const *st)
00104 {
00105 #ifdef STAT_TIMESPEC
00106   return STAT_TIMESPEC (st, st_atim);
00107 #else
00108   struct timespec t;
00109   t.tv_sec = st->st_atime;
00110   t.tv_nsec = get_stat_atime_ns (st);
00111   return t;
00112 #endif
00113 }
00114 
00115 /* Return *ST's status change time.  */
00116 static inline struct timespec
00117 get_stat_ctime (struct stat const *st)
00118 {
00119 #ifdef STAT_TIMESPEC
00120   return STAT_TIMESPEC (st, st_ctim);
00121 #else
00122   struct timespec t;
00123   t.tv_sec = st->st_ctime;
00124   t.tv_nsec = get_stat_ctime_ns (st);
00125   return t;
00126 #endif
00127 }
00128 
00129 /* Return *ST's data modification time.  */
00130 static inline struct timespec
00131 get_stat_mtime (struct stat const *st)
00132 {
00133 #ifdef STAT_TIMESPEC
00134   return STAT_TIMESPEC (st, st_mtim);
00135 #else
00136   struct timespec t;
00137   t.tv_sec = st->st_mtime;
00138   t.tv_nsec = get_stat_mtime_ns (st);
00139   return t;
00140 #endif
00141 }
00142 
00143 /* Return *ST's birth time, if available; otherwise return a value
00144    with negative tv_nsec.  */
00145 static inline struct timespec
00146 get_stat_birthtime (struct stat const *st)
00147 {
00148   struct timespec t;
00149 
00150 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
00151      || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
00152   t = STAT_TIMESPEC (st, st_birthtim);
00153 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
00154   t.tv_sec = st->st_birthtime;
00155   t.tv_nsec = st->st_birthtimensec;
00156 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00157   /* Woe32 native platforms (but not Cygwin) put the "file creation
00158      time" in st_ctime (!).  See
00159      <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>.  */
00160   t.tv_sec = st->st_ctime;
00161   t.tv_nsec = 0;
00162 #else
00163   /* Birth time is not supported.  Set tv_sec to avoid undefined behavior.  */
00164   t.tv_sec = -1;
00165   t.tv_nsec = -1;
00166 #endif
00167 
00168 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
00169      || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
00170      || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
00171   /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
00172      using zero.  Attempt to work around this problem.  Alas, this can
00173      report failure even for valid time stamps.  Also, NetBSD
00174      sometimes returns junk in the birth time fields; work around this
00175      bug if it it is detected.  There's no need to detect negative
00176      tv_nsec junk as negative tv_nsec already indicates an error.  */
00177   if (t.tv_sec == 0 || 1000000000 <= t.tv_nsec)
00178     t.tv_nsec = -1;
00179 #endif
00180 
00181   return t;
00182 }
00183 
00184 #endif

© sourcejam.com 2005-2008