CVS commit: src/bin/sleep

2019-03-10 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sun Mar 10 15:18:45 UTC 2019

Modified Files:
src/bin/sleep: sleep.c

Log Message:
Deal with overflow when the sleep duration given is a simple
integer (previously it was just clamped at the max possible value).
This would have caused
sleep 1000
(or anything bigger) to have only actually slept for 9223372036854775807
secs.   Someone would have noticed that happen, one day, in some other
universe.

This is now an error, as it was previously if this had been entered as
sleep 1e19

Also detect an attempt to sleep for so long that a time_t will no longer
be able to represent the current time when the sleep is done.

Undo the attempts to work around a broken kernel nanosleep()
implementation (by only ever issuing shortish sleep requests,
and looping).   That code was broken (idiot botch of mine) though
you would have had to wait a month to observe it happen.  I was going
to just fix it, but sanity prevailed, and the kernel got fixed instead.

That allows this to be much simplified, only looping as needed to
handle dealing with SIGINFO.   Switch to using clock_nanosleep()
to implement the delay, as while our nanosleep() uses CLOCK_MONOTONIC
the standards say it should use CLOCK_REALTIME, and if that we
ever changed that, the old way would alter "sleep 5" from
"sleep for 5 seconds" to "sleep until now + 5 secs", which is
subtly different.

Always use %g format to print the original sleep duration in reports of how
much time remains - this works best for both long and short durations.
A couple of other minor (frill) mods to the SIGINFO report message as well.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.29 src/bin/sleep/sleep.c:1.30
--- src/bin/sleep/sleep.c:1.29	Sun Jan 27 02:00:45 2019
+++ src/bin/sleep/sleep.c	Sun Mar 10 15:18:45 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: sleep.c,v 1.29 2019/01/27 02:00:45 christos Exp $ */
+/* $NetBSD: sleep.c,v 1.30 2019/03/10 15:18:45 kre Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,17 +39,19 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: sleep.c,v 1.29 2019/01/27 02:00:45 christos Exp $");
+__RCSID("$NetBSD: sleep.c,v 1.30 2019/03/10 15:18:45 kre Exp $");
 #endif
 #endif /* not lint */
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -73,9 +75,10 @@ main(int argc, char *argv[])
 	const char *msg;
 	double fval, ival, val;
 	struct timespec ntime;
+	struct timespec endtime;
+	struct timespec now;
 	time_t original;
 	int ch, fracflag;
-	unsigned delay;
 
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
@@ -145,9 +148,15 @@ main(int argc, char *argv[])
 		if (ntime.tv_sec == 0 && ntime.tv_nsec == 0)
 			return EXIT_SUCCESS;	/* was 0.0 or underflowed */
 	} else {
+		errno = 0;
 		ntime.tv_sec = strtol(arg, , 10);
 		if (ntime.tv_sec < 0 || temp == arg || *temp != '\0')
 			usage();
+		if (errno == ERANGE)
+			errx(EXIT_FAILURE, "Requested delay (%s) out of range",
+			arg);
+		else if (errno != 0)
+			err(EXIT_FAILURE, "Requested delay (%s)", arg);
 
 		if (ntime.tv_sec == 0)
 			return EXIT_SUCCESS;
@@ -155,40 +164,45 @@ main(int argc, char *argv[])
 	}
 
 	original = ntime.tv_sec;
-	if (ntime.tv_nsec != 0)
-		msg = " and a bit";
-	else
+	if (original < 86400) {
+		if (ntime.tv_nsec > 10 * 2 / 3) {
+			original++;
+			msg = " less a bit";
+		} else if (ntime.tv_nsec != 0)
+			msg = " and a bit";
+		else
+			msg = "";
+	} else
 		msg = "";
 
+	if (clock_gettime(CLOCK_MONOTONIC, ) != 0)
+		err(EXIT_FAILURE, "clock_gettime");
+	timespecadd(, , );
+
+	if (endtime.tv_sec < now.tv_sec || (endtime.tv_sec == now.tv_sec &&
+	endtime.tv_nsec <= now.tv_nsec))
+		errx(EXIT_FAILURE, "cannot sleep beyond the end of time");
+
 	signal(SIGINFO, report_request);
