fluffy

Network Archaeology tools for Unix
git clone https://git.woozle.org/neale/fluffy.git

Neale Pickett  ·  2018-06-12

slice.c

 1/*
 2 * slice up octet streams -- 2017 Neale Pickett <zephyr@dirtbags.net>
 3 *
 4 * This file is in the public domain.  I make no promises about the functionality
 5 * of this program. 
 6 */
 7
 8#include <limits.h>
 9#include <stdbool.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13int
14main(int argc, char *argv[])
15{
16	int argn = 1;
17	unsigned long int drop = true;
18	unsigned long int next;
19	unsigned long int pos;
20
21	if (1 == argc) {
22		fprintf(stderr, "Usage: %s start [end start...] [end]\n", argv[0]);
23		fprintf(stderr, "\n");
24		fprintf(stderr, "Slices input, keeping specified ranges\n");
25		return 1;
26	}
27
28	next = strtoul(argv[argn], NULL, 0);
29
30	for (pos = 0; ; pos += 1) {
31		int c = getchar();
32
33		if (EOF == c) {
34			break;
35		}
36
37		if (next == pos) {
38			drop = !drop;
39		    argn += 1;
40			if (argn == argc) {
41				if (drop) {
42					break;
43				}
44				next = ULONG_MAX;
45			} else {
46				next = strtoul(argv[argn], NULL, 0);
47			}
48		}
49
50		if (drop) {
51			/* drop */
52		} else {
53			putchar(c);
54		}
55	}
56
57	return 0;
58}