Commit c8fff863 authored by Paul Eggert's avatar Paul Eggert

Add gnulib files to support higher-resolution time stamps.

Fixes: debbugs:9000
parent 36cec983
/* Convert double to timespec.
Copyright (C) 2011-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* written by Paul Eggert */
/* Convert the double value SEC to a struct timespec. Round toward
positive infinity. On overflow, return an extremal value. */
#include <config.h>
#include "timespec.h"
#include "intprops.h"
struct timespec
dtotimespec (double sec)
{
enum { BILLION = 1000 * 1000 * 1000 };
double min_representable = TYPE_MINIMUM (time_t);
double max_representable =
((TYPE_MAXIMUM (time_t) * (double) BILLION + (BILLION - 1))
/ BILLION);
struct timespec r;
if (! (min_representable < sec))
{
r.tv_sec = TYPE_MINIMUM (time_t);
r.tv_nsec = 0;
}
else if (! (sec < max_representable))
{
r.tv_sec = TYPE_MAXIMUM (time_t);
r.tv_nsec = BILLION - 1;
}
else
{
time_t s = sec;
double frac = BILLION * (sec - s);
long ns = frac;
ns += ns < frac;
s += ns / BILLION;
ns %= BILLION;
if (ns < 0)
{
s--;
ns += BILLION;
}
r.tv_sec = s;
r.tv_nsec = ns;
}
return r;
}
/* gettime -- get the system clock
Copyright (C) 2002, 2004-2007, 2009-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Written by Paul Eggert. */
#include <config.h>
#include "timespec.h"
#include <sys/time.h>
/* Get the system time into *TS. */
void
gettime (struct timespec *ts)
{
#if HAVE_NANOTIME
nanotime (ts);
#else
# if defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME
if (clock_gettime (CLOCK_REALTIME, ts) == 0)
return;
# endif
{
struct timeval tv;
gettimeofday (&tv, NULL);
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
#endif
}
/* Provide gettimeofday for systems that don't have it or for which it's broken.
Copyright (C) 2001-2003, 2005-2007, 2009-2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>. */
/* written by Jim Meyering */
#include <config.h>
/* Specification. */
#include <sys/time.h>
#include <time.h>
#if HAVE_SYS_TIMEB_H
# include <sys/timeb.h>
#endif
#if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
/* Work around the bug in some systems whereby gettimeofday clobbers
the static buffer that localtime uses for its return value. The
gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
this problem. The tzset replacement is necessary for at least
Solaris 2.5, 2.5.1, and 2.6. */
static struct tm tm_zero_buffer;
static struct tm *localtime_buffer_addr = &tm_zero_buffer;
# undef localtime
extern struct tm *localtime (time_t const *);
# undef gmtime
extern struct tm *gmtime (time_t const *);
/* This is a wrapper for localtime. It is used only on systems for which
gettimeofday clobbers the static buffer used for localtime's result.
On the first call, record the address of the static buffer that
localtime uses for its result. */
struct tm *
rpl_localtime (time_t const *timep)
{
struct tm *tm = localtime (timep);
if (localtime_buffer_addr == &tm_zero_buffer)
localtime_buffer_addr = tm;
return tm;
}
/* Same as above, since gmtime and localtime use the same buffer. */
struct tm *
rpl_gmtime (time_t const *timep)
{
struct tm *tm = gmtime (timep);
if (localtime_buffer_addr == &tm_zero_buffer)
localtime_buffer_addr = tm;
return tm;
}
#endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
#if TZSET_CLOBBERS_LOCALTIME
# undef tzset
extern void tzset (void);
/* This is a wrapper for tzset, for systems on which tzset may clobber
the static buffer used for localtime's result. */
void
rpl_tzset (void)
{
/* Save and restore the contents of the buffer used for localtime's
result around the call to tzset. */
struct tm save = *localtime_buffer_addr;
tzset ();
*localtime_buffer_addr = save;
}
#endif
/* This is a wrapper for gettimeofday. It is used only on systems
that lack this function, or whose implementation of this function
causes problems. */
int
gettimeofday (struct timeval *restrict tv, void *restrict tz)
{
#undef gettimeofday
#if HAVE_GETTIMEOFDAY
# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
/* Save and restore the contents of the buffer used for localtime's
result around the call to gettimeofday. */
struct tm save = *localtime_buffer_addr;
# endif
# if defined timeval /* 'struct timeval' overridden by gnulib? */
# undef timeval
struct timeval otv;
int result = gettimeofday (&otv, (struct timezone *) tz);
if (result == 0)
{
tv->tv_sec = otv.tv_sec;
tv->tv_usec = otv.tv_usec;
}
# else
int result = gettimeofday (tv, (struct timezone *) tz);
# endif
# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
*localtime_buffer_addr = save;
# endif
return result;
#else
# if HAVE__FTIME
struct _timeb timebuf;
_ftime (&timebuf);
tv->tv_sec = timebuf.time;
tv->tv_usec = timebuf.millitm * 1000;
# else
# if !defined OK_TO_USE_1S_CLOCK
# error "Only 1-second nominal clock resolution found. Is that intended?" \
"If so, compile with the -DOK_TO_USE_1S_CLOCK option."
# endif
tv->tv_sec = time (NULL);
tv->tv_usec = 0;
# endif
return 0;
#endif
}
/* pselect - synchronous I/O multiplexing
Copyright 2011-2012 Free Software Foundation, Inc.
This file is part of gnulib.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
/* written by Paul Eggert */
#include <config.h>
#include <sys/select.h>
#include <errno.h>
#include <signal.h>
/* Examine the size-NFDS file descriptor sets in RFDS, WFDS, and XFDS
to see whether some of their descriptors are ready for reading,
ready for writing, or have exceptions pending. Wait for at most
TIMEOUT seconds, and use signal mask SIGMASK while waiting. A null
pointer parameter stands for no descriptors, an infinite timeout,
or an unaffected signal mask. */
int
pselect (int nfds, fd_set *restrict rfds,
fd_set *restrict wfds, fd_set *restrict xfds,
struct timespec const *restrict timeout,
sigset_t const *restrict sigmask)
{
int select_result;
sigset_t origmask;
struct timeval tv, *tvp;
if (timeout)
{
if (! (0 <= timeout->tv_nsec && timeout->tv_nsec < 1000000000))
{
errno = EINVAL;
return -1;
}
tv.tv_sec = timeout->tv_sec;
tv.tv_usec = (timeout->tv_nsec + 999) / 1000;
tvp = &tv;
}
else
tvp = NULL;
/* Signal mask munging should be atomic, but this is the best we can
do in this emulation. */
if (sigmask)
pthread_sigmask (SIG_SETMASK, sigmask, &origmask);
select_result = select (nfds, rfds, wfds, xfds, tvp);
if (sigmask)
{
int select_errno = errno;
pthread_sigmask (SIG_SETMASK, &origmask, NULL);
errno = select_errno;
}
return select_result;
}
/* stat-related time functions.
Copyright (C) 2005, 2007, 2009-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Written by Paul Eggert. */
#ifndef STAT_TIME_H
#define STAT_TIME_H 1
#include <sys/stat.h>
#include <time.h>
/* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
for access, status change, data modification, or birth (creation)
time respectively.
These macros are private to stat-time.h. */
#if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
# ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
# else
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
# endif
#elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
#elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
#elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
#endif
/* Return the nanosecond component of *ST's access time. */
static inline long int
get_stat_atime_ns (struct stat const *st)
{
# if defined STAT_TIMESPEC
return STAT_TIMESPEC (st, st_atim).tv_nsec;
# elif defined STAT_TIMESPEC_NS
return STAT_TIMESPEC_NS (st, st_atim);
# else
return 0;
# endif
}
/* Return the nanosecond component of *ST's status change time. */
static inline long int
get_stat_ctime_ns (struct stat const *st)
{
# if defined STAT_TIMESPEC
return STAT_TIMESPEC (st, st_ctim).tv_nsec;
# elif defined STAT_TIMESPEC_NS
return STAT_TIMESPEC_NS (st, st_ctim);
# else
return 0;
# endif
}
/* Return the nanosecond component of *ST's data modification time. */
static inline long int
get_stat_mtime_ns (struct stat const *st)
{
# if defined STAT_TIMESPEC
return STAT_TIMESPEC (st, st_mtim).tv_nsec;
# elif defined STAT_TIMESPEC_NS
return STAT_TIMESPEC_NS (st, st_mtim);
# else
return 0;
# endif
}
/* Return the nanosecond component of *ST's birth time. */
static inline long int
get_stat_birthtime_ns (struct stat const *st)
{
# if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
# elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
return STAT_TIMESPEC_NS (st, st_birthtim);
# else
/* Avoid a "parameter unused" warning. */
(void) st;
return 0;
# endif
}
/* Return *ST's access time. */
static inline struct timespec
get_stat_atime (struct stat const *st)
{
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_atim);
#else
struct timespec t;
t.tv_sec = st->st_atime;
t.tv_nsec = get_stat_atime_ns (st);
return t;
#endif
}
/* Return *ST's status change time. */
static inline struct timespec
get_stat_ctime (struct stat const *st)
{
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_ctim);
#else
struct timespec t;
t.tv_sec = st->st_ctime;
t.tv_nsec = get_stat_ctime_ns (st);
return t;
#endif
}
/* Return *ST's data modification time. */
static inline struct timespec
get_stat_mtime (struct stat const *st)
{
#ifdef STAT_TIMESPEC
return STAT_TIMESPEC (st, st_mtim);
#else
struct timespec t;
t.tv_sec = st->st_mtime;
t.tv_nsec = get_stat_mtime_ns (st);
return t;
#endif
}
/* Return *ST's birth time, if available; otherwise return a value
with tv_sec and tv_nsec both equal to -1. */
static inline struct timespec
get_stat_birthtime (struct stat const *st)
{
struct timespec t;
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
t = STAT_TIMESPEC (st, st_birthtim);
#elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
t.tv_sec = st->st_birthtime;
t.tv_nsec = st->st_birthtimensec;
#elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
/* Native Windows platforms (but not Cygwin) put the "file creation
time" in st_ctime (!). See
<http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
t.tv_sec = st->st_ctime;
t.tv_nsec = 0;
#else
/* Birth time is not supported. */
t.tv_sec = -1;
t.tv_nsec = -1;
/* Avoid a "parameter unused" warning. */
(void) st;
#endif
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
/* FreeBSD and NetBSD sometimes signal the absence of knowledge by
using zero. Attempt to work around this problem. Alas, this can
report failure even for valid time stamps. Also, NetBSD
sometimes returns junk in the birth time fields; work around this
bug if it is detected. */
if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
{
t.tv_sec = -1;
t.tv_nsec = -1;
}
#endif
return t;
}
#endif
/* Substitute for <sys/select.h>.
Copyright (C) 2007-2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>. */
# if __GNUC__ >= 3
@PRAGMA_SYSTEM_HEADER@
# endif
@PRAGMA_COLUMNS@
/* On OSF/1, <sys/types.h> and <sys/time.h> include <sys/select.h>.
Simply delegate to the system's header in this case. */
#if @HAVE_SYS_SELECT_H@ && defined __osf__ && (defined _SYS_TYPES_H_ && !defined _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TYPES_H) && defined _OSF_SOURCE
# define _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TYPES_H
# @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
#elif @HAVE_SYS_SELECT_H@ && defined __osf__ && (defined _SYS_TIME_H_ && !defined _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H) && defined _OSF_SOURCE
# define _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H
# @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
/* On IRIX 6.5, <sys/timespec.h> includes <sys/types.h>, which includes
<sys/bsd_types.h>, which includes <sys/select.h>. At this point we cannot
include <signal.h>, because that includes <internal/signal_core.h>, which
gives a syntax error because <sys/timespec.h> has not been completely
processed. Simply delegate to the system's header in this case. */
#elif @HAVE_SYS_SELECT_H@ && defined __sgi && (defined _SYS_BSD_TYPES_H && !defined _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_BSD_TYPES_H)
# define _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_BSD_TYPES_H
# @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
/* On OpenBSD 5.0, <pthread.h> includes <sys/types.h>, which includes
<sys/select.h>. At this point we cannot include <signal.h>, because that
includes gnulib's pthread.h override, which gives a syntax error because
/usr/include/pthread.h has not been completely processed. Simply delegate
to the system's header in this case. */
#elif @HAVE_SYS_SELECT_H@ && defined __OpenBSD__ && (defined _PTHREAD_H_ && !defined PTHREAD_MUTEX_INITIALIZER)
# @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
#else
#ifndef _@GUARD_PREFIX@_SYS_SELECT_H
/* On many platforms, <sys/select.h> assumes prior inclusion of
<sys/types.h>. Also, mingw defines sigset_t there, instead of
in <signal.h> where it belongs. */
#include <sys/types.h>
#if @HAVE_SYS_SELECT_H@
/* On OSF/1 4.0, <sys/select.h> provides only a forward declaration
of 'struct timeval', and no definition of this type.
Also, Mac OS X, AIX, HP-UX, IRIX, Solaris, Interix declare select()
in <sys/time.h>.
But avoid namespace pollution on glibc systems. */
# ifndef __GLIBC__
# include <sys/time.h>
# endif
/* On AIX 7 and Solaris 10, <sys/select.h> provides an FD_ZERO implementation
that relies on memset(), but without including <string.h>.
But in any case avoid namespace pollution on glibc systems. */
# if (defined __OpenBSD__ || defined _AIX || defined __sun || defined __osf__ || defined __BEOS__) \
&& ! defined __GLIBC__
# include <string.h>
# endif
/* The include_next requires a split double-inclusion guard. */
# @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
#endif
/* Get definition of 'sigset_t'.
But avoid namespace pollution on glibc systems.
Do this after the include_next (for the sake of OpenBSD 5.0) but before
the split double-inclusion guard (for the sake of Solaris). */
#if !(defined __GLIBC__ && !defined __UCLIBC__)
# include <signal.h>
#endif
#ifndef _@GUARD_PREFIX@_SYS_SELECT_H
#define _@GUARD_PREFIX@_SYS_SELECT_H
#if !@HAVE_SYS_SELECT_H@
/* A platform that lacks <sys/select.h>. */