annotate mpffs/ls.c @ 285:e5b0164f6060

compal/c156-fw-disasm: first attempt
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 14 Mar 2019 09:06:43 +0000
parents 9bc7f0e03fa8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module contains the main function and other code specific to mpffs-ls
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <ctype.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdio.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <string.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <strings.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <unistd.h>
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include "types.h"
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include "struct.h"
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 extern char *imgfile;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 extern struct objinfo root;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 extern int verbose;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 char workpath[512];
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20
39
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
21 void
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
22 extra_chunk_callback(obj, opaque)
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
23 struct objinfo *obj;
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
24 u_long opaque;
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
25 {
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
26 size_t *accump = (size_t *) opaque;
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
27 struct chunkinfo chi;
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
28
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
29 size_extra_chunk(obj, &chi);
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
30 *accump += chi.len;
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
31 }
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
32
31
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
33 size_t
30
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 get_file_size(head)
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 struct objinfo *head;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 {
31
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
37 struct chunkinfo chi;
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
38 size_t accum;
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
39
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
40 size_head_chunk(head, &chi);
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
41 accum = chi.len;
39
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
42 iterate_extra_chunks(head->descend, extra_chunk_callback,
9bc7f0e03fa8 iterate_extra_chunks() function written, mpffs-ls converted to use it
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 32
diff changeset
43 (u_long) &accum);
31
3cca8070ef0f mpffs-ls reports file sizes
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 30
diff changeset
44 return(accum);
30
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 }
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 dump_dir(firstent, path_prefix)
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 {
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 int ent;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 struct objinfo obj;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 u_long size;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 for (ent = firstent; ent != 0xFFFF; ent = obj.sibling) {
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 obj.entryno = ent;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 get_index_entry(&obj);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 if (!obj.type) /* skip deleted objects w/o further validation */
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 continue;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 validate_chunk(&obj);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 validate_obj_name(&obj);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 if (path_prefix + strlen(obj.dataptr) + 2 > sizeof workpath) {
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 fprintf(stderr,
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 "handling object at index %x, name \"%s\": path buffer overflow\n",
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 obj.entryno, (char *)obj.dataptr);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 exit(1);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 }
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 sprintf(workpath + path_prefix, "/%s", (char *)obj.dataptr);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 switch (obj.type) {
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 case 0xF2:
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 /* directory */
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 printf("d %s\n", workpath);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 dump_dir(obj.descend, strlen(workpath));
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 continue;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 case 0xF1:
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 /* regular file */
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 size = get_file_size(&obj);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 printf("- %7lu %s\n", size, workpath);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 continue;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 case 0xE1:
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 /* special .journal file */
32
0bf037ba4149 mpffs-ls: report the full chunk length of the /.journal file
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 31
diff changeset
80 printf("j %7lu %s\n", (u_long) obj.len, workpath);
30
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 continue;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 default:
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 printf("%s (index entry #%x): unexpected type %02X; skipping\n",
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 workpath, obj.entryno, obj.type);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 continue;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 }
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 }
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 }
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 usage()
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 {
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 fprintf(stderr, "usage: mpffs-ls [options] ffs-image\n");
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 exit(1);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 }
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 main(argc, argv)
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 char **argv;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 {
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 extern int optind;
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 parse_cmdline_options(argc, argv);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 if (argc - optind != 1)
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 usage();
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 imgfile = argv[optind];
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 preliminaries();
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 dump_dir(root.descend, 0);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 exit(0);
9c3c5a572b57 mpffs-ls works with the length code stubbed out
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 }