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#include "libbb.h"
53#include "dump.h"
54
55
56
57static void bb_dump_addfile(dumper_t *dumper, char *name)
58{
59 char *p;
60 FILE *fp;
61 char *buf;
62
63 fp = xfopen_for_read(name);
64 while ((buf = xmalloc_fgetline(fp)) != NULL) {
65 p = skip_whitespace(buf);
66 if (*p && (*p != '#')) {
67 bb_dump_add(dumper, p);
68 }
69 free(buf);
70 }
71 fclose(fp);
72}
73
74static const char *const add_strings[] = {
75 "\"%07.7_ax \"16/1 \"%03o \"\"\n\"",
76 "\"%07.7_ax \"16/1 \"%3_c \"\"\n\"",
77 "\"%07.7_ax \"8/2 \" %05u \"\"\n\"",
78 "\"%07.7_ax \"8/2 \" %06o \"\"\n\"",
79 "\"%07.7_ax \"8/2 \" %04x \"\"\n\"",
80};
81
82static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
83
84static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
85
86int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
87int hexdump_main(int argc, char **argv)
88{
89 dumper_t *dumper = alloc_dumper();
90 const char *p;
91 int ch;
92
93 if (ENABLE_HD
94 && (!ENABLE_HEXDUMP || !applet_name[2])
95 ) {
96 ch = 'C';
97 goto hd_applet;
98 }
99
100
101
102 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
103 p = strchr(hexdump_opts, ch);
104 if (!p)
105 bb_show_usage();
106 if ((p - hexdump_opts) < 5) {
107 bb_dump_add(dumper, add_first);
108 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
109 }
110
111 if (ch == 'C') {
112 hd_applet:
113 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
114
115 bb_dump_add(dumper, "\"%08.8_ax \"8/1 \"%02x \"\" \"8/1 \"%02x \"");
116
117 bb_dump_add(dumper, "\" |\"16/1 \"%_p\"\"|\n\"");
118 }
119 if (ch == 'e') {
120 bb_dump_add(dumper, optarg);
121 }
122 if (ch == 'f') {
123 bb_dump_addfile(dumper, optarg);
124 }
125 if (ch == 'n') {
126 dumper->dump_length = xatoi_positive(optarg);
127 }
128 if (ch == 's') {
129 dumper->dump_skip = xstrtoull_range_sfx(
130 optarg,
131 0,
132 0, OFF_T_MAX,
133 bkm_suffixes
134 );
135 }
136 if (ch == 'v') {
137 dumper->dump_vflag = ALL;
138 }
139 }
140
141 if (!dumper->fshead) {
142 bb_dump_add(dumper, add_first);
143 bb_dump_add(dumper, "\"%07.7_ax \"8/2 \"%04x \"\"\n\"");
144 }
145
146 argv += optind;
147
148 return bb_dump_dump(dumper, argv);
149}
150