+	for (;;) {
+		int e;
+
+		if ((e = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
+		 , NULL)) == 0)
+			return EXIT_SUCCESS;
 
-	if (ntime.tv_sec <= 1) {			/* arbitrary */
-		while (nanosleep(, ) != 0) {
-			if (report_requested) {
-report(ntime.tv_sec, original, msg);
-report_requested = 0;
-			} else
-err(EXIT_FAILURE, "nanosleep failed");
+		if (!report_requested || e != EINTR) {
+			errno = e;
+			err(EXIT_FAILURE, "clock_nanotime");
 		}
-	} else while (ntime.tv_sec > 0) {
-		delay = (unsigned int)ntime.tv_sec;
 
-		if ((time_t)delay != ntime.tv_sec || delay > 30 * 86400)
-			delay = 30 * 86400;
+		report_requested = 0;
+		if (clock_gettime(CLOCK_MONOTONIC, ) != 0) /* Huh? */
+			continue;
 
-		ntime.tv_sec -= delay;
-		delay = sleep(delay);
-		ntime.tv_sec += delay;
-
-		if (delay != 0 && 

CVS commit: src/bin/sleep

2019-01-27 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Sun Jan 27 17:42:53 UTC 2019

Modified Files:
src/bin/sleep: sleep.1

Log Message:
Sort sections.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/bin/sleep/sleep.1

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.26 src/bin/sleep/sleep.1:1.27
--- src/bin/sleep/sleep.1:1.26	Sat Jan 26 15:20:50 2019
+++ src/bin/sleep/sleep.1	Sun Jan 27 17:42:53 2019
@@ -1,4 +1,4 @@
-.\"	$NetBSD: sleep.1,v 1.26 2019/01/26 15:20:50 kre Exp $
+.\"	$NetBSD: sleep.1,v 1.27 2019/01/27 17:42:53 wiz Exp $
 .\"
 .\" Copyright (c) 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -149,6 +149,17 @@ The
 command is expected to be
 .St -p1003.2
 compatible.
+.Sh HISTORY
+A
+.Nm
+utility appeared in
+.At v4 .
+Processing fractional seconds, and processing the
+.Ic seconds
+argument respecting the current locale, was added in
+.Nx 1.3 .
+The ability to sleep for extended periods appeared in
+.Nx 9 .
 .Sh BUGS
 This
 .Nm
@@ -164,14 +175,3 @@ individual
 .Nm
 invocation limited to 200 billion years
 approximately.
-.Sh HISTORY
-A
-.Nm
-utility appeared in
-.At v4 .
-Processing fractional seconds, and processing the
-.Ic seconds
-argument respecting the current locale, was added in
-.Nx 1.3 .
-The ability to sleep for extended periods appeared in
-.Nx 9 .



CVS commit: src/bin/sleep

2019-01-26 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jan 27 02:00:45 UTC 2019

Modified Files:
src/bin/sleep: sleep.c

Log Message:
cast to intmax_t instead of long, since time_t is "long long"


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.28 src/bin/sleep/sleep.c:1.29
--- src/bin/sleep/sleep.c:1.28	Sat Jan 26 13:14:22 2019
+++ src/bin/sleep/sleep.c	Sat Jan 26 21:00:45 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: sleep.c,v 1.28 2019/01/26 18:14:22 martin Exp $ */
+/* $NetBSD: sleep.c,v 1.29 2019/01/27 02:00:45 christos Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: sleep.c,v 1.28 2019/01/26 18:14:22 martin Exp $");
+__RCSID("$NetBSD: sleep.c,v 1.29 2019/01/27 02:00:45 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -197,16 +197,16 @@ report(const time_t remain, const time_t
 {
 	if (remain == 0)
 		warnx("In the final moments of the original"
-		" %ld%s second%s", (long)original, msg,
+		" %jd%s second%s", (intmax_t)original, msg,
 		original == 1 && *msg == '\0' ? "" : "s");
 	else if (remain < 2000)
-		warnx("Between %ld and %ld seconds left"
+		warnx("Between %jd and %jd seconds left"
 		" out of the original %g%s",
-		(long)remain, (long)remain + 1, (double)original,
+		(intmax_t)remain, (intmax_t)remain + 1, (double)original,
 		msg);
 	else if ((original - remain) < 10 && (original-remain) < original/8)
-		warnx("Have waited only %d seconds of the original %g",
-			(int)(original - remain), (double)original);
+		warnx("Have waited only %jd seconds of the original %g",
+			(intmax_t)(original - remain), (double)original);
 	else
 		warnx("Approximately %g seconds left out of the original %g",
 			(double)remain, (double)original);



CVS commit: src/bin/sleep

2019-01-26 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sat Jan 26 18:14:22 UTC 2019

Modified Files:
src/bin/sleep: sleep.c

Log Message:
Explicitly cast time_t to match format string - should fix the build on
some 32bit architectures.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.27 src/bin/sleep/sleep.c:1.28
--- src/bin/sleep/sleep.c:1.27	Sat Jan 26 15:20:50 2019
+++ src/bin/sleep/sleep.c	Sat Jan 26 18:14:22 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: sleep.c,v 1.27 2019/01/26 15:20:50 kre Exp $ */
+/* $NetBSD: sleep.c,v 1.28 2019/01/26 18:14:22 martin Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: sleep.c,v 1.27 2019/01/26 15:20:50 kre Exp $");
+__RCSID("$NetBSD: sleep.c,v 1.28 2019/01/26 18:14:22 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -202,7 +202,8 @@ report(const time_t remain, const time_t
 	else if (remain < 2000)
 		warnx("Between %ld and %ld seconds left"
 		" out of the original %g%s",
-		remain, remain + 1, (double)original, msg);
+		(long)remain, (long)remain + 1, (double)original,
+		msg);
 	else if ((original - remain) < 10 && (original-remain) < original/8)
 		warnx("Have waited only %d seconds of the original %g",
 			(int)(original - remain), (double)original);



CVS commit: src/bin/sleep

2019-01-26 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sat Jan 26 15:20:50 UTC 2019

Modified Files:
src/bin/sleep: sleep.1 sleep.c

Log Message:
While cute, the previous version is not really safe.
After all, a system might want to sleep for several
thousand years on a spaceship headed to a distant
solar system...

So, remove the pause() code, deal with limits on the
range (it is just an int) that can be passed to sleep()
by looping, and do a much better job of checking for
out of range input values.

With this change sleep(1) should work for durations
up to something more than 250 billion years.  It
fails (at startup, with an error) if the requested
duration is beyond what can be handled.

Here no changes at all related to locales and arg
parsing.Still for another day.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/bin/sleep/sleep.1
cvs rdiff -u -r1.26 -r1.27 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.25 src/bin/sleep/sleep.1:1.26
--- src/bin/sleep/sleep.1:1.25	Sat Jan 26 15:19:08 2019
+++ src/bin/sleep/sleep.1	Sat Jan 26 15:20:50 2019
@@ -1,4 +1,4 @@
-.\"	$NetBSD: sleep.1,v 1.25 2019/01/26 15:19:08 kre Exp $
+.\"	$NetBSD: sleep.1,v 1.26 2019/01/26 15:20:50 kre Exp $
 .\"
 .\" Copyright (c) 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -61,15 +61,6 @@ Permitting non-integral delays is a non-
 and its use will decrease the probability that
 a shell script will execute properly on another system.
 .Pp
-If the request is for more than a little over 135 years
-.Nm
-will pause forever.
-If the computer is still running when the
-.Nm
-should have terminated,
-.Nm
-will need to be killed by outside intervention.
-.Pp
 When the
 .Dv SIGINFO
 signal is received, an estimate of the number of seconds remaining to
@@ -158,6 +149,21 @@ The
 command is expected to be
 .St -p1003.2
 compatible.
+.Sh BUGS
+This
+.Nm
+command cannot handle requests for durations
+much longer than about 250 billion years.
+Any such attempt will result in an error,
+and immediate termination.
+It is suggested that when there is a need
+for sleeps exceeding this period, the
+.Nm
+command be executed in a loop, with each
+individual
+.Nm
+invocation limited to 200 billion years
+approximately.
 .Sh HISTORY
 A
 .Nm
@@ -167,3 +173,5 @@ Processing fractional seconds, and proce
 .Ic seconds
 argument respecting the current locale, was added in
 .Nx 1.3 .
+The ability to sleep for extended periods appeared in
+.Nx 9 .

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.26 src/bin/sleep/sleep.c:1.27
--- src/bin/sleep/sleep.c:1.26	Sat Jan 26 15:19:08 2019
+++ src/bin/sleep/sleep.c	Sat Jan 26 15:20:50 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: sleep.c,v 1.26 2019/01/26 15:19:08 kre Exp $ */
+/* $NetBSD: sleep.c,v 1.27 2019/01/26 15:20:50 kre Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: sleep.c,v 1.26 2019/01/26 15:19:08 kre Exp $");
+__RCSID("$NetBSD: sleep.c,v 1.27 2019/01/26 15:20:50 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -109,6 +109,8 @@ main(int argc, char *argv[])
 	 * into the floating point conversion path if the input
 	 * is hex (the 'x' in 0xA is not a digit).  Then if
 	 * strtod() handles hex (on NetBSD it does) so will we.
+	 * That path is also taken for scientific notation (1.2e+3)
+	 * and when the input is simply nonsense.
 	 */
 	fracflag = 0;
 	arg = *argv;
@@ -132,19 +134,21 @@ main(int argc, char *argv[])
 			val = strtod_l(arg, , LC_C_LOCALE);
 		if (val < 0 || temp == arg || *temp != '\0')
 			usage();
+
 		ival = floor(val);
 		fval = (10 * (val-ival));
-		if (ival  >= (double)UINT_MAX)
-			ntime.tv_sec = (double)UINT_MAX;
-		else
-			ntime.tv_sec = ival;
+		ntime.tv_sec = ival;
+		if ((double)ntime.tv_sec != ival)
+			errx(1, "requested delay (%s) out of range", arg);
 		ntime.tv_nsec = fval;
+
 		if (ntime.tv_sec == 0 && ntime.tv_nsec == 0)
 			return EXIT_SUCCESS;	/* was 0.0 or underflowed */
 	} else {
 		ntime.tv_sec = strtol(arg, , 10);
 		if (ntime.tv_sec < 0 || temp == arg || *temp != '\0')
 			usage();
+
 		if (ntime.tv_sec == 0)
 			return EXIT_SUCCESS;
 		ntime.tv_nsec = 0;
@@ -166,28 +170,21 @@ main(int argc, char *argv[])
 			} else
 err(EXIT_FAILURE, "nanosleep failed");
 		}
-	} else {
-		delay = (unsigned long)ntime.tv_sec;
+	} else while (ntime.tv_sec > 0) {
+		delay = (unsigned int)ntime.tv_sec;
 
-		if ((time_t)delay != ntime.tv_sec ||
-		delay > UINT_MAX - 86400) {
-			for (;;) {
-pause();
-if (report_requested) {
-	warnx("Waiting for the end of time");
-	report_requested = 0;
-} else
-	break;
-			}
-		} else {
-			while ((delay = sleep(delay)) != 0) {
-		

CVS commit: src/bin/sleep

2019-01-26 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sat Jan 26 15:19:08 UTC 2019

Modified Files:
src/bin/sleep: sleep.1 sleep.c

Log Message:
Adjust the way the arg string is parsed in the "not entirely
integer" case, so we avoid adjusting the locale of sleep,
and generally be more reliable and simpler.

In addition, deal with weirdness in nanosleep() when the
interval gets long, by avoiding using it.  In this version
when the sleep interval < 1 seconds, we use nanosleep()
as before, for delays longer than that we use sleep() instead,
and ignore any fractional seconds.

We avoid overflow problems here by not bothering to sleep
at all for delays longer than 135 years (approx) and simply
pause() instead.   That sleep never terminates in such a
case is unlikely to ever be observed.

This commit makes no decision on the question of whether
the arg should be interpreted in the locale of the user,
or always in the C locale.   That is for another day.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/bin/sleep/sleep.1
cvs rdiff -u -r1.25 -r1.26 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.24 src/bin/sleep/sleep.1:1.25
--- src/bin/sleep/sleep.1:1.24	Mon Jul  3 21:33:24 2017
+++ src/bin/sleep/sleep.1	Sat Jan 26 15:19:08 2019
@@ -1,4 +1,4 @@
-.\"	$NetBSD: sleep.1,v 1.24 2017/07/03 21:33:24 wiz Exp $
+.\"	$NetBSD: sleep.1,v 1.25 2019/01/26 15:19:08 kre Exp $
 .\"
 .\" Copyright (c) 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	@(#)sleep.1	8.3 (Berkeley) 4/18/94
 .\"
-.Dd August 12, 2016
+.Dd January 26, 2019
 .Dt SLEEP 1
 .Os
 .Sh NAME
@@ -44,9 +44,9 @@
 .Sh DESCRIPTION
 The
 .Nm
-utility
-suspends execution for a minimum of
-.Ar seconds .
+utility suspends execution for a minimum of
+.Ar seconds
+seconds, then exits.
 It is usually used to schedule the execution of other commands (see
 .Sx EXAMPLES
 below).
@@ -55,12 +55,24 @@ Note: The
 .Nx
 .Nm
 command will accept and honor a non-integer number of specified seconds.
-This is a non-portable extension, and its use will nearly guarantee that
-a shell script will not execute properly on another system.
+Note however, that if the request is for much more than 2.5 hours,
+any fractional seconds will be ignored.
+Permitting non-integral delays is a non-portable extension,
+and its use will decrease the probability that
+a shell script will execute properly on another system.
+.Pp
+If the request is for more than a little over 135 years
+.Nm
+will pause forever.
+If the computer is still running when the
+.Nm
+should have terminated,
+.Nm
+will need to be killed by outside intervention.
 .Pp
 When the
 .Dv SIGINFO
-signal is received, the estimate of the amount of seconds left to
+signal is received, an estimate of the number of seconds remaining to
 sleep is printed on the standard output.
 .Sh EXIT STATUS
 The
@@ -77,15 +89,16 @@ An error occurred.
 .Sh EXAMPLES
 To schedule the execution of a command for 1800 seconds later:
 .Pp
-.Dl (sleep 1800; sh command_file >& errors)&
+.Dl (sleep 1800; sh command_file >errors 2>&1)&
 .Pp
 This incantation would wait half an hour before
-running the script command_file.
+running the script
+.Dq command_file .
 (See the
 .Xr at 1
 utility.)
 .Pp
-To reiteratively run a command (with
+To repeatedly run a command (using
 .Xr csh 1 ) :
 .Pp
 .Bd -literal -offset indent -compact
@@ -107,12 +120,36 @@ running is taking longer than expected t
 files, and it would be nice to have
 another program start processing the files created by the first
 program as soon as it is finished (when zzz.rawdata is created).
-The script checks every five minutes for the file zzz.rawdata,
-when the file is found, then another portion processing
+The script checks every five minutes for the file zzz.rawdata.
+When the file is found, processing the generated files (*.rawdata)
 is done courteously by sleeping for 70 seconds in between each
 awk job.
+.Pp
+To wait until a particular time, the following,
+with some error checking added, might be used (using
+.Xr sh 1
+on
+.Nx ) :
+.Bd -literal -offset indent
+END=$(( $( date -d "$1" +%s ) - START_TIME ))
+while [ "${SECONDS}" -lt "${END}" ]
+do
+	sleep "$((END - SECONDS))"
+done
+.Ed
+.Pp
+where the argument
+.Sq \&$1
+specifies the desired date and time in any format the
+.Fl d
+option to the
+.Xr date 1
+command accepts.
 .Sh SEE ALSO
 .Xr at 1 ,
+.Xr csh 1 ,
+.Xr date 1 ,
+.Xr sh 1 ,
 .Xr nanosleep 2 ,
 .Xr sleep 3
 .Sh STANDARDS
@@ -126,3 +163,7 @@ A
 .Nm
 utility appeared in
 .At v4 .
+Processing fractional seconds, and processing the
+.Ic seconds
+argument respecting the current locale, was added in
+.Nx 1.3 .

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.25 src/bin/sleep/sleep.c:1.26
--- src/bin/sleep/sleep.c:1.25	Sat 

CVS commit: src/bin/sleep

2019-01-19 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sat Jan 19 13:27:12 UTC 2019

Modified Files:
src/bin/sleep: sleep.c

Log Message:
Allow the decimal radix character '.' to work, regardless of
what the current locale's radix character happens to be,
while still allowing locale specific entry of fractional
seconds (ie: if you're in locale where the radix character
is ',' you san use "sleep 2.5" or "sleep 2,5" and they
accomplish the same thing).

This avoids issues with the "sleep 0.05" in rc.subr which
generated usage messages when a locale that does not use
'.' as its radix character was in use.

Reported on netbsd-users by Dima Veselov, with the problem
diagnosed by Martin Husemann

While here, tighten the arg validity checking (3+4 is
no longer permitted as a synonym of 3) and allow 0.0
to mean the same thing as 0 rather than being an error.

Also, make the SIGINFO reports a little nicer (IMO).

The ATF tests for sleep all pass (not that that means a lot).


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.24 src/bin/sleep/sleep.c:1.25
--- src/bin/sleep/sleep.c:1.24	Mon Aug 29 14:51:19 2011
+++ src/bin/sleep/sleep.c	Sat Jan 19 13:27:12 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: sleep.c,v 1.24 2011/08/29 14:51:19 joerg Exp $ */
+/* $NetBSD: sleep.c,v 1.25 2019/01/19 13:27:12 kre Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: sleep.c,v 1.24 2011/08/29 14:51:19 joerg Exp $");
+__RCSID("$NetBSD: sleep.c,v 1.25 2019/01/19 13:27:12 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -68,6 +68,7 @@ int
 main(int argc, char *argv[])
 {
 	char *arg, *temp;
+	const char *msg;
 	double fval, ival, val;
 	struct timespec ntime;
 	time_t original;
@@ -100,36 +101,71 @@ main(int argc, char *argv[])
 	 * problem. Why use an isdigit() check instead of checking for
 	 * a period? Because doing it this way means locales will be
 	 * handled transparently by the atof code.
+	 *
+	 * Since fracflag is set for any non-digit, we also fall
+	 * into the floating point conversion path if the input
+	 * is hex (the 'x' in 0xA is not a digit).  Then if
+	 * strtod() handles hex (on NetBSD it does) so will we.
 	 */
 	fracflag = 0;
 	arg = *argv;
 	for (temp = arg; *temp != '\0'; temp++)
-		if (!isdigit((unsigned char)*temp))
+		if (!isdigit((unsigned char)*temp)) {
+			ch = *temp;
 			fracflag++;
+		}
 
 	if (fracflag) {
-		val = atof(arg);
-		if (val <= 0)
+		/*
+		 * If the radix char in the arg was a '.'
+		 * (as is likely when used from scripts, etc)
+		 * then force the C locale, so atof() works
+		 * as intended, even if the user's locale
+		 * expects something different, like ','
+		 * (but leave the locale alone otherwise, so if
+		 * the user entered 2,4 and that is correct for
+		 * the locale, it will work).
+		 */
+		if (ch == '.')
+			(void)setlocale(LC_ALL, "C");
+		val = strtod(arg, );
+		if (val < 0 || temp == arg || *temp != '\0')
 			usage();
 		ival = floor(val);
 		fval = (10 * (val-ival));
 		ntime.tv_sec = ival;
 		ntime.tv_nsec = fval;
-	}
-	else {
-		ntime.tv_sec = atol(arg);
-		if (ntime.tv_sec <= 0)
+		if (ntime.tv_sec == 0 && ntime.tv_nsec == 0)
+			return EXIT_SUCCESS;	/* was 0.0 or underflowed */
+	} else {
+		ntime.tv_sec = strtol(arg, , 10);
+		if (ntime.tv_sec < 0 || temp == arg || *temp != '\0')
+			usage();
+		if (ntime.tv_sec == 0)
 			return EXIT_SUCCESS;
 		ntime.tv_nsec = 0;
 	}
 
 	original = ntime.tv_sec;
+	if (ntime.tv_nsec != 0)
+		msg = " and a bit";
+	else
+		msg = "";
+
 	signal(SIGINFO, report_request);
 	while ((rv = nanosleep(, )) != 0) {
 		if (report_requested) {
-		/* Reporting does not bother with nanoseconds. */
-			warnx("about %d second(s) left out of the original %d",
-			(int)ntime.tv_sec, (int)original);
+			/* Reporting does not bother (much) with nanoseconds. */
+			if (ntime.tv_sec == 0)
+			warnx("in the final moments of the original"
+			   " %ld%s second%s", (long)original, msg,
+			   original == 1 && *msg == '\0' ? "" : "s");
+			else
+			warnx("between %ld and %ld seconds left"
+" out of the original %ld%s",
+(long)ntime.tv_sec, (long)ntime.tv_sec + 1,
+(long)original, msg);
+
 			report_requested = 0;
 		} else
 			break;



CVS commit: src/bin/sleep

2016-08-11 Thread Sevan Janiyan
Module Name:src
Committed By:   sevan
Date:   Fri Aug 12 02:36:38 UTC 2016

Modified Files:
src/bin/sleep: sleep.1

Log Message:
Document the version sleep first appeared.
Bump date.


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/bin/sleep/sleep.1

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.22 src/bin/sleep/sleep.1:1.23
--- src/bin/sleep/sleep.1:1.22	Mon Aug 15 14:45:36 2011
+++ src/bin/sleep/sleep.1	Fri Aug 12 02:36:38 2016
@@ -1,4 +1,4 @@
-.\"	$NetBSD: sleep.1,v 1.22 2011/08/15 14:45:36 wiz Exp $
+.\"	$NetBSD: sleep.1,v 1.23 2016/08/12 02:36:38 sevan Exp $
 .\"
 .\" Copyright (c) 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	@(#)sleep.1	8.3 (Berkeley) 4/18/94
 .\"
-.Dd August 13, 2011
+.Dd August 12, 2016
 .Dt SLEEP 1
 .Os
 .Sh NAME
@@ -121,3 +121,8 @@ The
 command is expected to be
 .St -p1003.2
 compatible.
+.Sh HISTORY
+A
+.Nm
+utility appeared in
+.At v4 .



CVS commit: src/bin/sleep

2011-08-15 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Aug 15 14:45:36 UTC 2011

Modified Files:
src/bin/sleep: sleep.1

Log Message:
Improve wording.
From Snader_LB.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/bin/sleep/sleep.1

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.21 src/bin/sleep/sleep.1:1.22
--- src/bin/sleep/sleep.1:1.21	Sat Oct  9 07:40:58 2010
+++ src/bin/sleep/sleep.1	Mon Aug 15 14:45:36 2011
@@ -1,4 +1,4 @@
-.\	$NetBSD: sleep.1,v 1.21 2010/10/09 07:40:58 wiz Exp $
+.\	$NetBSD: sleep.1,v 1.22 2011/08/15 14:45:36 wiz Exp $
 .\
 .\ Copyright (c) 1990, 1993, 1994
 .\	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\
 .\	@(#)sleep.1	8.3 (Berkeley) 4/18/94
 .\
-.Dd October 9, 2010
+.Dd August 13, 2011
 .Dt SLEEP 1
 .Os
 .Sh NAME
@@ -79,13 +79,13 @@
 .Pp
 .Dl (sleep 1800; sh command_file \*[Gt]\*[Am] errors)\*[Am]
 .Pp
-This incantation would wait a half hour before
+This incantation would wait half an hour before
 running the script command_file.
 (See the
 .Xr at 1
 utility.)
 .Pp
-To reiteratively run a command (with the
+To reiteratively run a command (with
 .Xr csh 1 ) :
 .Pp
 .Bd -literal -offset indent -compact



CVS commit: src/bin/sleep

2010-10-09 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Sat Oct  9 07:40:58 UTC 2010

Modified Files:
src/bin/sleep: sleep.1

Log Message:
Bump date for SIGINFO.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/bin/sleep/sleep.1

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.20 src/bin/sleep/sleep.1:1.21
--- src/bin/sleep/sleep.1:1.20	Sat Oct  9 04:57:30 2010
+++ src/bin/sleep/sleep.1	Sat Oct  9 07:40:58 2010
@@ -1,4 +1,4 @@
-.\	$NetBSD: sleep.1,v 1.20 2010/10/09 04:57:30 mrg Exp $
+.\	$NetBSD: sleep.1,v 1.21 2010/10/09 07:40:58 wiz Exp $
 .\
 .\ Copyright (c) 1990, 1993, 1994
 .\	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\
 .\	@(#)sleep.1	8.3 (Berkeley) 4/18/94
 .\
-.Dd April 18, 1994
+.Dd October 9, 2010
 .Dt SLEEP 1
 .Os
 .Sh NAME



CVS commit: src/bin/sleep

2010-10-08 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sat Oct  9 04:57:30 UTC 2010

Modified Files:
src/bin/sleep: sleep.1 sleep.c

Log Message:
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.19 -r1.20 src/bin/sleep/sleep.1
cvs rdiff -u -r1.22 -r1.23 src/bin/sleep/sleep.c

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

Modified files:

Index: src/bin/sleep/sleep.1
diff -u src/bin/sleep/sleep.1:1.19 src/bin/sleep/sleep.1:1.20
--- src/bin/sleep/sleep.1:1.19	Sat Aug 18 00:41:52 2007
+++ src/bin/sleep/sleep.1	Sat Oct  9 04:57:30 2010
@@ -1,4 +1,4 @@
-.\	$NetBSD: sleep.1,v 1.19 2007/08/18 00:41:52 hubertf Exp $
+.\	$NetBSD: sleep.1,v 1.20 2010/10/09 04:57:30 mrg Exp $
 .\
 .\ Copyright (c) 1990, 1993, 1994
 .\	The Regents of the University of California.  All rights reserved.
@@ -57,6 +57,11 @@
 command will accept and honor a non-integer number of specified seconds.
 This is a non-portable extension, and its use will nearly guarantee that
 a shell script will not execute properly on another system.
+.Pp
+When the
+.Dv SIGINFO
+signal is received, the estimate of the amount of seconds left to
+sleep is printed on the standard output.
 .Sh EXIT STATUS
 The
 .Nm

Index: src/bin/sleep/sleep.c
diff -u src/bin/sleep/sleep.c:1.22 src/bin/sleep/sleep.c:1.23
--- src/bin/sleep/sleep.c:1.22	Sun Jul 20 00:52:40 2008
+++ src/bin/sleep/sleep.c	Sat Oct  9 04:57:30 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: sleep.c,v 1.22 2008/07/20 00:52:40 lukem Exp $ */
+/* $NetBSD: sleep.c,v 1.23 2010/10/09 04:57:30 mrg Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = @(#)sleep.c	8.3 (Berkeley) 4/2/94;
 #else
-__RCSID($NetBSD: sleep.c,v 1.22 2008/07/20 00:52:40 lukem Exp $);
+__RCSID($NetBSD: sleep.c,v 1.23 2010/10/09 04:57:30 mrg Exp $);
 #endif
 #endif /* not lint */
 
@@ -57,13 +57,23 @@
 static void usage(void);
 int main(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[])
 {
 	char *arg, *temp;
 	double fval, ival, val;
 	struct timespec ntime;
-	int ch, fracflag;
+	time_t original;
+	int ch, fracflag, rv;
 
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, );
@@ -115,7 +125,19 @@
 		ntime.tv_nsec = 0;
 	}
 
-	if (nanosleep(ntime, NULL) == -1)
+	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 %d second(s) left out of the original %d,
+			(int)ntime.tv_sec, (int)original);
+			report_requested = 0;
+		} else
+			break;
+	}
+
+	if (rv == -1)
 		err(EXIT_FAILURE, nanosleep failed);
 
 	return EXIT_SUCCESS;