From ae218aeb74edf96e6d3959d80a6ba643a0674cf8 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 13 May 2004 21:59:46 +0000 Subject: [PATCH] Don't assume that gettimeofday and settimeofday exist or work. --- lib/ChangeLog | 9 +++++++++ lib/gettime.c | 21 +++++++++++++++++---- lib/settime.c | 39 +++++++++++++++++++++++++++++++++++---- m4/ChangeLog | 6 ++++++ m4/gettime.m4 | 7 ++++--- m4/settime.m4 | 7 ++++--- 6 files changed, 75 insertions(+), 14 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index c45e64958..2283e15ff 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,12 @@ +2004-05-13 Paul Eggert + + * gettime.c (gettime): Fall back on `time' if `gettimeofday' + doesn't work. + * settime.c: Include , for stime (on Solaris 8, anyway). + (ENOSYS): Define if not defined. + (settime): Fall back on stime if it exists and settimeofday fails. + But don't bother with fallbacks if a method fails with errno == EPERM. + 2004-05-11 Jim Meyering Prior to this change, the save_cwd caller required read access to the diff --git a/lib/gettime.c b/lib/gettime.c index 528060733..715d1916a 100644 --- a/lib/gettime.c +++ b/lib/gettime.c @@ -1,5 +1,5 @@ /* gettime -- get the system clock - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 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 @@ -33,14 +33,27 @@ gettime (struct timespec *ts) return 0; #endif +#if HAVE_GETTIMEOFDAY { struct timeval tv; - int r = gettimeofday (&tv, 0); - if (r == 0) + if (gettimeofday (&tv, 0) == 0) { ts->tv_sec = tv.tv_sec; ts->tv_nsec = tv.tv_usec * 1000; + return 0; } - return r; } +#endif + + { + time_t t = time (0); + if (t != (time_t) -1) + { + ts->tv_sec = t; + ts->tv_nsec = 0; + return 0; + } + } + + return -1; } diff --git a/lib/settime.c b/lib/settime.c index 277c80583..e989a6db6 100644 --- a/lib/settime.c +++ b/lib/settime.c @@ -1,5 +1,5 @@ /* settime -- set the system clock - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 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 @@ -23,21 +23,52 @@ #include "timespec.h" +#if HAVE_UNISTD_H +# include +#endif + +#include + +/* Some systems don't have ENOSYS. */ +#ifndef ENOSYS +# ifdef ENOTSUP +# define ENOSYS ENOTSUP +# else +/* Some systems don't have ENOTSUP either. */ +# define ENOSYS EINVAL +# endif +#endif + /* Set the system time. */ int settime (struct timespec const *ts) { #if defined CLOCK_REALTIME && HAVE_CLOCK_SETTIME - if (clock_settime (CLOCK_REALTIME, ts) == 0) - return 0; + { + int r = clock_settime (CLOCK_REALTIME, ts); + if (r == 0 || errno == EPERM) + return r; + } #endif +#if HAVE_SETTIMEOFDAY { struct timeval tv; + int r; tv.tv_sec = ts->tv_sec; tv.tv_usec = ts->tv_nsec / 1000; - return settimeofday (&tv, 0); + r = settimeofday (&tv, 0); + if (r == 0 || errno == EPERM) + return r; } +#endif + +#if HAVE_STIME + return stime (&ts->tv_sec); +#endif + + errno = ENOSYS; + return -1; } diff --git a/m4/ChangeLog b/m4/ChangeLog index 3acdb345f..47612ddf9 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,9 @@ +2004-05-13 Paul Eggert + + * gettime.m4 (gl_GETTIME): Require gl_TIMESPEC. Check for gettimeofday. + * settime.m4 (gl_SETTIME): Require gl_TIMESPEC. Check for settimeofday, + stime. + 2004-04-20 Paul Eggert * host-os.m4: Add a copyright notice. diff --git a/m4/gettime.m4 b/m4/gettime.m4 index 645e71298..37b9098e8 100644 --- a/m4/gettime.m4 +++ b/m4/gettime.m4 @@ -1,5 +1,5 @@ -# gettime.m4 serial 1 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# gettime.m4 serial 2 +dnl Copyright (C) 2002, 2004 Free Software Foundation, Inc. dnl This file is free software, distributed under the terms of the GNU dnl General Public License. As a special exception to the GNU General dnl Public License, this file may be distributed as part of a program @@ -9,6 +9,7 @@ dnl the same distribution terms as the rest of that program. AC_DEFUN([gl_GETTIME], [ dnl Prerequisites of lib/gettime.c. - # Need clock_gettime. AC_REQUIRE([gl_CLOCK_TIME]) + AC_REQUIRE([gl_TIMESPEC]) + AC_CHECK_FUNCS_ONCE(gettimeofday) ]) diff --git a/m4/settime.m4 b/m4/settime.m4 index 3751af564..89c030cd0 100644 --- a/m4/settime.m4 +++ b/m4/settime.m4 @@ -1,5 +1,5 @@ -# settime.m4 serial 1 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# settime.m4 serial 2 +dnl Copyright (C) 2002, 2004 Free Software Foundation, Inc. dnl This file is free software, distributed under the terms of the GNU dnl General Public License. As a special exception to the GNU General dnl Public License, this file may be distributed as part of a program @@ -9,6 +9,7 @@ dnl the same distribution terms as the rest of that program. AC_DEFUN([gl_SETTIME], [ dnl Prerequisites of lib/settime.c. - # Need clock_settime. AC_REQUIRE([gl_CLOCK_TIME]) + AC_REQUIRE([gl_TIMESPEC]) + AC_CHECK_FUNCS_ONCE(settimeofday stime) ]) -- 2.11.0