~vdupras/collapseos

ref: ac3629b817115b1af75096856ee9499cfbbd3392 collapseos/tools/blkunpack.c -rw-r--r-- 1.7 KiB
ac3629b8Virgil Dupras Make BLK@* and BLK!* into ialiases 1 year, 10 months ago
                                                                                
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

/* Unpacks blkfs into its source form.
 *
 * If numerical "upto" is specified, we stop unpacking when reaching this
 * blkno.
 */
void usage()
{
    fprintf(stderr, "Usage: blkunpack [upto] < blkfs > blk.fs\n");
}

int main(int argc, char *argv[])
{
    char buf[1024];
    int blkid = 0;
    int upto = 0;
    if (argc > 2) {
        usage();
        return 1;
    }
    if (argc == 2) {
        upto = strtol(argv[1], NULL, 10);
    }
    while (fread(buf, 1024, 1, stdin) == 1) {
        int linecnt = 0 ;
        for (int i=1023; i>=0; i--) {
            if (buf[i]) {
                linecnt = (i / 64) + 1;
                break;
            }
        }
        if (linecnt) {
            // not an empty block
            printf("( ----- %03d )\n", blkid);
            for (int i=0; i<linecnt; i++) {
                char *line = &buf[i*64];
                // line length is *not* strlen()! it's the
                // position of the first non-null, starting
                // from the right. Then, we normalize nulls
                // to space.
                int j;
                for (j=63; j>=0; j--) {
                    if (line[j] != '\0') {
                        break;
                    }
                }
                int len = j+1;
                if (len) {
                    for (; j>=0; j--) {
                        if (line[j] == '\0') {
                            line[j] = ' ';
                        }
                    }
                    fwrite(line, len, 1, stdout);
                }
                fputc('\n', stdout);
            }
        }
        blkid++;
        if (blkid == upto) {
            break;
        }
    }
    return 0;
}