comparison mpffs/cat.c @ 35:ee4c761187cf

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