aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/sleep.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
committerRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
commit3a9241add947cb6d24b5de7a8927517426a78795 (patch)
treed122ab6570439cd6b17c7d73ed8d4e085e0b8a95 /toys/posix/sleep.c
parent689f095bc976417bf50810fe59a3b3ac32b21105 (diff)
downloadtoybox-3a9241add947cb6d24b5de7a8927517426a78795.tar.gz
Move commands into "posix", "lsb", and "other" menus/directories.
Diffstat (limited to 'toys/posix/sleep.c')
-rw-r--r--toys/posix/sleep.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/toys/posix/sleep.c b/toys/posix/sleep.c
new file mode 100644
index 00000000..ef6d827b
--- /dev/null
+++ b/toys/posix/sleep.c
@@ -0,0 +1,51 @@
+/* vi: set sw=4 ts=4:
+ *
+ * sleep.c - Wait for a number of seconds.
+ *
+ * Copyright 2007 Rob Landley <rob@landley.net>
+ * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
+ *
+ * See http://www.opengroup.org/onlinepubs/009695399/utilities/sleep.html
+
+USE_SLEEP(NEWTOY(sleep, "<1", TOYFLAG_BIN))
+
+config SLEEP
+ bool "sleep"
+ default y
+ help
+ usage: sleep SECONDS
+
+ Wait before exiting.
+
+config SLEEP_FLOAT
+ bool
+ default y
+ depends on SLEEP && TOYBOX_FLOAT
+ help
+ The delay can be a decimal fraction. An optional suffix can be "m"
+ (minutes), "h" (hours), "d" (days), or "s" (seconds, the default).
+*/
+
+#include "toys.h"
+
+void sleep_main(void)
+{
+
+ if (!CFG_TOYBOX_FLOAT) toys.exitval = sleep(atol(*toys.optargs));
+ else {
+ char *arg;
+ double d = strtod(*toys.optargs, &arg);
+ struct timespec tv;
+
+ // Parse suffix
+ if (*arg) {
+ int ismhd[]={1,60,3600,86400};
+ char *smhd = "smhd", *c = strchr(smhd, *arg);
+ if (!c) error_exit("Unknown suffix '%c'", *arg);
+ d *= ismhd[c-smhd];
+ }
+
+ tv.tv_nsec=1000000000*(d-(tv.tv_sec = (unsigned long)d));
+ toys.exitval = !!nanosleep(&tv, NULL);
+ }
+}