changeset 5059:eb3efcec6ae2

Don't assume that gettimeofday and settimeofday exist or work.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 13 May 2004 21:59:46 +0000
parents d67c037bd2ab
children 4e59cddb690f
files lib/ChangeLog lib/gettime.c lib/settime.c m4/ChangeLog m4/gettime.m4 m4/settime.m4
diffstat 6 files changed, 75 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -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
--- 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 @@
     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;
 }
--- 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 <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;
 }
--- a/m4/ChangeLog
+++ b/m4/ChangeLog
@@ -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.
--- 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 @@
 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)
 ])
--- 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 @@
 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)
 ])