Hi,

I've attached a patch to use current_timespec from Gnulib's 'gettime'
module instead of gettimeofday. POSIX.1-2024 removed gettimeofday and
recommends using clock_gettime instead which supports nanosecond
precision. Gnulib uses this for current_timespec or whatever the best
alternative is (gettimeofday vs. time).

Also, in the process of checking LDADD for this change I noticed a minor
bug. The find program was missing this change in Gnulib:

2023-01-07  clock_time      Link with $(CLOCK_TIME_LIB) instead of
                            $(LIB_CLOCK_GETTIME).

If I understand correctly it is only required for very old Solaris
versions. So not a major issue IMO.

Collin

>From 0c2bccb4278e0910191b1fa249a4b4cdea6718aa Mon Sep 17 00:00:00 2001
From: Collin Funk <[email protected]>
Date: Thu, 1 Aug 2024 19:18:30 -0700
Subject: [PATCH] maint: don't use gettimeofday

POSIX marked gettimeofday obsolete in POSIX.1-2017 and removed it in
POSIX.1-2024. Prefer Gnulib functions which use nanosecond resolution if
supported by the operating system.

* bootstrap.conf (gnulib_extra_files): Remove gettimeofday. Add gettime.
* find/util.c (now): Remove function.
(set_option_defaults): Use current_timespec.
* find/Makefile.am (LDADD): Rename $(LIB_CLOCK_GETTIME) to
$(CLOCK_TIME_LIB) per Gnulib NEWS 2023-01-07.
---
 bootstrap.conf   |  2 +-
 find/Makefile.am |  4 ++--
 find/util.c      | 22 +---------------------
 3 files changed, 4 insertions(+), 24 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 00203844..385acc05 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -107,7 +107,7 @@ gnulib_modules="
     getline
     getopt-gnu
     gettext-h
-    gettimeofday
+    gettime
     git-version-gen
     gnupload
     gpl-3.0
diff --git a/find/Makefile.am b/find/Makefile.am
index e1ccbfc8..67802225 100644
--- a/find/Makefile.am
+++ b/find/Makefile.am
@@ -28,10 +28,10 @@ man_MANS         = find.1
 
 EXTRA_DIST = defs.h sharefile.h print.h $(man_MANS)
 AM_CPPFLAGS = -I../gl/lib -I$(top_srcdir)/lib -I$(top_srcdir)/gl/lib -DLOCALEDIR=\"$(localedir)\"
-LDADD = libfindtools.a ../lib/libfind.a ../gl/lib/libgnulib.a $(LIBINTL) $(LIB_CLOCK_GETTIME) $(LIB_EACCESS) $(LIB_SELINUX) $(LIB_CLOSE) $(MODF_LIBM) $(FINDLIBS) $(GETHOSTNAME_LIB) $(LIB_EACCESS) $(LIB_SETLOCALE_NULL) $(LIB_MBRTOWC)
+LDADD = libfindtools.a ../lib/libfind.a ../gl/lib/libgnulib.a $(LIBINTL) $(CLOCK_TIME_LIB) $(LIB_EACCESS) $(LIB_SELINUX) $(LIB_CLOSE) $(MODF_LIBM) $(FINDLIBS) $(GETHOSTNAME_LIB) $(LIB_EACCESS) $(LIB_SETLOCALE_NULL) $(LIB_MBRTOWC)
 # gnulib advises we link against <first> because we use <second>:
 # $(GETHOSTNAME_LIB)            uname
-# $(LIB_CLOCK_GETTIME)          (some indirect dependency)
+# $(CLOCK_TIME_LIB)             gettime
 # $(LIB_EACCESS)                faccessat
 # $(LIB_SELINUX)                selinux-h
 # $(MODF_LIBM)                  modf
diff --git a/find/util.c b/find/util.c
index a1736807..541b4242 100644
--- a/find/util.c
+++ b/find/util.c
@@ -960,26 +960,6 @@ process_leading_options (int argc, char *argv[])
   return end_of_leading_options;
 }
 
-static struct timespec
-now(void)
-{
-  struct timespec retval;
-  struct timeval tv;
-  time_t t;
-
-  if (0 == gettimeofday (&tv, NULL))
-    {
-      retval.tv_sec  = tv.tv_sec;
-      retval.tv_nsec = tv.tv_usec * 1000; /* convert unit from microseconds to nanoseconds */
-      return retval;
-    }
-  t = time (NULL);
-  assert (t != (time_t)-1);
-  retval.tv_sec = t;
-  retval.tv_nsec = 0;
-  return retval;
-}
-
 void
 set_option_defaults (struct options *p)
 {
@@ -1021,7 +1001,7 @@ set_option_defaults (struct options *p)
   p->explicit_depth = false;
   p->maxdepth = p->mindepth = -1;
 
-  p->start_time = now ();
+  p->start_time = current_timespec ();
   p->cur_day_start.tv_sec = p->start_time.tv_sec - DAYSECS;
   p->cur_day_start.tv_nsec = p->start_time.tv_nsec;
 
-- 
2.45.2

Reply via email to