aboutsummaryrefslogtreecommitdiff
path: root/util-linux/hexdump.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/hexdump.c')
-rw-r--r--util-linux/hexdump.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/util-linux/hexdump.c b/util-linux/hexdump.c
new file mode 100644
index 000000000..5cb245feb
--- /dev/null
+++ b/util-linux/hexdump.c
@@ -0,0 +1,101 @@
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 "busybox.h"
13#include <getopt.h>
14#include "dump.h"
15
16static void bb_dump_addfile(char *name)
17{
18 char *p;
19 FILE *fp;
20 char *buf;
21
22 fp = xfopen(name, "r");
23
24 while ((buf = xmalloc_getline(fp)) != NULL) {
25 p = skip_whitespace(buf);
26
27 if (*p && (*p != '#')) {
28 bb_dump_add(p);
29 }
30 free(buf);
31 }
32 fclose(fp);
33}
34
35static 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 */
41};
42
43static const char add_first[] = "\"%07.7_Ax\n\"";
44
45static const char hexdump_opts[] = "bcdoxCe:f:n:s:v";
46
47static const struct suffix_mult suffixes[] = {
48 {"b", 512 },
49 {"k", 1024 },
50 {"m", 1024*1024 },
51 {NULL, 0 }
52};
53
54int hexdump_main(int argc, char **argv)
55{
56 const char *p;
57 int ch;
58
59 bb_dump_vflag = FIRST;
60 bb_dump_length = -1;
61
62 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
63 p = strchr(hexdump_opts, ch);
64 if (!p)
65 bb_show_usage();
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\"");
73 } else {
74 /* Save a little bit of space below by omitting the 'else's. */
75 if (ch == 'e') {
76 bb_dump_add(optarg);
77 } /* else */
78 if (ch == 'f') {
79 bb_dump_addfile(optarg);
80 } /* else */
81 if (ch == 'n') {
82 bb_dump_length = xatoi_u(optarg);
83 } /* else */
84 if (ch == 's') {
85 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
86 } /* else */
87 if (ch == 'v') {
88 bb_dump_vflag = ALL;
89 }
90 }
91 }
92
93 if (!bb_dump_fshead) {
94 bb_dump_add(add_first);
95 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
96 }
97
98 argv += optind;
99
100 return bb_dump_dump(argv);
101}