1 /* vi: set sw=4 ts=4: */
3 * hexdump implementation for busybox
4 * Based on code from util-linux v 2.11l
7 * The Regents of the University of California. All rights reserved.
9 * Licensed under GPLv2 or later, see file License in this tarball for details.
16 static void bb_dump_addfile(char *name)
22 fp = xfopen(name, "r");
24 while ((buf = xmalloc_getline(fp)) != NULL) {
25 p = skip_whitespace(buf);
27 if (*p && (*p != '#')) {
35 static const char * const add_strings[] = {
36 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
37 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
38 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
39 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
40 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
43 static const char add_first[] = "\"%07.7_Ax\n\"";
45 static const char hexdump_opts[] = "bcdoxCe:f:n:s:v";
47 static const struct suffix_mult suffixes[] = {
54 int hexdump_main(int argc, char **argv)
59 bb_dump_vflag = FIRST;
62 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
63 p = strchr(hexdump_opts, ch);
66 if ((p - hexdump_opts) < 5) {
67 bb_dump_add(add_first);
68 bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
69 } else if (ch == 'C') {
70 bb_dump_add("\"%08.8_Ax\n\"");
71 bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
72 bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
74 /* Save a little bit of space below by omitting the 'else's. */
79 bb_dump_addfile(optarg);
82 bb_dump_length = xatoi_u(optarg);
85 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
93 if (!bb_dump_fshead) {
94 bb_dump_add(add_first);
95 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
100 return bb_dump_dump(argv);