Replace "drop" with more general "slice"

This commit is contained in:
Neale Pickett 2017-08-10 15:09:58 +00:00
parent 17104abf80
commit eeddb3cb9a
4 changed files with 75 additions and 57 deletions

View File

@ -6,7 +6,7 @@ TARGETS += pyesc
TARGETS += xor
TARGETS += unhex
TARGETS += pcat
TARGETS += drop
TARGETS += slice
TARGETS += hex
all: $(TARGETS)

View File

@ -75,21 +75,19 @@ The "-x" option treats values as hex.
cbcbcb
### drop: discard octets
### slice: slice octet stream
Throws away some octets from stdin,
and sends the rest to stdout.
Slices up input octet stream,
similar to Python's slice operation.
You could use `dd` for the same purpose.
$ echo 01234567 | drop 0 3
34567
$ echo 01234567 | drop 4 7
01237
$ echo 01234567 | drop 4 6
012367
$ echo 01234567 | drop 3 9999
012
~/src/fluffy $ printf '0123456789abcdef' | slice 2; echo
23456789abcdef
~/src/fluffy $ printf '0123456789abcdef' | slice 2 6; echo
2345
~/src/fluffy $ printf '0123456789abcdef' | slice 2 6 8; echo
234589abcdef
~/src/fluffy $ printf '0123456789abcdef' | slice 2 6 8 0xa
234589
### pcat: print text representation of pcap file
@ -133,6 +131,14 @@ writing to output.
The opposite of `unhex`:
encoding all input into a single output line.
This differs from `hexdump` in the following ways:
* All input is encoded into a single line of output
* Does not output offsets
* Does not output glyph representations of octets
In other words: you can feed `hex` output into `unhex` with no manipulations.
$ printf "hello\nworld\n" | hex
68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a

43
drop.c
View File

@ -1,43 +0,0 @@
/*
* drop octets -- 2017 Neale Pickett <zephyr@dirtbags.net>
*
* This file is in the public domain. I make no promises about the functionality
* of this program.
*/
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
unsigned long int start;
unsigned long int end;
unsigned long int pos;
if (3 == argc) {
start = strtoul(argv[1], NULL, 0);
end = strtoul(argv[2], NULL, 0);
} else {
fprintf(stderr, "Usage: %s start end\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "Drops octets from input\n");
return 1;
}
for (pos = 0; ; pos += 1) {
int c = getchar();
if (EOF == c) {
break;
}
if ((start <= pos) && (pos < end)) {
/* drop */
} else {
putchar(c);
}
}
return 0;
}

55
slice.c Normal file
View File

@ -0,0 +1,55 @@
/*
* slice up octet streams -- 2017 Neale Pickett <zephyr@dirtbags.net>
*
* 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) {
next = ULONG_MAX;
} else {
next = strtoul(argv[argn], NULL, 0);
}
}
if (drop) {
/* drop */
} else {
putchar(c);
}
}
return 0;
}