[PATCH] [time_cmd] Add 'tryfor' command

Shao Miller shao.miller at yrdsb.edu.on.ca
Sun Mar 28 12:09:40 EDT 2010


Usage:
  tryfor <seconds> <command>

Keep trying <command> up to a maximum of
<second> seconds. 0 == forever.

Uncompresed code size cost: 226 bytes

Suggested-by: Jonathan Andrews <jon at jonshouse.co.uk>
Suggested-by: Piotr Jaroszynski <p.jaroszynski at gmail.com>
---
 src/hci/commands/time_cmd.c |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 src/hci/commands/time_cmd.c

diff --git a/src/hci/commands/time_cmd.c b/src/hci/commands/time_cmd.c
old mode 100644
new mode 100755
index 947410e..e736b87
--- a/src/hci/commands/time_cmd.c
+++ b/src/hci/commands/time_cmd.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2009 Daniel Verkamp <daniel at drv.nu>.
+ * Portions copyright (C) 2010 Shao Miller <shao.miller at yrdsb.edu.on.ca>.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -17,6 +18,9 @@
  *
  * March-19-2009 @ 02:44: Added sleep command.
  * Shao Miller <shao.miller at yrdsb.edu.on.ca>.
+ *
+ * March-28-2010 @ 11:33: Added tryfor command.
+ * Shao Miller <shao.miller at yrdsb.edu.on.ca>.
  */
 
 #include <stdio.h>
@@ -82,3 +86,29 @@ struct command sleep_command __command = {
 	.name = "sleep",
 	.exec = sleep_exec,
 };
+
+static int tryfor_exec ( int argc, char **argv ) {
+	unsigned long start, timeout;
+	int rc;
+
+	if ( argc < 3 ) {
+		printf ( "Usage:\n"
+			 "  %s <seconds> <command>\n"
+			 "\n"
+			 "Keep trying <command> up to a maximum of <second> "
+			 "seconds. 0 == forever\n",
+			 argv[0] );
+		return 1;
+	}
+	start = currticks();
+	timeout = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
+	while ( ( ( rc = execv ( argv[2], &argv[2] ) ) != 0 ) &&
+		( ( timeout == 0 ) ||
+		  ( ( currticks() - start ) <= timeout ) ) ) {};
+	return rc;
+}
+
+struct command tryfor_command __command = {
+	.name = "tryfor",
+	.exec = tryfor_exec,
+};
-- 
1.5.3.4


--------------000909040007020108020506--


More information about the gPXE mailing list