From b65266f94024bc6a4c8c76c544fd09aceb43862a Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Fri, 19 Apr 2019 16:01:58 +0200
Subject: [PATCH] kernel: Change timespec{add,sub}() to take 3 args, a la
 FreeBSD r336914.

---
 games/tetris/input.h                          | 12 --------
 sys/dev/acpica/acpi_cmbat.c                   |  2 +-
 sys/dev/acpica/acpi_smbat.c                   |  2 +-
 sys/dev/acpica/acpi_thermal.c                 |  2 +-
 sys/dev/disk/isp/isp_freebsd.c                |  5 ++--
 sys/dev/drm/include/linux/time.h              |  4 +--
 sys/kern/kern_clock.c                         |  2 +-
 sys/kern/kern_sig.c                           |  6 ++--
 sys/netproto/smb/smb_iod.c                    |  6 ++--
 sys/netproto/smb/smb_trantcp.c                |  9 +++---
 sys/opencrypto/crypto.c                       |  2 +-
 sys/sys/time.h                                | 28 ++++++++----------
 test/testcases/posixipc/common/common.c       |  6 ++--
 test/testcases/posixipc/common/common.h       | 29 -------------------
 .../kq_connect_client/kq_connect_client.c     | 14 +--------
 .../kq_sendrecv/kq_sendcli/kq_sendcli.c       | 28 +++---------------
 usr.bin/who/utmpentry.c                       | 24 ---------------
 usr.sbin/powerd/powerd.c                      | 22 +++-----------
 18 files changed, 42 insertions(+), 161 deletions(-)

diff --git a/games/tetris/input.h b/games/tetris/input.h
index 351fa6bcf5..eb1d1f1cd1 100644
--- a/games/tetris/input.h
+++ b/games/tetris/input.h
@@ -35,18 +35,6 @@
  *	@(#)input.h	8.1 (Berkeley) 5/31/93
  */
 
-/* Operations on timespecs. */
-#define	timespecclear(tsp)	(tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
-#define	timespecsub(tsp, usp, vsp)					\
-	do {								\
-		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
-		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
-		if ((vsp)->tv_nsec < 0) {				\
-			(vsp)->tv_sec--;				\
-			(vsp)->tv_nsec += 1000000000L;			\
-		}							\
-	} while (/* CONSTCOND */ 0)
-
 int rwait(struct timespec *);
 int tgetchar(void);
 void tsleep(void);
diff --git a/sys/dev/acpica/acpi_cmbat.c b/sys/dev/acpica/acpi_cmbat.c
index 1f636430be..ae32aed962 100644
--- a/sys/dev/acpica/acpi_cmbat.c
+++ b/sys/dev/acpica/acpi_cmbat.c
@@ -222,7 +222,7 @@ acpi_cmbat_info_expired(struct timespec *lastupdated)
 	return (TRUE);
 
     getnanotime(&curtime);
-    timespecsub(&curtime, lastupdated);
+    timespecsub(&curtime, lastupdated, &curtime);
     return (curtime.tv_sec < 0 ||
 	    curtime.tv_sec > acpi_battery_get_info_expire());
 }
diff --git a/sys/dev/acpica/acpi_smbat.c b/sys/dev/acpica/acpi_smbat.c
index 37672d205d..3b4a7aaf97 100644
--- a/sys/dev/acpica/acpi_smbat.c
+++ b/sys/dev/acpica/acpi_smbat.c
@@ -171,7 +171,7 @@ acpi_smbat_info_expired(struct timespec *lastupdated)
 		return (TRUE);
 
 	getnanotime(&curtime);
-	timespecsub(&curtime, lastupdated);
+	timespecsub(&curtime, lastupdated, &curtime);
 	return (curtime.tv_sec < 0 ||
 	    curtime.tv_sec > acpi_battery_get_info_expire());
 }
