aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Mallory <mallorya@fastmail.com>2022-05-24 07:43:38 +0000
committerStefan Eßer <se@FreeBSD.org>2022-05-24 07:43:38 +0000
commit34978f7edd15ef5aa9c14a6eecb18ae5d6fd8842 (patch)
tree1b38969ef6a10a29c01ad067c3ecff97a84e1971
parentcdcd52d41e246ba1c0fcfad0769bd691487355ef (diff)
downloadsrc-34978f7edd15.tar.gz
src-34978f7edd15.zip
bin/sleep: add support for units other than seconds
The coreutils version of this command accepts a unit designation of s, m, h, or d (for seconds, minutes, hours, days) immediately following the number of (fractional) units to delay. The submitted patch has been modified in one detail: the test meant to detect the presence of the unit modified was not specific (!= 1) and would have accepted a non-numeric initial element or extra characters following the union. The committed version accepts only the number immediately followed by one of the defined unit designators and no further characters. PR: 264162 MFC after: 1 week
-rw-r--r--bin/sleep/sleep.112
-rw-r--r--bin/sleep/sleep.c18
2 files changed, 25 insertions, 5 deletions
diff --git a/bin/sleep/sleep.1 b/bin/sleep/sleep.1
index 924bc732a0d0..3ce82936a0e7 100644
--- a/bin/sleep/sleep.1
+++ b/bin/sleep/sleep.1
@@ -40,13 +40,21 @@
.Nd suspend execution for an interval of time
.Sh SYNOPSIS
.Nm
-.Ar seconds
+.Ar number[unit]
.Sh DESCRIPTION
The
.Nm
command
suspends execution for a minimum of
-.Ar seconds .
+.Ar number
+seconds (the default, or unit
+.Ar s ) ,
+or minutes (unit
+.Ar m ) ,
+hours (unit
+.Ar h ) ,
+or days (unit
+.Ar d ) .
.Pp
If the
.Nm
diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c
index b4b54a336100..8f383e43c479 100644
--- a/bin/sleep/sleep.c
+++ b/bin/sleep/sleep.c
@@ -66,6 +66,7 @@ main(int argc, char *argv[])
struct timespec time_to_sleep;
double d;
time_t original;
+ char unit;
char buf[2];
if (caph_limit_stdio() < 0 || caph_enter() < 0)
@@ -74,8 +75,17 @@ main(int argc, char *argv[])
if (argc != 2)
usage();
- if (sscanf(argv[1], "%lf%1s", &d, buf) != 1)
- usage();
+ if (sscanf(argv[1], "%lf%c%1s", &d, &unit, buf) == 2)
+ switch(unit) {
+ case 'd': d *= 24;
+ case 'h': d *= 60;
+ case 'm': d *= 60;
+ case 's': break;
+ default: usage();
+ }
+ else
+ if (sscanf(argv[1], "%lf%1s", &d, buf) != 1)
+ usage();
if (d > INT_MAX)
usage();
if (d <= 0)
@@ -106,6 +116,8 @@ static void
usage(void)
{
- fprintf(stderr, "usage: sleep seconds\n");
+ fprintf(stderr, "usage: sleep number[unit]\n");
+ fprintf(stderr, "Unit can be 's' (seconds, the default), "
+ "m (minutes), h (hours), or d (days).\n");
exit(1);
}