-
Notifications
You must be signed in to change notification settings - Fork 10
/
slice.c
58 lines (49 loc) · 984 Bytes
/
slice.c
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
/*
* slice up octet streams -- 2017 Neale Pickett <[email protected]>
*
* This file is in the public domain. I make no promises about the functionality
* of this program.
*/
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
int argn = 1;
unsigned long int drop = true;
unsigned long int next;
unsigned long int pos;
if (1 == argc) {
fprintf(stderr, "Usage: %s start [end start...] [end]\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "Slices input, keeping specified ranges\n");
return 1;
}
next = strtoul(argv[argn], NULL, 0);
for (pos = 0; ; pos += 1) {
int c = getchar();
if (EOF == c) {
break;
}
if (next == pos) {
drop = !drop;
argn += 1;
if (argn == argc) {
if (drop) {
break;
}
next = ULONG_MAX;
} else {
next = strtoul(argv[argn], NULL, 0);
}
}
if (drop) {
/* drop */
} else {
putchar(c);
}
}
return 0;
}