diff --git a/sys/dev/acpica/acpi_thermal.c b/sys/dev/acpica/acpi_thermal.c
index 12a91aa436..caf7f8a864 100644
--- a/sys/dev/acpica/acpi_thermal.c
+++ b/sys/dev/acpica/acpi_thermal.c
@@ -570,7 +570,7 @@ acpi_tz_monitor(void *Context)
 	(newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
 
 	getnanotime(&curtime);
-	timespecsub(&curtime, &sc->tz_cooling_started);
+	timespecsub(&curtime, &sc->tz_cooling_started, &curtime);
 	if (curtime.tv_sec < acpi_tz_min_runtime)
 	    newactive = sc->tz_active;
     }
diff --git a/sys/dev/disk/isp/isp_freebsd.c b/sys/dev/disk/isp/isp_freebsd.c
index 7696ed9262..a004552b28 100644
--- a/sys/dev/disk/isp/isp_freebsd.c
+++ b/sys/dev/disk/isp/isp_freebsd.c
@@ -5595,8 +5595,9 @@ uint64_t
 isp_nanotime_sub(struct timespec *b, struct timespec *a)
 {
 	uint64_t elapsed;
-	struct timespec x = *b;
-	timespecsub(&x, a);
+	struct timespec x;
+
+	timespecsub(b, a, &x);
 	elapsed = GET_NANOSEC(&x);
 	if (elapsed == 0)
 		elapsed++;
diff --git a/sys/dev/drm/include/linux/time.h b/sys/dev/drm/include/linux/time.h
index 4cba112697..b2f2211794 100644
--- a/sys/dev/drm/include/linux/time.h
+++ b/sys/dev/drm/include/linux/time.h
@@ -75,9 +75,7 @@ timespec_sub(struct timespec lhs, struct timespec rhs)
 {
 	struct timespec ts;
 
-	ts.tv_sec = lhs.tv_sec;
-	ts.tv_nsec = lhs.tv_nsec;
-	timespecsub(&ts, &rhs);
+	timespecsub(&lhs, &rhs, &ts);
 
 	return ts;
 }
diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c
index dc937c52aa..0071a0c537 100644
--- a/sys/kern/kern_clock.c
+++ b/sys/kern/kern_clock.c
@@ -1646,7 +1646,7 @@ pps_event(struct pps_state *pps, sysclock_t count, int event)
 	*tsp = ts;
 
 	if (foff) {
-		timespecadd(tsp, osp);
+		timespecadd(tsp, osp, tsp);
 		if (tsp->tv_nsec < 0) {
 			tsp->tv_nsec += 1000000000;
 			tsp->tv_sec -= 1;
diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
index ff59d8eb0e..934d9844ea 100644
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -1747,8 +1747,7 @@ kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
 		    timeout->tv_nsec < 1000000000) {
 			timevalid = 1;
 			getnanouptime(&rts);
-		 	ets = rts;
-			timespecadd(&ets, timeout);
+			timespecadd(&rts, timeout, &ets);
 		}
 	}
 
@@ -1793,8 +1792,7 @@ kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
 				error = EAGAIN;
 				break;
 			}
-			ts = ets;
-			timespecsub(&ts, &rts);
+			timespecsub(&ets, &rts, &ts);
 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
 			hz = tvtohz_high(&tv);
 		} else {
diff --git a/sys/netproto/smb/smb_iod.c b/sys/netproto/smb/smb_iod.c
index ccef727579..9f5590fdc5 100644
--- a/sys/netproto/smb/smb_iod.c
+++ b/sys/netproto/smb/smb_iod.c
@@ -574,9 +574,9 @@ smb_iod_sendall(struct smbiod *iod)
 			break;
 		    case SMBRQ_SENT:
 			SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
-			timespecadd(&tstimeout, &tstimeout);
+			timespecadd(&tstimeout, &tstimeout, &tstimeout);
 			getnanotime(&ts);
-			timespecsub(&ts, &tstimeout);
+			timespecsub(&ts, &tstimeout, &ts);
 			if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
 				smb_iod_rqprocessed(rqp, ETIMEDOUT);
 			}
