annotate mpffs/cat.c @ 37:8256eec598dd

mpffs-cat: more sensible handling of -v
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 30 Jun 2013 17:56:27 +0000
parents ee4c761187cf
children 7ceab8bfacb3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module contains the main function and other code specific to mpffs-cat
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <ctype.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdio.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <string.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <strings.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <unistd.h>
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include "types.h"
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include "struct.h"
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 extern char *imgfile;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 extern int verbose;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17
37
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
18 int cat_v;
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
19
35
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 cat_chunk(chi)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 struct chunkinfo *chi;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 u8 *dp;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 size_t len;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 int c;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 dp = chi->start;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 len = chi->len;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 while (len) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 c = *dp++;
37
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
31 if (!cat_v || c >= ' ' && c <= '~' || c == '\n')
35
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 putchar(c);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 else {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 if (c & 0x80) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 putchar('M');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 putchar('-');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 c &= 0x7F;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 putchar('^');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 if (c == 0x7F)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 putchar('?');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 else
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 putchar(c + '@');
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 len--;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 cat_file(headidx)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 int ent;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 struct objinfo obj;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 struct chunkinfo chi;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 obj.entryno = headidx;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 get_index_entry(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 if (obj.type != 0xF1) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 fprintf(stderr,
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 "mpffs-cat: the requested FFS object is not a regular file\n");
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 exit(1);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 validate_chunk(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 size_head_chunk(&obj, &chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 cat_chunk(&chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 for (ent = obj.descend; ent != 0xFFFF; ent = obj.descend) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 obj.entryno = ent;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 get_index_entry(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 if (obj.type != 0xF4) {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 fprintf(stderr,
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 "file continuation object at index %x: type %02X != expected F4\n",
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 ent, obj.type);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 exit(1);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 validate_chunk(&obj);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 size_extra_chunk(&obj, &chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 cat_chunk(&chi);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 }
37
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
78 if (cat_v)
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
79 putchar('\n');
35
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 usage()
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 fprintf(stderr, "usage: mpffs-cat [options] ffs-image pathname\n");
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 exit(1);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 }
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 main(argc, argv)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 char **argv;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 {
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 extern int optind;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 int idx;
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 parse_cmdline_options(argc, argv);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 if (argc - optind != 2)
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 usage();
37
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
97 if (verbose) {
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
98 cat_v = 1;
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
99 verbose--;
8256eec598dd mpffs-cat: more sensible handling of -v
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 35
diff changeset
100 }
35
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 imgfile = argv[optind];
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 preliminaries();
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 idx = find_pathname(argv[optind+1]);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 cat_file(idx);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 exit(0);
ee4c761187cf mpffs-cat implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 }