Module Name:    othersrc
Committed By:   simonb
Date:           Thu Feb 25 08:42:31 UTC 2021

Modified Files:
        othersrc/usr.bin/sleepto: sleepto.c

Log Message:
Adapt rev 1.23 of bin/sleep/sleep.c:

  add SIGINFO support; from freebsd:

  when a SIGINFO is delivered, display the approximate remaining seconds.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 othersrc/usr.bin/sleepto/sleepto.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/usr.bin/sleepto/sleepto.c
diff -u othersrc/usr.bin/sleepto/sleepto.c:1.1 othersrc/usr.bin/sleepto/sleepto.c:1.2
--- othersrc/usr.bin/sleepto/sleepto.c:1.1	Thu Feb 25 07:03:57 2021
+++ othersrc/usr.bin/sleepto/sleepto.c	Thu Feb 25 08:42:31 2021
@@ -1,21 +1,34 @@
-/* $NetBSD: sleepto.c,v 1.1 2021/02/25 07:03:57 simonb Exp $ */
+/* $NetBSD: sleepto.c,v 1.2 2021/02/25 08:42:31 simonb Exp $ */
 
+#include <err.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
 #include <unistd.h>
 
+#include "parsetime.h"
+
 #define TIMEFMT	"%R"
 
 static void	usage(void);
 
-extern time_t	parsetime(int, char **);
+static volatile sig_atomic_t report_requested;
+static void
+report_request(int signo __unused)
+{
+
+	report_requested = 1;
+}
+
 
 int
 main(int argc, char *argv[])
 {
+	struct timespec ntime;
 	struct timeval tv;
-	time_t when, now;
+	time_t when, now, original;
+	int rv;
 
 	if (argc < 2)
 		usage();
@@ -30,7 +43,27 @@ main(int argc, char *argv[])
 	now = tv.tv_sec;
 
 	printf("sleeping for %ld seconds...\n", when - now);
-	sleep(when - now);
+	ntime.tv_sec = when - now;
+	if (ntime.tv_sec <= 0)
+		return EXIT_SUCCESS;
+	ntime.tv_nsec = 0;
+
+	original = ntime.tv_sec;
+	signal(SIGINFO, report_request);
+	while ((rv = nanosleep(&ntime, &ntime)) != 0) {
+		if (report_requested) {
+			/* Reporting does not bother with nanoseconds. */
+			warnx("about %ld second%s left out of the original %ld",
+			    ntime.tv_sec,
+			    ntime.tv_sec == 1 ? "" : "s",
+			    original);
+			report_requested = 0;
+		} else
+			break;
+	}
+
+	if (rv == -1)
+ 		err(EXIT_FAILURE, "nanosleep failed");
 
 	return EXIT_SUCCESS;
 }

Reply via email to