@@ -647,7 +647,7 @@ smb_iod_main(struct smbiod *iod)
 #if 0
 	if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
 		getnanotime(&tsnow);
-		timespecsub(&tsnow, &iod->iod_pingtimo);
+		timespecsub(&tsnow, &iod->iod_pingtimo, &tsnow);
 		if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
 			smb_smb_echo(vcp, &iod->iod_scred);
 		}
diff --git a/sys/netproto/smb/smb_trantcp.c b/sys/netproto/smb/smb_trantcp.c
index 27e18d16c8..de6468c3bb 100644
--- a/sys/netproto/smb/smb_trantcp.c
+++ b/sys/netproto/smb/smb_trantcp.c
@@ -473,15 +473,14 @@ smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
 	if (error)
 		return error;
 	getnanotime(&ts2);
-	timespecsub(&ts2, &ts1);
+	timespecsub(&ts2, &ts1, &ts2);
 	if (ts2.tv_sec == 0) {
 		ts2.tv_sec = 1;
 		ts2.tv_nsec = 0;
 	}
-	nbp->nbp_timo = ts2;
-	timespecadd(&nbp->nbp_timo, &ts2);
-	timespecadd(&nbp->nbp_timo, &ts2);
-	timespecadd(&nbp->nbp_timo, &ts2);	/*  * 4 */
+	timespecadd(&ts2, &ts2, &nbp->nbp_timo);
+	timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
+	timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);	/*  * 4 */
 	error = nbssn_rq_request(nbp, td);
 	if (error)
 		smb_nbst_disconnect(vcp, td);
diff --git a/sys/opencrypto/crypto.c b/sys/opencrypto/crypto.c
index 2666b72b0f..b4e32c21ca 100644
--- a/sys/opencrypto/crypto.c
+++ b/sys/opencrypto/crypto.c
@@ -1027,7 +1027,7 @@ crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
 		t.tv_sec--;
 		t.tv_nsec += 1000000000;
 	}
-	timespecadd(&ts->acc, &t);
+	timespecadd(&ts->acc, &t, &ts->acc);
 	if (timespeccmp(&t, &ts->min, <))
 		ts->min = t;
 	if (timespeccmp(&t, &ts->max, >))
