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#include "libbb.h"
66#include "dump.h"
67
68
69
70static void bb_dump_addfile(dumper_t *dumper, char *name)
71{
72 char *p;
73 FILE *fp;
74 char *buf;
75
76 fp = xfopen_for_read(name);
77 while ((buf = xmalloc_fgetline(fp)) != NULL) {
78 p = skip_whitespace(buf);
79 if (*p && (*p != '#')) {
80 bb_dump_add(dumper, p);
81 }
82 free(buf);
83 }
84 fclose(fp);
85}
86
87static const char *const add_strings[] = {
88 "\"%07.7_ax \"16/1 \"%03o \"\"\n\"",
89 "\"%07.7_ax \"16/1 \"%3_c \"\"\n\"",
90 "\"%07.7_ax \"8/2 \" %05u \"\"\n\"",
91 "\"%07.7_ax \"8/2 \" %06o \"\"\n\"",
92 "\"%07.7_ax \"8/2 \" %04x \"\"\n\"",
93};
94
95static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
96
97static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
98
99int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
100int hexdump_main(int argc, char **argv)
101{
102 dumper_t *dumper = alloc_dumper();
103 const char *p;
104 int ch;
105#if ENABLE_FEATURE_HEXDUMP_REVERSE
106 FILE *fp;
107 smallint rdump = 0;
108#endif
109
110 if (ENABLE_HD
111 && (!ENABLE_HEXDUMP || !applet_name[2])
112 ) {
113 ch = 'C';
114 goto hd_applet;
115 }
116
117
118
119 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
120 p = strchr(hexdump_opts, ch);
121 if (!p)
122 bb_show_usage();
123 if ((p - hexdump_opts) < 5) {
124 bb_dump_add(dumper, add_first);
125 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
126 }
127
128 if (ch == 'C') {
129 hd_applet:
130 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
131
132 bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \"");
133
134 bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
135 }
136 if (ch == 'e') {
137 bb_dump_add(dumper, optarg);
138 }
139 if (ch == 'f') {
140 bb_dump_addfile(dumper, optarg);
141 }
142 if (ch == 'n') {
143 dumper->dump_length = xatoi_positive(optarg);
144 }
145 if (ch == 's') {
146 dumper->dump_skip = xstrtoull_range_sfx(
147 optarg,
148 0,
149 0, OFF_T_MAX,
150 bkm_suffixes
151 );
152 }
153 if (ch == 'v') {
154 dumper->dump_vflag = ALL;
155 }
156#if ENABLE_FEATURE_HEXDUMP_REVERSE
157 if (ch == 'R') {
158 rdump = 1;
159 }
160#endif
161 }
162
163 if (!dumper->fshead) {
164 bb_dump_add(dumper, add_first);
165 bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\"");
166 }
167
168 argv += optind;
169
170#if !ENABLE_FEATURE_HEXDUMP_REVERSE
171 return bb_dump_dump(dumper, argv);
172#else
173 if (!rdump) {
174 return bb_dump_dump(dumper, argv);
175 }
176
177
178 fp = stdin;
179 if (!*argv) {
180 argv--;
181 goto jump_in;
182 }
183
184 do {
185 char *buf;
186 fp = xfopen_for_read(*argv);
187 jump_in:
188 while ((buf = xmalloc_fgetline(fp)) != NULL) {
189 p = buf;
190 while (1) {
191
192 while (isxdigit(*p)) p++;
193 while (*p == ' ') p++;
194
195 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
196 break;
197 putchar(ch);
198 }
199 free(buf);
200 }
201 fclose(fp);
202 } while (*++argv);
203
204 fflush_stdout_and_exit(EXIT_SUCCESS);
205#endif
206}
207