commit db101f60ec24a988a10c0d8cf68a921fa0907e25
parent bda0c09745bb127f9622f8413d91459a33634d94
Author: Louis Burda <quent.burda@gmail.com>
Date: Sat, 3 Jul 2021 17:24:41 +0200
Add option to reverse bit order per byte
Diffstat:
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/bitcat.c b/bitcat.c
@@ -5,7 +5,7 @@
#define ISFLAG(str, fshort, flong) (!strcmp(str, fshort) || !strcmp(str, flong))
-const char *usage = "USAGE: bitcat [-s SKIP] [-n COUNT] FILE\n";
+const char *usage = "USAGE: bitcat [-r] [-s SKIP] [-n COUNT] FILE\n";
void
die(const char *fmtstr, ...)
@@ -25,14 +25,14 @@ main(int argc, const char **argv)
unsigned char byte, bit;
size_t skip, size, pos;
const char *filepath;
+ int i, revbyte;
char *end;
FILE *f;
- int i;
if (argc <= 1) die(usage);
filepath = NULL;
- size = skip = 0;
+ revbyte = size = skip = 0;
for (i = 1; i < argc; i++) {
if (ISFLAG(argv[i], "-s", "--skip")) {
if (i++ == argc - 1) goto missing_arg;
@@ -42,6 +42,8 @@ main(int argc, const char **argv)
if (i++ == argc - 1) goto missing_arg;
size = strtol(argv[i], &end, 0);
if (end && *end) goto bad_arg;
+ } else if (ISFLAG(argv[i], "-r", "--revb")) {
+ revbyte = 1;
} else if (ISFLAG(argv[i], "-h", "--help")) {
die(usage);
} else if (*argv[i] == '-') {
@@ -67,7 +69,10 @@ main(int argc, const char **argv)
for (pos = 0; pos < size + skip && !feof(f); pos++) {
if (pos % 8 == 0) byte = fgetc(f);
- bit = (byte >> (pos % 8)) & 1;
+ if (revbyte)
+ bit = (byte >> (7 - pos % 8)) & 1;
+ else
+ bit = (byte >> (pos % 8)) & 1;
if (pos >= skip) putchar(bit ? '1' : '0');
}