diff --git a/sys/sys/time.h b/sys/sys/time.h
index d3c9ed59d2..967dfeb310 100644
--- a/sys/sys/time.h
+++ b/sys/sys/time.h
@@ -67,8 +67,6 @@ struct timezone {
 #define	DST_EET		5	/* Eastern European dst */
 #define	DST_CAN		6	/* Canada */
 
-#ifdef _KERNEL
-
 /* Operations on timespecs */
 #define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
 #define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
@@ -76,22 +74,22 @@ struct timezone {
 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
 	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#define timespecadd(vvp, uvp)						\
+#define	timespecadd(tsp, usp, vsp)					\
 	do {								\
-		(vvp)->tv_sec += (uvp)->tv_sec;				\
-		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
-		if ((vvp)->tv_nsec >= 1000000000) {			\
-			(vvp)->tv_sec++;				\
-			(vvp)->tv_nsec -= 1000000000;			\
+		(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;		\
+		(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;	\
+		if ((vsp)->tv_nsec >= 1000000000L) {			\
+			(vsp)->tv_sec++;				\
+			(vsp)->tv_nsec -= 1000000000L;			\
 		}							\
 	} while (0)
-#define timespecsub(vvp, uvp)						\
+#define	timespecsub(tsp, usp, vsp)					\
 	do {								\
-		(vvp)->tv_sec -= (uvp)->tv_sec;				\
-		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
-		if ((vvp)->tv_nsec < 0) {				\
-			(vvp)->tv_sec--;				\
-			(vvp)->tv_nsec += 1000000000;			\
+		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
+		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
+		if ((vsp)->tv_nsec < 0) {				\
+			(vsp)->tv_sec--;				\
+			(vsp)->tv_nsec += 1000000000L;			\
 		}							\
 	} while (0)
 
@@ -106,8 +104,6 @@ struct timezone {
 
 /* timevaladd and timevalsub are not inlined */
 
-#endif /* _KERNEL */
-
 #ifndef _KERNEL			/* NetBSD/OpenBSD compatible interfaces */
 
 #define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
diff --git a/test/testcases/posixipc/common/common.c b/test/testcases/posixipc/common/common.c
index fc97e6bf6a..d806b2b296 100644
--- a/test/testcases/posixipc/common/common.c
+++ b/test/testcases/posixipc/common/common.c
@@ -118,7 +118,7 @@ testwait(sem_t *id, u_int *delta)
 		perror("clock_gettime(CLOCK_REALTIME)");
 		return (-1);
 	}
-	timespecsub(&end, &start);
+	timespecsub(&end, &start, &end);
 	*delta = end.tv_nsec / 1000000;
 	*delta += end.tv_sec * 1000;
 	return (0);
@@ -268,7 +268,7 @@ timedwait(sem_t *id, u_int msec, u_int *delta, int error)
 	}
 	end.tv_sec = msec / 1000;
 	end.tv_nsec = msec % 1000 * 1000000;
-	timespecadd(&end, &start);
+	timespecadd(&end, &start, &end);
 	if (sem_timedwait(id, &end) < 0) {
 		if (errno != error) {
 			perror("sem_timedwait");
@@ -281,7 +281,7 @@ timedwait(sem_t *id, u_int msec, u_int *delta, int error)
 		perror("clock_gettime(CLOCK_REALTIME)");
 		return (-1);
 	}
-	timespecsub(&end, &start);
+	timespecsub(&end, &start, &end);
 	*delta = end.tv_nsec / 1000000;
 	*delta += end.tv_sec * 1000;
 	return (0);
diff --git a/test/testcases/posixipc/common/common.h b/test/testcases/posixipc/common/common.h
index e5c3d37c39..2c97ae0d88 100644
--- a/test/testcases/posixipc/common/common.h
+++ b/test/testcases/posixipc/common/common.h
@@ -51,35 +51,6 @@ extern sem_t *alarm_id;
 extern int alarm_errno;
 extern int alarm_handler_installed;
 
-/* Cut and pasted from kernel header, bah! */
-
-/* Operations on timespecs */
-#define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
-#define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
-#define	timespeccmp(tvp, uvp, cmp)					\
-	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
-	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
-	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#define timespecadd(vvp, uvp)						\
-	do {								\
-		(vvp)->tv_sec += (uvp)->tv_sec;				\
-		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
-		if ((vvp)->tv_nsec >= 1000000000) {			\
-			(vvp)->tv_sec++;				\
-			(vvp)->tv_nsec -= 1000000000;			\
-		}							\
-	} while (0)
-#define timespecsub(vvp, uvp)						\
-	do {								\
-		(vvp)->tv_sec -= (uvp)->tv_sec;				\
-		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
-		if ((vvp)->tv_nsec < 0) {				\
-			(vvp)->tv_sec--;				\
-			(vvp)->tv_nsec += 1000000000;			\
-		}							\
-	} while (0)
-
-
 /* Macros for passing child status to parent over a pipe. */
 #define	CSTAT(class, error)		((class) << 16 | (error))
 #define	CSTAT_CLASS(stat)		((stat) >> 16)
diff --git a/tools/tools/netrate/accept_connect/kq_connect_client/kq_connect_client.c b/tools/tools/netrate/accept_connect/kq_connect_client/kq_connect_client.c
index 6b1957c4ae..fe4d5332a1 100644
--- a/tools/tools/netrate/accept_connect/kq_connect_client/kq_connect_client.c
+++ b/tools/tools/netrate/accept_connect/kq_connect_client/kq_connect_client.c
@@ -19,18 +19,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#ifndef timespecsub
-#define timespecsub(vvp, uvp)                                           \
-	do {                                                            \
-		(vvp)->tv_sec -= (uvp)->tv_sec;                         \
-		(vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
-		if ((vvp)->tv_nsec < 0) {                               \
-			(vvp)->tv_sec--;                                \
-			(vvp)->tv_nsec += 1000000000;                   \
-		}                                                       \
-	} while (0)
-#endif
-
 static void	mainloop(const struct sockaddr_in *, int, int, long, u_long *,
 		    int, int);
 
@@ -310,7 +298,7 @@ mainloop(const struct sockaddr_in *in, int in_cnt, int nconn_max,
 	}
 	clock_gettime(CLOCK_MONOTONIC_PRECISE, &end);
 
-	timespecsub(&end, &start);
+	timespecsub(&end, &start, &end);
 	time_us = ((double)end.tv_sec * 1000000.0) +
 	    ((double)end.tv_nsec / 1000.0);
 
diff --git a/tools/tools/netrate/kq_sendrecv/kq_sendcli/kq_sendcli.c b/tools/tools/netrate/kq_sendrecv/kq_sendcli/kq_sendcli.c
index bc9b0e4faf..884be47c1d 100644
--- a/tools/tools/netrate/kq_sendrecv/kq_sendcli/kq_sendcli.c
+++ b/tools/tools/netrate/kq_sendrecv/kq_sendcli/kq_sendcli.c
@@ -42,25 +42,6 @@
  * start roughly at the same time.
  */
 
-#ifndef timespecsub
-#define timespecsub(vvp, uvp)						\
-	do {								\
-		(vvp)->tv_sec -= (uvp)->tv_sec;				\
-		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
-		if ((vvp)->tv_nsec < 0) {				\
-			(vvp)->tv_sec--;				\
-			(vvp)->tv_nsec += 1000000000;			\
-		}							\
-	} while (0)
-#endif
-
-#ifndef timespeccmp
-#define	timespeccmp(tvp, uvp, cmp)					\
-	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
-	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
-	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#endif
-
 #if 0
 #define SEND_DEBUG
 #endif
@@ -425,8 +406,7 @@ main(int argc, char *argv[])
 		ctx = &ctx_arr[i];
 		pthread_join(ctx->t_tid, NULL);
 
-		run = ctx->t_end;
-		timespecsub(&run, &ctx->t_start);
+		timespecsub(&ctx->t_end, &ctx->t_start, &run);
 		ctx->t_run_us = ((double)run.tv_sec * 1000000.0) +
 		    ((double)run.tv_nsec / 1000.0);
 
@@ -452,8 +432,7 @@ main(int argc, char *argv[])
 	    start.tv_sec, start.tv_nsec, end.tv_sec, end.tv_nsec);
 #endif
 
-	run = end;
-	timespecsub(&run, &start);
+	timespecsub(&end, &start, &run);
 	total_run_us = ((double)run.tv_sec * 1000000.0) +
 	    ((double)run.tv_nsec / 1000.0);
 	total = 0.0;
@@ -508,7 +487,8 @@ main(int argc, char *argv[])
 				continue;
 
 			run = conn->c_terr;
-			timespecsub(&run, &ctx_arr[conn->c_thr_id].t_start);
+			timespecsub(&conn->c_terr,
+			    &ctx_arr[conn->c_thr_id].t_start, &run);
 			tmp_run = ((double)run.tv_sec * 1000000.0) +
 			    ((double)run.tv_nsec / 1000.0);
 			fprintf(stderr, "snd%d ->%s:%d, %ld sec, %.2lf Mbps, "
diff --git a/usr.bin/who/utmpentry.c b/usr.bin/who/utmpentry.c
index b4f23b8fe1..af245dc144 100644
--- a/usr.bin/who/utmpentry.c
+++ b/usr.bin/who/utmpentry.c
@@ -48,30 +48,6 @@
 #include "utmpentry.h"
 
 /* Operations on timespecs. */
-#define	timespecclear(tsp)	(tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
-#define	timespecisset(tsp)	((tsp)->tv_sec || (tsp)->tv_nsec)
-#define	timespeccmp(tsp, usp, cmp)					\
-	(((tsp)->tv_sec == (usp)->tv_sec) ?				\
-	    ((tsp)->tv_nsec cmp (usp)->tv_nsec) :			\
-	    ((tsp)->tv_sec cmp (usp)->tv_sec))
-#define	timespecadd(tsp, usp, vsp)					\
-	do {								\
-		(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;		\
-		(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;	\
-		if ((vsp)->tv_nsec >= 1000000000L) {			\
-			(vsp)->tv_sec++;				\
-			(vsp)->tv_nsec -= 1000000000L;			\
-		}							\
-	} while (/* CONSTCOND */ 0)
-#define	timespecsub(tsp, usp, vsp)					\
-	do {								\
-		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
-		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
-		if ((vsp)->tv_nsec < 0) {				\
-			(vsp)->tv_sec--;				\
-			(vsp)->tv_nsec += 1000000000L;			\
-		}							\
-	} while (/* CONSTCOND */ 0)
 #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
 
 
diff --git a/usr.sbin/powerd/powerd.c b/usr.sbin/powerd/powerd.c
index 2e1c82564b..0975d73972 100644
--- a/usr.sbin/powerd/powerd.c
+++ b/usr.sbin/powerd/powerd.c
@@ -696,18 +696,6 @@ usage(void)
 	exit(1);
 }
 
-#ifndef timespecsub
-#define timespecsub(vvp, uvp)						\
-	do {								\
-		(vvp)->tv_sec -= (uvp)->tv_sec;				\
-		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
-		if ((vvp)->tv_nsec < 0) {				\
-			(vvp)->tv_sec--;				\
-			(vvp)->tv_nsec += 1000000000;			\
-		}							\
-	} while (0)
-#endif
-
 #define BAT_SYSCTL_TIME_MAX	50000000 /* unit: nanosecond */
 
 static int
@@ -727,7 +715,7 @@ has_battery(void)
 	}
 	clock_gettime(CLOCK_MONOTONIC_FAST, &e);
 
-	timespecsub(&e, &s);
+	timespecsub(&e, &s, &e);
 	if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
 		/* hw.acpi.acline takes to long to be useful */
 		syslog(LOG_NOTICE, "hw.acpi.acline takes too long");
@@ -742,7 +730,7 @@ has_battery(void)
 	}
 	clock_gettime(CLOCK_MONOTONIC_FAST, &e);
 
-	timespecsub(&e, &s);
+	timespecsub(&e, &s, &e);
 	if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
 		/* hw.acpi.battery.life takes to long to be useful */
 		syslog(LOG_NOTICE, "hw.acpi.battery.life takes too long");
@@ -795,8 +783,7 @@ mon_battery(void)
 	size_t len;
 
 	clock_gettime(CLOCK_MONOTONIC_FAST, &cur);
-	ts = cur;
-	timespecsub(&ts, &BatLifePrevT);
+	timespecsub(&cur, &BatLifePrevT, &ts);
 	if (ts.tv_sec < BatLifePollIntvl)
 		return 1;
 	BatLifePrevT = cur;
@@ -852,8 +839,7 @@ mon_battery(void)
 		return 1;
 
 	if (BatShutdownLinger > 0) {
-		ts = cur;
-		timespecsub(&ts, &BatShutdownStartT);
+		timespecsub(&cur, &BatShutdownStartT, &ts);
 		if (ts.tv_sec > BatShutdownLinger)
 			BatShutdownLinger = 0;
 	}
-- 
2.21.0

