I've committed this patch. Similar to the two I sent yesterday.

Collin

>From 572af87d9ed5c8cdccee006bac79f958b211a880 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Thu, 27 Jun 2024 23:10:12 -0700
Subject: [PATCH] ftp: Modernize time functions.

* ftp/Makefile.am (LDADD): Add $(CLOCK_TIME_LIB).
* ftp/extern.h (struct timespec): Add a forward declaration.
(ptransfer): Use timespec instead of timeval.
(tvsub): Remove declaration.
* ftp/ftp.c: Include <float.h> and <timespec.h>
(sendrequest): Use current_timespec instead of gettimeofday.
(recvrequest): Likewise.
(ptransfer): Use timespec_sub instead of tvsub. Divide by FLT_MIN
instead of 1 if seconds is zero.
(tvsub): Remove function.
---
 ftp/Makefile.am |  3 +-
 ftp/extern.h    |  4 +--
 ftp/ftp.c       | 74 +++++++++++++++++++------------------------------
 3 files changed, 33 insertions(+), 48 deletions(-)

diff --git a/ftp/Makefile.am b/ftp/Makefile.am
index 0875eb49..e14e1ecb 100644
--- a/ftp/Makefile.am
+++ b/ftp/Makefile.am
@@ -26,7 +26,8 @@ AM_CPPFLAGS = \
 LDADD = \
 	$(iu_LIBRARIES) \
 	$(LIBREADLINE) $(LIBHISTORY) \
-	$(LIBIDN)
+	$(LIBIDN) \
+	$(CLOCK_TIME_LIB)
 
 bin_PROGRAMS = $(ftp_BUILD)
 
diff --git a/ftp/extern.h b/ftp/extern.h
index 94a90aab..f773ec82 100644
--- a/ftp/extern.h
+++ b/ftp/extern.h
@@ -47,6 +47,7 @@
 
 struct timeval;
 struct fd_set;
+struct timespec;
 
 void abort_remote (FILE *);
 void abortpt (int sig);
@@ -103,7 +104,7 @@ void proxabort (int sig);
 void proxtrans (char *, char *, char *);
 void psabort (int sig);
 void pswitch (int);
-void ptransfer (char *, long long, struct timeval *, struct timeval *);
+void ptransfer (char *, long long, struct timespec *, struct timespec *);
 void put (int, char **);
 void pwd (int, char **);
 void quit (int, char **);
@@ -152,7 +153,6 @@ void site (int, char **);
 void sizecmd (int, char **);
 void status (int, char **);
 void syst (int, char **);
-void tvsub (struct timeval *, struct timeval *, struct timeval *);
 void user (int, char **);
 
 extern jmp_buf abortprox;
diff --git a/ftp/ftp.c b/ftp/ftp.c
index 3f6e9c7b..9ec40a70 100644
--- a/ftp/ftp.c
+++ b/ftp/ftp.c
@@ -70,6 +70,7 @@
 #include <error.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <float.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <signal.h>
@@ -87,6 +88,8 @@
 # include <idna.h>
 #endif
 
+#include <timespec.h>
+
 #include "ftp_var.h"
 #include "attribute.h"
 
@@ -595,7 +598,7 @@ void
 sendrequest (char *cmd, char *local, char *remote, int printnames)
 {
   struct stat st;
-  struct timeval start, stop;
+  struct timespec start, stop;
   int c, d;
   FILE *fin, *dout = 0, *popen (const char *, const char *);
   int (*closefunc) (FILE *);
@@ -766,7 +769,7 @@ sendrequest (char *cmd, char *local, char *remote, int printnames)
       bufsize = blksize;
     }
 
-  gettimeofday (&start, (struct timezone *) 0);
+  start = current_timespec ();
   oldintp = signal (SIGPIPE, SIG_IGN);
   switch (curtype)
     {
@@ -850,7 +853,7 @@ sendrequest (char *cmd, char *local, char *remote, int printnames)
   if (closefunc != NULL)
     (*closefunc) (fin);
   fclose (dout);
-  gettimeofday (&stop, (struct timezone *) 0);
+  stop = current_timespec ();
   getreply (0);
   signal (SIGINT, oldintr);
   if (oldintp)
@@ -878,7 +881,7 @@ abort:
   code = -1;
   if (closefunc != NULL && fin != NULL)
     (*closefunc) (fin);
-  gettimeofday (&stop, (struct timezone *) 0);
+  stop = current_timespec ();
   if (bytes > 0)
     ptransfer ("sent", bytes, &start, &stop);
 }
