[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] sleep: allow ms suffix for milliseconds
From: |
Rasmus Villemoes |
Subject: |
[PATCH] sleep: allow ms suffix for milliseconds |
Date: |
Fri, 29 Nov 2019 13:30:36 +0000 |
* src/sleep.c: Accept ms suffix.
* doc/coreutils.texi (sleep invocation): Document it.
When one wants to sleep for some number of milliseconds, one can
do (at least in bash)
sleep $(printf '%d.%03d' $((x/1000)) $((x%1000)))
but that's a bit cumbersome. Extend sleep(1) to also accept "ms" as a
suffix, so one can instead do
sleep ${x}ms
---
doc/coreutils.texi | 4 +++-
src/sleep.c | 59 ++++++++++++++++++++--------------------------
2 files changed, 29 insertions(+), 34 deletions(-)
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 32ddba597..9b2ae431b 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -18225,7 +18225,7 @@ the values of the command line arguments.
Synopsis:
@example
-sleep @var{number}[smhd]@dots{}
+sleep @var{number}[ms|s|m|h|d]@dots{}
@end example
@cindex time units
@@ -18233,6 +18233,8 @@ Each argument is a non-negative number followed by an
optional unit; the default
is seconds. The units are:
@table @samp
+@item ms
+milliseconds
@item s
seconds
@item m
diff --git a/src/sleep.c b/src/sleep.c
index 1c5f7c607..7ea0d6c84 100644
--- a/src/sleep.c
+++ b/src/sleep.c
@@ -45,9 +45,9 @@ usage (int status)
Usage: %s NUMBER[SUFFIX]...\n\
or: %s OPTION\n\
Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),\n\
-'m' for minutes, 'h' for hours or 'd' for days. NUMBER need not be an\n\
-integer. Given two or more arguments, pause for the amount of time\n\
-specified by the sum of their values.\n\
+'ms' for milliseconds, 'm' for minutes, 'h' for hours or 'd' for days.\n\
+NUMBER need not be an integer. Given two or more arguments, pause for\n\
+the amount of time specified by the sum of their values.\n\
\n\
"),
program_name, program_name);
@@ -58,35 +58,30 @@ specified by the sum of their values.\n\
exit (status);
}
-/* Given a floating point value *X, and a suffix character, SUFFIX_CHAR,
- scale *X by the multiplier implied by SUFFIX_CHAR. SUFFIX_CHAR may
- be the NUL byte or 's' to denote seconds, 'm' for minutes, 'h' for
- hours, or 'd' for days. If SUFFIX_CHAR is invalid, don't modify *X
- and return false. Otherwise return true. */
+/* Given a floating point value *X, and a suffix, SUFFIX, scale *X by
+ the multiplier implied by SUFFIX. SUFFIX may be "ms" to denote
+ milliseconds, the empty string or "s" for seconds, "m" for minutes,
+ "h" for hours, or "d" for days. If SUFFIX is invalid, don't modify
+ *X and return false. Otherwise return true. */
static bool
-apply_suffix (double *x, char suffix_char)
+apply_suffix (double *x, const char *suffix)
{
- int multiplier;
-
- switch (suffix_char)
- {
- case 0:
- case 's':
- multiplier = 1;
- break;
- case 'm':
- multiplier = 60;
- break;
- case 'h':
- multiplier = 60 * 60;
- break;
- case 'd':
- multiplier = 60 * 60 * 24;
- break;
- default:
- return false;
- }
+ double multiplier;
+
+ if (STREQ (suffix, "") || STREQ (suffix, "s"))
+ return true;
+
+ if (STREQ (suffix, "ms"))
+ multiplier = 0.001;
+ else if (STREQ (suffix, "m"))
+ multiplier = 60;
+ else if (STREQ (suffix, "h"))
+ multiplier = 60 * 60;
+ else if (STREQ (suffix, "d"))
+ multiplier = 60 * 60 * 24;
+ else
+ return false;
*x *= multiplier;
@@ -124,10 +119,8 @@ main (int argc, char **argv)
if (! (xstrtod (argv[i], &p, &s, cl_strtod) || errno == ERANGE)
/* Nonnegative interval. */
|| ! (0 <= s)
- /* No extra chars after the number and an optional s,m,h,d char. */
- || (*p && *(p+1))
- /* Check any suffix char and update S based on the suffix. */
- || ! apply_suffix (&s, *p))
+ /* Check any suffix and update S based on the suffix. */
+ || ! apply_suffix (&s, p))
{
error (0, 0, _("invalid time interval %s"), quote (argv[i]));
ok = false;
--
2.23.0
- [PATCH] sleep: allow ms suffix for milliseconds,
Rasmus Villemoes <=