summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/slaap.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/slaap.c b/src/slaap.c
new file mode 100644
index 0000000..f818cd5
--- /dev/null
+++ b/src/slaap.c
@@ -0,0 +1,50 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "util/option.h"
+
+static const char *usage_string =
+ "Gebruik: %s <seconden>\n"
+ "\n"
+ "Slaap voor het gegeven aantal seconden.\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_slaap(int argc, char **argv) {
+ char **args = parse_options(argc, argv);
+
+ if (*args == NULL) {
+ fprintf(stderr, "slaap: geen hoeveelheid seconden opgegeven\n");
+ return 1;
+ }
+
+ errno = 0;
+ const double secs = strtod(*args, NULL);
+ if (errno != 0) {
+ fprintf(stderr, "slaap: ongeldig aantal seconden opgegeven\n");
+ return 1;
+ }
+ if (secs < 0) {
+ fprintf(stderr, "slaap: negatief tijdsinterval opgegeven\n");
+ return 1;
+ }
+
+ if (usleep(secs * 1e6 + 0.5) != 0) {
+ fprintf(stderr, "slaap: fout tijdens slapen\n");
+ return 1;
+ }
+ return 0;
+}