diff options
| author | Tom Smeding <tom@tomsmeding.com> | 2026-03-16 15:45:38 +0100 |
|---|---|---|
| committer | Tom Smeding <tom@tomsmeding.com> | 2026-03-16 15:45:38 +0100 |
| commit | 9384587cb62e9cd9741a2093bc1d48fff022fb39 (patch) | |
| tree | 446f37867711d97ae21feee8a6b905a11ea9d03b | |
| parent | f19b257e18eec038f720f950c52e7ec0805efd16 (diff) | |
slaap
| -rw-r--r-- | src/slaap.c | 50 |
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; +} |
