summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-07-22 21:10:47 +0200
committerTom Smeding <tom@tomsmeding.com>2024-07-22 21:20:02 +0200
commitbaa5edb354dddf921c6558b648a79d28eb2bc094 (patch)
treee071c62c2ec2515b9bdaba7fb48da3803b1d9ee5
parent84598634562c998f07d879e85ac5ee1509a101b5 (diff)
Voeg rij toe
-rw-r--r--src/rij.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/rij.c b/src/rij.c
new file mode 100644
index 0000000..1ea1b35
--- /dev/null
+++ b/src/rij.c
@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <unistd.h>
+#include <errno.h>
+#include "util/versie.h"
+
+
+static void usage(FILE *f) {
+ fprintf(f,
+ "Gebruik: rij [-hV] [EERSTE] [STAP] LAATSTE\n"
+ "\n"
+ "Druk getallen van EERSTE t/m LAATSTE af, in stappen van STAP.\n"
+ "\n"
+ " -h Toon deze hulptekst\n"
+ " -V Toon versienummer\n");
+}
+
+// Returns pointer to argument array containing the file names
+static char** parse_options(int argc, char **argv) {
+ int opt;
+ while ((opt = getopt(argc, argv, "hV")) != -1) {
+ switch (opt) {
+ case 'h':
+ usage(stdout);
+ exit(0);
+
+ case 'V':
+ drukkedoos_print_versie(stdout, "rij");
+ exit(0);
+
+ case '?':
+ fprintf(stderr, "Ongeldige optie: -%c\n", optopt);
+ usage(stderr);
+ exit(1);
+ }
+ }
+
+ return argv + optind;
+}
+
+int entry_rij(int argc, char **argv) {
+ char **args = parse_options(argc, argv);
+
+ // Use 'long long' because that's what strtoll() parses
+ long long argnums[3], nnums = 0;
+ while (*args && nnums < 3) {
+ char *endp;
+ errno = 0;
+ argnums[nnums++] = strtoll(*args, &endp, 10);
+ if (errno == ERANGE) {
+ fprintf(stderr, "rij: Getal te groot: %s\n", *args);
+ return 1;
+ }
+ if (!**args || *endp) {
+ fprintf(stderr, "rij: Ongeldig getal '%s'\n", *args);
+ return 1;
+ }
+ args++;
+ }
+
+ if (nnums == 0) {
+ fprintf(stderr, "rij: Missend argument\n");
+ return 1;
+ }
+
+ if (nnums == 3 && *args) {
+ fprintf(stderr, "rij: Teveel argumenten\n");
+ return 1;
+ }
+
+ long long start, step, end;
+ switch (nnums) {
+ case 1: start = step = 1; end = argnums[0]; break;
+ case 2: start = argnums[0]; step = 1; end = argnums[1]; break;
+ case 3: start = argnums[0]; step = argnums[1]; end = argnums[2]; break;
+ }
+
+ const long long safeend = end == LLONG_MAX ? LLONG_MAX - 1 : end;
+
+ for (long long i = start; i <= safeend; i += step) {
+ printf("%lld\n", i);
+ }
+
+ if (end == LLONG_MAX) printf("%lld\n", LLONG_MAX);
+
+ return 0;
+}