Blame lib/stat-time.h

Packit Service fdd496
/* stat-related time functions.
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 2005, 2007, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Paul Eggert.  */
Packit Service fdd496
Packit Service fdd496
#ifndef STAT_TIME_H
Packit Service fdd496
#define STAT_TIME_H 1
Packit Service fdd496
Packit Service fdd496
#include <sys/stat.h>
Packit Service fdd496
#include <time.h>
Packit Service fdd496
Packit Service fdd496
#ifndef _GL_INLINE_HEADER_BEGIN
Packit Service fdd496
 #error "Please include config.h first."
Packit Service fdd496
#endif
Packit Service fdd496
_GL_INLINE_HEADER_BEGIN
Packit Service fdd496
#ifndef _GL_STAT_TIME_INLINE
Packit Service fdd496
# define _GL_STAT_TIME_INLINE _GL_INLINE
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#ifdef __cplusplus
Packit Service fdd496
extern "C" {
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
Packit Service fdd496
   struct timespec, if available.  If not, then STAT_TIMESPEC_NS (ST,
Packit Service fdd496
   ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
Packit Service fdd496
   if available.  ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
Packit Service fdd496
   for access, status change, data modification, or birth (creation)
Packit Service fdd496
   time respectively.
Packit Service fdd496
Packit Service fdd496
   These macros are private to stat-time.h.  */
Packit Service fdd496
#if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
Packit Service fdd496
# if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
Packit Service fdd496
#  define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
Packit Service fdd496
# else
Packit Service fdd496
#  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
Packit Service fdd496
# endif
Packit Service fdd496
#elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
Packit Service fdd496
# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
Packit Service fdd496
#elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
Packit Service fdd496
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
Packit Service fdd496
#elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
Packit Service fdd496
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* Return the nanosecond component of *ST's access time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_atime_ns (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
# if defined STAT_TIMESPEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_atim).tv_nsec;
Packit Service fdd496
# elif defined STAT_TIMESPEC_NS
Packit Service fdd496
  return STAT_TIMESPEC_NS (st, st_atim);
Packit Service fdd496
# else
Packit Service fdd496
  return 0;
Packit Service fdd496
# endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return the nanosecond component of *ST's status change time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_ctime_ns (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
# if defined STAT_TIMESPEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_ctim).tv_nsec;
Packit Service fdd496
# elif defined STAT_TIMESPEC_NS
Packit Service fdd496
  return STAT_TIMESPEC_NS (st, st_ctim);
Packit Service fdd496
# else
Packit Service fdd496
  return 0;
Packit Service fdd496
# endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return the nanosecond component of *ST's data modification time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_mtime_ns (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
# if defined STAT_TIMESPEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_mtim).tv_nsec;
Packit Service fdd496
# elif defined STAT_TIMESPEC_NS
Packit Service fdd496
  return STAT_TIMESPEC_NS (st, st_mtim);
Packit Service fdd496
# else
Packit Service fdd496
  return 0;
Packit Service fdd496
# endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return the nanosecond component of *ST's birth time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_birthtime_ns (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
# if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
Packit Service fdd496
# elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
Packit Service fdd496
  return STAT_TIMESPEC_NS (st, st_birthtim);
Packit Service fdd496
# else
Packit Service fdd496
  /* Avoid a "parameter unused" warning.  */
Packit Service fdd496
  (void) st;
Packit Service fdd496
  return 0;
Packit Service fdd496
# endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return *ST's access time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_atime (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
#ifdef STAT_TIMESPEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_atim);
Packit Service fdd496
#else
Packit Service fdd496
  struct timespec t;
Packit Service fdd496
  t.tv_sec = st->st_atime;
Packit Service fdd496
  t.tv_nsec = get_stat_atime_ns (st);
Packit Service fdd496
  return t;
Packit Service fdd496
#endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return *ST's status change time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_ctime (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
#ifdef STAT_TIMESPEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_ctim);
Packit Service fdd496
#else
Packit Service fdd496
  struct timespec t;
Packit Service fdd496
  t.tv_sec = st->st_ctime;
Packit Service fdd496
  t.tv_nsec = get_stat_ctime_ns (st);
Packit Service fdd496
  return t;
Packit Service fdd496
#endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return *ST's data modification time.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_mtime (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
#ifdef STAT_TIMESPEC
Packit Service fdd496
  return STAT_TIMESPEC (st, st_mtim);
Packit Service fdd496
#else
Packit Service fdd496
  struct timespec t;
Packit Service fdd496
  t.tv_sec = st->st_mtime;
Packit Service fdd496
  t.tv_nsec = get_stat_mtime_ns (st);
Packit Service fdd496
  return t;
Packit Service fdd496
#endif
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Return *ST's birth time, if available; otherwise return a value
Packit Service fdd496
   with tv_sec and tv_nsec both equal to -1.  */
Packit Service fdd496
_GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
Packit Service fdd496
get_stat_birthtime (struct stat const *st)
Packit Service fdd496
{
Packit Service fdd496
  struct timespec t;
Packit Service fdd496
Packit Service fdd496
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
Packit Service fdd496
     || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
Packit Service fdd496
  t = STAT_TIMESPEC (st, st_birthtim);
Packit Service fdd496
#elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
Packit Service fdd496
  t.tv_sec = st->st_birthtime;
Packit Service fdd496
  t.tv_nsec = st->st_birthtimensec;
Packit Service fdd496
#elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit Service fdd496
  /* Native Windows platforms (but not Cygwin) put the "file creation
Packit Service fdd496
     time" in st_ctime (!).  See
Packit Service fdd496
     <https://msdn.microsoft.com/en-us/library/14h5k7ff(VS.80).aspx>.  */
Packit Service fdd496
# if _GL_WINDOWS_STAT_TIMESPEC
Packit Service fdd496
  t = st->st_ctim;
Packit Service fdd496
# else
Packit Service fdd496
  t.tv_sec = st->st_ctime;
Packit Service fdd496
  t.tv_nsec = 0;
Packit Service fdd496
# endif
Packit Service fdd496
#else
Packit Service fdd496
  /* Birth time is not supported.  */
Packit Service fdd496
  t.tv_sec = -1;
Packit Service fdd496
  t.tv_nsec = -1;
Packit Service fdd496
  /* Avoid a "parameter unused" warning.  */
Packit Service fdd496
  (void) st;
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
Packit Service fdd496
     || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
Packit Service fdd496
     || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
Packit Service fdd496
  /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
Packit Service fdd496
     using zero.  Attempt to work around this problem.  Alas, this can
Packit Service fdd496
     report failure even for valid timestamps.  Also, NetBSD
Packit Service fdd496
     sometimes returns junk in the birth time fields; work around this
Packit Service fdd496
     bug if it is detected.  */
Packit Service fdd496
  if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
Packit Service fdd496
    {
Packit Service fdd496
      t.tv_sec = -1;
Packit Service fdd496
      t.tv_nsec = -1;
Packit Service fdd496
    }
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  return t;
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
#ifdef __cplusplus
Packit Service fdd496
}
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
_GL_INLINE_HEADER_END
Packit Service fdd496
Packit Service fdd496
#endif