1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#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);
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;
default: __builtin_unreachable();
}
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;
}
|