comparison ffstools/tiffs-rd/cat.c @ 240:acedd4c88d2e

tiffs cat command implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 27 Jan 2014 02:32:46 +0000
parents
children c95efd27fb2e
comparison
equal deleted inserted replaced
239:28ea957a9d8a 240:acedd4c88d2e
1 /*
2 * This C module implements the cat and catino commands.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <unistd.h>
11 #include "types.h"
12 #include "struct.h"
13 #include "globals.h"
14 #include "pathname.h"
15
16 int cat_mode;
17
18 static u8 hex_buf[16];
19 static int hex_fill;
20 static u32 hex_offset;
21
22 cat_hex_flush()
23 {
24 int i, c;
25
26 printf("%08X: ", hex_offset);
27 for (i = 0; i < 16; i++) {
28 if (i < hex_fill)
29 printf("%02X ", hex_buf[i]);
30 else
31 fputs(" ", stdout);
32 if (i == 7 || i == 15)
33 putchar(' ');
34 }
35 for (i = 0; i < hex_fill; i++) {
36 c = hex_buf[i];
37 if (c < ' ' || c > '~')
38 c = '.';
39 putchar(c);
40 }
41 putchar('\n');
42 }
43
44 cat_hex_byte(inb)
45 {
46 hex_buf[hex_fill++] = inb;
47 if (hex_fill >= 16) {
48 cat_hex_flush();
49 hex_offset += hex_fill;
50 hex_fill = 0;
51 }
52 }
53
54 void
55 cat_chunk(ch)
56 struct chunkinfo *ch;
57 {
58 u8 *p;
59 int c;
60
61 if (!ch->len)
62 return;
63 switch (cat_mode) {
64 case 0:
65 write(1, ch->start, ch->len);
66 return;
67 case 1:
68 for (p = ch->start; p < ch->end; p++) {
69 c = *p;
70 if (c >= ' ' && c <= '~' || c == '\n')
71 putchar(c);
72 else {
73 if (c & 0x80) {
74 putchar('M');
75 putchar('-');
76 c &= 0x7F;
77 }
78 putchar('^');
79 if (c == 0x7F)
80 putchar('?');
81 else
82 putchar(c + '@');
83 }
84 }
85 return;
86 case 2:
87 for (p = ch->start; p < ch->end; p++)
88 cat_hex_byte(*p);
89 return;
90 }
91 }
92
93 void
94 cat_finish()
95 {
96 switch (cat_mode) {
97 case 1:
98 putchar('\n');
99 return;
100 case 2:
101 if (hex_fill)
102 cat_hex_flush();
103 return;
104 }
105 }
106
107 static void
108 segment_cat_callback(inf, opaque)
109 struct inode_info *inf;
110 u_long opaque;
111 {
112 struct chunkinfo chi;
113
114 size_extra_chunk(inf, &chi);
115 cat_chunk(&chi);
116 }
117
118 cmd_cat(argc, argv)
119 char **argv;
120 {
121 extern int optind;
122 int c, headino;
123 struct inode_info *inf;
124 struct chunkinfo chi;
125
126 optind = 0;
127 while ((c = getopt(argc, argv, "hv")) != EOF)
128 switch (c) {
129 case 'h':
130 cat_mode = 2;
131 continue;
132 case 'v':
133 cat_mode = 1;
134 continue;
135 default:
136 usage: fprintf(stderr, "usage: cat [-v|-h] pathname\n");
137 exit(1);
138 }
139 if (argc != optind + 1)
140 goto usage;
141
142 read_ffs_image();
143 find_inode_block();
144 alloc_inode_table();
145 find_root_inode();
146
147 headino = find_pathname(argv[optind]);
148 inf = inode_info[headino];
149 switch (inf->type) {
150 case 0xE1:
151 case 0xF1:
152 break;
153 case 0xF2:
154 fprintf(stderr, "error: the requested object is a directory\n");
155 exit(1);
156 case 0xF3:
157 fprintf(stderr,
158 "error: the requested object is a symlink; use readlink instead of cat\n");
159 exit(1);
160 default:
161 fprintf(stderr, "error: unexpected object type %02X\n",
162 inf->type);
163 exit(1);
164 }
165 size_head_chunk(inf, &chi);
166 cat_chunk(&chi);
167 iterate_seg_file(headino, segment_cat_callback, 0L, 0, 0);
168 cat_finish();
169 exit(0);
170 }