[Toybox] [PATCH] Support coreutils extensions in toys/sleep.c

Georgi Chorbadzhiyski gf at unixsol.org
Mon Feb 27 16:28:00 PST 2012


The attached patch adds support for all features supported by coreutils'
sleep program.

-- 
Georgi Chorbadzhiyski
http://georgi.unixsol.org/
-------------- next part --------------
The following patch adds support for all features supported by coreutils'
sleep program.

diff -r 763d581badae toys/sleep.c
--- a/toys/sleep.c	Sun Feb 26 22:04:37 2012 -0600
+++ b/toys/sleep.c	Tue Feb 28 02:18:17 2012 +0200
@@ -3,6 +3,7 @@
  * sleep.c - Wait for a number of seconds.
  *
  * Copyright 2007 Rob Landley <rob at landley.net>
+ * Copyright 2012 Georgi Chorbadzhiyski <georgi at unixsol.org>
  *
  * See http://www.opengroup.org/onlinepubs/009695399/utilities/sleep.html
 
@@ -12,18 +13,31 @@
 	bool "sleep"
 	default y
 	help
-	  usage: sleep SECONDS
+	  usage: sleep [.]SECONDS[SUFFIX]
 
-	  Wait a decimal integer number of seconds.
+	  Wait a decimal integer number of seconds. If seconds is preceded by .
+	  then sleep waits .X seconds. [SUFFIX] can be set to "m" for minutes,
+	  "h" for hours or "d" for days. The default suffix is "s" - seconds.
+	  The dot prefix and suffix are GNU extensions.
 */
 
 #include "toys.h"
 
-DEFINE_GLOBALS(
-	long seconds;
-)
-
 void sleep_main(void)
 {
-	toys.exitval = sleep(atol(*toys.optargs));
+	char *arg = *toys.optargs;
+	unsigned long period;
+	if (arg[0] == '.') {
+		period = (unsigned long)(strtod(arg, NULL) * 1000000);
+		toys.exitval = usleep(period);
+	} else {
+		char suffix = arg[strlen(arg) - 1];
+		period = strtoul(arg, NULL, 10);
+		switch (suffix) {
+			case 'm': period *= 60; break;
+			case 'h': period *= 3600; break;
+			case 'd': period *= 86400; break;
+		}
+		toys.exitval = sleep(period);
+	}
 }


More information about the Toybox mailing list