commit 249e86082c4c66897f8ef49bccce5e34950cd3f5
parent b26b6ca20289f22698b0fa830d665e6df85ea977
Author: Byron Torres <b@torresjrjr.com>
Date: Wed, 15 Sep 2021 08:45:46 +0100
sleep: new command
Diffstat:
2 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -8,6 +8,7 @@ utils=\
false \
head \
nl \
+ sleep \
tee \
true \
uname
@@ -27,6 +28,7 @@ cat: cat.ha main/main.ha
false: false.ha
head: head.ha
nl: nl.ha
+sleep: sleep.ha
tee: tee.ha
true: true.ha
uname: uname.ha
diff --git a/sleep.ha b/sleep.ha
@@ -0,0 +1,30 @@
+use getopt;
+use main;
+use os;
+use strconv;
+use time;
+
+export fn utilmain() (void | main::error) = {
+ const help: [_]getopt::help = [
+ "suspend execution for an interval",
+ "<seconds>",
+ ];
+ const cmd = getopt::parse(os::args, help...);
+ defer getopt::finish(&cmd);
+
+ if (len(cmd.args) != 1) {
+ usage(help);
+ };
+
+ const seconds = match (strconv::stou(cmd.args[0])) {
+ (strconv::invalid | strconv::overflow) => usage(help),
+ s: uint => s: int,
+ };
+
+ time::sleep(seconds * time::SECOND);
+};
+
+@noreturn fn usage(help: []getopt::help) void = {
+ getopt::printusage(os::stderr, os::args[0], help);
+ os::exit(1);
+};