@@ -908,7 +911,7 @@ recvrequest (char *cmd, char *local, char *remote, char *lmode,
   static int bufsize = 0;
   static char *buf;
   long long bytes = 0, local_hashbytes = hashbytes;
-  struct timeval start, stop;
+  struct timespec start, stop;
 
   is_retr = strcmp (cmd, "RETR") == 0;
   if (is_retr && verbose && printnames)
@@ -1031,7 +1034,7 @@ recvrequest (char *cmd, char *local, char *remote, char *lmode,
       bufsize = blksize;
     }
 
-  gettimeofday (&start, (struct timezone *) 0);
+  start = current_timespec ();
   switch (curtype)
     {
 
@@ -1181,7 +1184,7 @@ recvrequest (char *cmd, char *local, char *remote, char *lmode,
   if (oldintp)
     signal (SIGPIPE, oldintp);
   fclose (din);
-  gettimeofday (&stop, (struct timezone *) 0);
+  stop = current_timespec ();
   getreply (0);
   if (bytes > 0 && is_retr)
     ptransfer ("received", bytes, &start, &stop);
@@ -1211,7 +1214,7 @@ abort:
     (*closefunc) (fout);
   if (din)
     fclose (din);
-  gettimeofday (&stop, (struct timezone *) 0);
+  stop = current_timespec ();
   if (bytes > 0)
     ptransfer ("received", bytes, &start, &stop);
   signal (SIGINT, oldintr);
@@ -1617,51 +1620,32 @@ dataconn (char *lmode)
 }
 
 void
-ptransfer (char *direction, long long int bytes,
-	   struct timeval *t0, struct timeval *t1)
+ptransfer (char *direction, long long int bytes, struct timespec *t0,
+	   struct timespec *t1)
 {
-  struct timeval td;
-  float s, bs;
+  double s, bs;
 
   if (verbose)
     {
-      tvsub (&td, t1, t0);
-      s = td.tv_sec + (td.tv_usec / 1000000.);
-#define nz(x)	((x) == 0 ? 1 : (x))
-      bs = bytes / nz (s);
-
-      printf ("%lld bytes %s in %.3g seconds", bytes, direction, s);
+      struct timespec ts = timespec_sub (*t1, *t0);
+      double seconds = timespectod (ts);
+      double bytes_per_second;
 
-      if (bs > 1048576.0)
-	printf (" (%.3g Mbytes/s)\n", bs / 1048576.0);
-      else if (bs > 1024.0)
-	printf (" (%.3g kbytes/s)\n", bs / 1024.0);
-      else
-	printf (" (%.3g bytes/s)\n", bs);
-    }
-}
-
-/*
-void
-tvadd(tsum, t0)
-	struct timeval *tsum, *t0;
-{
+      /* Don't divide by zero.  Can this happen?  */
+      if (seconds == 0.0)
+	seconds = DBL_MIN;
 
-	tsum->tv_sec += t0->tv_sec;
-	tsum->tv_usec += t0->tv_usec;
-	if (tsum->tv_usec > 1000000)
-		tsum->tv_sec++, tsum->tv_usec -= 1000000;
-}
-*/
+      bytes_per_second = bytes / seconds;
 
-void
-tvsub (struct timeval *tdiff, struct timeval *t1, struct timeval *t0)
-{
+      printf ("%lld bytes %s in %.4f seconds", bytes, direction, seconds);
 
-  tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
-  tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
-  if (tdiff->tv_usec < 0)
-    tdiff->tv_sec--, tdiff->tv_usec += 1000000;
+      if (bytes_per_second > 1048576.0)
+	printf (" (%.4f Mbytes/s)\n", bytes_per_second / 1048576.0);
+      else if (bytes_per_second > 1024.0)
+	printf (" (%.4f kbytes/s)\n", bytes_per_second / 1024.0);
+      else
+	printf (" (%.4f bytes/s)\n", bytes_per_second);
+    }
 }
 
 void
-- 
2.45.2

Reply via email to