summary refs log tree commit diff
path: root/.bin/xx.c
blob: 30bf17089797094491c2216064f618a9b915c9d1 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#if 0
exec cc -Weverything -Wno-vla -o ~/.bin/xx $0
#endif

#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static bool zero(const uint8_t *buf, size_t len)
{
    for (size_t i = 0; i < len; ++i)
        if (buf[i]) return false;
    return true;
}

enum {
    ASCII = 1,
    OFFSETS = 2,
    SKIP = 4,
};

int main(int argc, char **argv)
{
    size_t cols = 16;
    size_t group = 8;
    uint8_t flags = ASCII | OFFSETS;
    char *path = NULL;

    while (getopt(argc, argv, "ac:g:os") > 0)
        if (optopt == 'a') {
            flags ^= ASCII;
        } else if (optopt == 'c') {
            cols = (size_t) strtol(optarg, NULL, 10);
            if (!cols) return EXIT_FAILURE;
        } else if (optopt == 'g') {
            group = (size_t) strtol(optarg, NULL, 10);
        } else if (optopt == 'o') {
            flags ^= OFFSETS;
        } else if (optopt == 's') {
            flags ^= SKIP;
        } else return EXIT_FAILURE;
    if (argc > optind)
        path = argv[optind];

    FILE *file = path ? fopen(path, "r") : stdin;
    if (!file) {
        perror(path);
        return EXIT_FAILURE;
    }

    uint8_t buf[cols];
    size_t offset = 0, n = 0, i;
    bool skip = false;
    for (;;) {
        offset += n;
        n = fread(buf, 1, sizeof(buf), file);

        if ((flags & SKIP) && n == sizeof(buf)) {
            if (zero(buf, n)) {
                if (!skip) printf("*\n");
                skip = true;
                continue;
            } else skip = false;
        }

        if (flags & OFFSETS)
            printf("%08zx:  ", offset);
        for (i = 0; i < n; ++i) {
            if (group && i && !(i % group)) printf(" ");
            printf("%02x ", buf[i]);
        }

        if (flags & ASCII) {
            // TODO: Fix alignment with group.
            for (i = n; i < cols; ++i)
                printf("   ");
            printf(" ");
            for (i = 0; i < n; ++i)
                printf("%c", isprint(buf[i]) ? buf[i] : '.');
        }

        printf("\n");
        if (n < sizeof(buf)) break;
    }
    if (ferror(file)) {
        perror(path);
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}