summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-09-08 17:46:53 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-09-08 17:46:53 +0200
commitcbde5993107646f14ae85382e474c4e7216263cb (patch)
tree416a4c3c2fd8ae4367dde3abd54db717a08042cc
parentdcf17f88c617abe4d75b65666a78cca396e5d10e (diff)
mslaap
-rw-r--r--src/mslaap.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/mslaap.c b/src/mslaap.c
new file mode 100644
index 0000000..4b32974
--- /dev/null
+++ b/src/mslaap.c
@@ -0,0 +1,46 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "util/option.h"
+
+static const char *usage_string =
+ "Gebruik: %s <microseconden>n"
+ "\n"
+ "Slaap voor het gegeven aantal microseconden.\n"
+ "\n"
+ " -h Toon deze hulptekst\n"
+ " -V Toon versienummer\n";
+
+static char** parse_options(int argc, char **argv) {
+ const struct option_spec spec[] = {
+ {'h', OPTION_HELPUSAGE(usage_string)},
+ {'V', OPTION_VERSION()},
+ OPTION_SPEC_END
+ };
+
+ return option_parse(argc, argv, spec);
+}
+
+int entry_mslaap(int argc, char **argv) {
+ char **args = parse_options(argc, argv);
+
+ if (*args == NULL) {
+ fprintf(stderr, "mslaap: geen hoeveelheid microseconden opgegeven\n");
+ return 1;
+ }
+
+ errno = 0;
+ const useconds_t us = strtoul(*args, NULL, 10);
+ if (errno != 0) {
+ fprintf(stderr, "mslaap: ongeldig aantal microseconden opgegeven\n");
+ return 1;
+ }
+
+ if (usleep(us) != 0) {
+ fprintf(stderr, "mslaap: fout tijdens slapen\n");
+ return 1;
+ }
+ return 0;
+}