1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * hexdump implementation for busybox
|
---|
4 | * Based on code from util-linux v 2.11l
|
---|
5 | *
|
---|
6 | * Copyright (c) 1989
|
---|
7 | * The Regents of the University of California. All rights reserved.
|
---|
8 | *
|
---|
9 | * Licensed under GPLv2 or later, see file License in this tarball for details.
|
---|
10 | */
|
---|
11 |
|
---|
12 | #include <getopt.h>
|
---|
13 | #include "libbb.h"
|
---|
14 | #include "dump.h"
|
---|
15 |
|
---|
16 | /* This is a NOEXEC applet. Be very careful! */
|
---|
17 |
|
---|
18 |
|
---|
19 | static void bb_dump_addfile(char *name)
|
---|
20 | {
|
---|
21 | char *p;
|
---|
22 | FILE *fp;
|
---|
23 | char *buf;
|
---|
24 |
|
---|
25 | fp = xfopen(name, "r");
|
---|
26 |
|
---|
27 | while ((buf = xmalloc_getline(fp)) != NULL) {
|
---|
28 | p = skip_whitespace(buf);
|
---|
29 |
|
---|
30 | if (*p && (*p != '#')) {
|
---|
31 | bb_dump_add(p);
|
---|
32 | }
|
---|
33 | free(buf);
|
---|
34 | }
|
---|
35 | fclose(fp);
|
---|
36 | }
|
---|
37 |
|
---|
38 | static const char *const add_strings[] = {
|
---|
39 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
|
---|
40 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
|
---|
41 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
|
---|
42 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
|
---|
43 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
|
---|
44 | };
|
---|
45 |
|
---|
46 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
|
---|
47 |
|
---|
48 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
|
---|
49 |
|
---|
50 | static const struct suffix_mult suffixes[] = {
|
---|
51 | { "b", 512 },
|
---|
52 | { "k", 1024 },
|
---|
53 | { "m", 1024*1024 },
|
---|
54 | { }
|
---|
55 | };
|
---|
56 |
|
---|
57 | int hexdump_main(int argc, char **argv);
|
---|
58 | int hexdump_main(int argc, char **argv)
|
---|
59 | {
|
---|
60 | const char *p;
|
---|
61 | int ch;
|
---|
62 |
|
---|
63 | bb_dump_vflag = FIRST;
|
---|
64 | bb_dump_length = -1;
|
---|
65 |
|
---|
66 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
|
---|
67 | p = strchr(hexdump_opts, ch);
|
---|
68 | if (!p)
|
---|
69 | bb_show_usage();
|
---|
70 | if ((p - hexdump_opts) < 5) {
|
---|
71 | bb_dump_add(add_first);
|
---|
72 | bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
|
---|
73 | } else if (ch == 'C') {
|
---|
74 | bb_dump_add("\"%08.8_Ax\n\"");
|
---|
75 | bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
|
---|
76 | bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
|
---|
77 | } else {
|
---|
78 | /* Save a little bit of space below by omitting the 'else's. */
|
---|
79 | if (ch == 'e') {
|
---|
80 | bb_dump_add(optarg);
|
---|
81 | } /* else */
|
---|
82 | if (ch == 'f') {
|
---|
83 | bb_dump_addfile(optarg);
|
---|
84 | } /* else */
|
---|
85 | if (ch == 'n') {
|
---|
86 | bb_dump_length = xatoi_u(optarg);
|
---|
87 | } /* else */
|
---|
88 | if (ch == 's') {
|
---|
89 | bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
|
---|
90 | } /* else */
|
---|
91 | if (ch == 'v') {
|
---|
92 | bb_dump_vflag = ALL;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (!bb_dump_fshead) {
|
---|
98 | bb_dump_add(add_first);
|
---|
99 | bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
|
---|
100 | }
|
---|
101 |
|
---|
102 | argv += optind;
|
---|
103 |
|
---|
104 | return bb_dump_dump(argv);
|
---|
105 | }
|
---|