Contents of /tags/mkinitrd-6_1_1/busybox/util-linux/hexdump.c
Parent Directory | Revision Log
Revision 841 -
(show annotations)
(download)
Mon May 4 16:31:54 2009 UTC (15 years, 4 months ago) by niro
File MIME type: text/plain
File size: 3492 byte(s)
Mon May 4 16:31:54 2009 UTC (15 years, 4 months ago) by niro
File MIME type: text/plain
File size: 3492 byte(s)
tagged 'mkinitrd-6_1_1'
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 "libbb.h" |
13 | #include "dump.h" |
14 | |
15 | /* This is a NOEXEC applet. Be very careful! */ |
16 | |
17 | static void bb_dump_addfile(dumper_t *dumper, char *name) |
18 | { |
19 | char *p; |
20 | FILE *fp; |
21 | char *buf; |
22 | |
23 | fp = xfopen_for_read(name); |
24 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
25 | p = skip_whitespace(buf); |
26 | if (*p && (*p != '#')) { |
27 | bb_dump_add(dumper, p); |
28 | } |
29 | free(buf); |
30 | } |
31 | fclose(fp); |
32 | } |
33 | |
34 | static const char *const add_strings[] = { |
35 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
36 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
37 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
38 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
39 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
40 | }; |
41 | |
42 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; |
43 | |
44 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" USE_FEATURE_HEXDUMP_REVERSE("R"); |
45 | |
46 | static const struct suffix_mult suffixes[] = { |
47 | { "b", 512 }, |
48 | { "k", 1024 }, |
49 | { "m", 1024*1024 }, |
50 | { } |
51 | }; |
52 | |
53 | int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
54 | int hexdump_main(int argc, char **argv) |
55 | { |
56 | dumper_t *dumper = alloc_dumper(); |
57 | const char *p; |
58 | int ch; |
59 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
60 | FILE *fp; |
61 | smallint rdump = 0; |
62 | #endif |
63 | |
64 | if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */ |
65 | ch = 'C'; |
66 | goto hd_applet; |
67 | } |
68 | |
69 | /* We cannot use getopt32: in hexdump options are cumulative. |
70 | * E.g. "hexdump -C -C file" should dump each line twice */ |
71 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
72 | p = strchr(hexdump_opts, ch); |
73 | if (!p) |
74 | bb_show_usage(); |
75 | if ((p - hexdump_opts) < 5) { |
76 | bb_dump_add(dumper, add_first); |
77 | bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); |
78 | } |
79 | /* Save a little bit of space below by omitting the 'else's. */ |
80 | if (ch == 'C') { |
81 | hd_applet: |
82 | bb_dump_add(dumper, "\"%08.8_Ax\n\""); |
83 | bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
84 | bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\""); |
85 | } |
86 | if (ch == 'e') { |
87 | bb_dump_add(dumper, optarg); |
88 | } /* else */ |
89 | if (ch == 'f') { |
90 | bb_dump_addfile(dumper, optarg); |
91 | } /* else */ |
92 | if (ch == 'n') { |
93 | dumper->dump_length = xatoi_u(optarg); |
94 | } /* else */ |
95 | if (ch == 's') { |
96 | dumper->dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes); |
97 | } /* else */ |
98 | if (ch == 'v') { |
99 | dumper->dump_vflag = ALL; |
100 | } |
101 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
102 | if (ch == 'R') { |
103 | rdump = 1; |
104 | } |
105 | #endif |
106 | } |
107 | |
108 | if (!dumper->fshead) { |
109 | bb_dump_add(dumper, add_first); |
110 | bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
111 | } |
112 | |
113 | argv += optind; |
114 | |
115 | #if !ENABLE_FEATURE_HEXDUMP_REVERSE |
116 | return bb_dump_dump(dumper, argv); |
117 | #else |
118 | if (!rdump) { |
119 | return bb_dump_dump(dumper, argv); |
120 | } |
121 | |
122 | /* -R: reverse of 'hexdump -Cv' */ |
123 | fp = stdin; |
124 | if (!*argv) { |
125 | argv--; |
126 | goto jump_in; |
127 | } |
128 | |
129 | do { |
130 | char *buf; |
131 | fp = xfopen_for_read(*argv); |
132 | jump_in: |
133 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
134 | p = buf; |
135 | while (1) { |
136 | /* skip address or previous byte */ |
137 | while (isxdigit(*p)) p++; |
138 | while (*p == ' ') p++; |
139 | /* '|' char will break the line */ |
140 | if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1) |
141 | break; |
142 | putchar(ch); |
143 | } |
144 | free(buf); |
145 | } |
146 | fclose(fp); |
147 | } while (*++argv); |
148 | |
149 | fflush_stdout_and_exit(EXIT_SUCCESS); |
150 | #endif |
151 | } |