Don't assume that gettimeofday and settimeofday exist or work.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 13 May 2004 21:59:46 +0000 (21:59 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 13 May 2004 21:59:46 +0000 (21:59 +0000)
lib/ChangeLog
lib/gettime.c
lib/settime.c
m4/ChangeLog
m4/gettime.m4
m4/settime.m4

index c45e649..2283e15 100644 (file)
@@ -1,3 +1,12 @@
+2004-05-13  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * gettime.c (gettime): Fall back on `time' if `gettimeofday'
+       doesn't work.
+       * settime.c: Include <unistd.h>, 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  <jim@meyering.net>
 
        Prior to this change, the save_cwd caller required read access to the
index 5280607..715d191 100644 (file)
@@ -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;
 }
index 277c805..e989a6d 100644 (file)
@@ -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
 
 #include "timespec.h"
 
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <errno.h>
+
+/* 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;
 }
index 3acdb34..47612dd 100644 (file)
@@ -1,3 +1,9 @@
+2004-05-13  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * 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  <eggert@twinsun.com>
 
        * host-os.m4: Add a copyright notice.
index 645e712..37b9098 100644 (file)
@@ -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)
 ])
index 3751af5..89c030c 100644 (file)
@@ -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)
 ])