summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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;
+}