Hello  all,

In attachment you can find an implementation of usleep. It was on the todo list, but on my debian I couldn't find a trace of it. Although its behavior can be mimiced by using the floating point-version of sleep it's actually more simple to have it functioning as a separate toy (usleep doesn't have the suffixes, nor the floating point input which sleep has).

When sleeping voor one second and one microsecond it translates into the following nanosleep call:

edb@lapedb:~/edb-stuff/toybox/toybox$ strace -e nanosleep ./toybox usleep 1000001
nanosleep({1, 1000}, NULL)              = 0


gr
E.

--
Elie De Brauwer

# HG changeset patch
# User Elie De Brauwer <[email protected]>
# Date 1345028034 -7200
# Node ID 131571cf708c03065d1f1f4417c0bea27b4bcae2
# Parent  3258d9233753139f94db8812778e3894337f3d19
Adding usleep

diff -r 3258d9233753 -r 131571cf708c toys/usleep.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toys/usleep.c	Wed Aug 15 12:53:54 2012 +0200
@@ -0,0 +1,32 @@
+/* vi: set sw=4 ts=4:
+ *
+ * usleep.c - Wait for a number of microseconds.
+ *
+ * Copyright 2012 Elie De Brauwer <[email protected]>
+ *
+ * No standard.
+
+USE_USLEEP(NEWTOY(usleep, "<1", TOYFLAG_BIN))
+
+config USLEEP
+	bool "usleep"
+	default y
+	help
+	  usage: usleep MICROSECONDS
+
+	  Pause for MICROSECONDS microseconds.
+
+*/
+
+#include "toys.h"
+
+void usleep_main(void)
+{
+    struct timespec tv;
+    long delay = atol(*toys.optargs);
+    
+    tv.tv_sec = delay/1000000;
+    tv.tv_nsec = (delay%1000000) * 1000;
+    toys.exitval = !!nanosleep(&tv, NULL);
+
+}
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to