-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseq.c
More file actions
53 lines (44 loc) · 1 KB
/
seq.c
File metadata and controls
53 lines (44 loc) · 1 KB
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
#include <unistd.h>
static char num_buf[32];
static inline void write_num(int n) {
char *p = num_buf + 31;
*p = '\0';
if (n == 0) {
*--p = '0';
} else {
while (n > 0) {
*--p = '0' + (n % 10);
n /= 10;
}
}
while (*p) {
write(1, p, 1);
p++;
}
}
static int parse_int(const char *s) {
int result = 0;
while (*s >= '0' && *s <= '9') {
result = result * 10 + (*s - '0');
s++;
}
return result;
}
int main(int argc, char **argv) {
int start = 1, end = 10, step = 1;
if (argc == 2) {
end = parse_int(argv[1]);
} else if (argc == 3) {
start = parse_int(argv[1]);
end = parse_int(argv[2]);
} else if (argc == 4) {
start = parse_int(argv[1]);
step = parse_int(argv[2]);
end = parse_int(argv[3]);
}
for (int i = start; i <= end; i += step) {
write_num(i);
write(1, "\n", 1);
}
return 0;
}