source: MondoRescue/branches/3.3/mindi-busybox/util-linux/hexdump.c@ 3625

Last change on this file since 3625 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

File size: 4.4 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]2/*
3 * hexdump implementation for busybox
4 * Based on code from util-linux v 2.11l
5 *
6 * Copyright (c) 1989
[2725]7 * The Regents of the University of California. All rights reserved.
[821]8 *
[2725]9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]10 */
11
[3232]12//usage:#define hexdump_trivial_usage
13//usage: "[-bcCdefnosvx" IF_FEATURE_HEXDUMP_REVERSE("R") "] [FILE]..."
14//usage:#define hexdump_full_usage "\n\n"
15//usage: "Display FILEs (or stdin) in a user specified format\n"
16//usage: "\n -b One-byte octal display"
17//usage: "\n -c One-byte character display"
18//usage: "\n -C Canonical hex+ASCII, 16 bytes per line"
19//usage: "\n -d Two-byte decimal display"
20//usage: "\n -e FORMAT_STRING"
21//usage: "\n -f FORMAT_FILE"
22//usage: "\n -n LENGTH Interpret only LENGTH bytes of input"
23//usage: "\n -o Two-byte octal display"
24//usage: "\n -s OFFSET Skip OFFSET bytes"
25//usage: "\n -v Display all input data"
26//usage: "\n -x Two-byte hexadecimal display"
27//usage: IF_FEATURE_HEXDUMP_REVERSE(
28//usage: "\n -R Reverse of 'hexdump -Cv'")
29//usage:
30//usage:#define hd_trivial_usage
31//usage: "FILE..."
32//usage:#define hd_full_usage "\n\n"
33//usage: "hd is an alias for hexdump -C"
34
[1765]35#include "libbb.h"
[821]36#include "dump.h"
37
[1765]38/* This is a NOEXEC applet. Be very careful! */
39
[2725]40static void bb_dump_addfile(dumper_t *dumper, char *name)
[821]41{
[1765]42 char *p;
[821]43 FILE *fp;
44 char *buf;
45
[2725]46 fp = xfopen_for_read(name);
47 while ((buf = xmalloc_fgetline(fp)) != NULL) {
[821]48 p = skip_whitespace(buf);
49 if (*p && (*p != '#')) {
[2725]50 bb_dump_add(dumper, p);
[821]51 }
52 free(buf);
53 }
54 fclose(fp);
55}
56
[1765]57static const char *const add_strings[] = {
[2725]58 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
59 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
60 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
61 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
62 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
[821]63};
64
[1765]65static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
[821]66
[2725]67static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
[821]68
[2725]69int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
[821]70int hexdump_main(int argc, char **argv)
71{
[2725]72 dumper_t *dumper = alloc_dumper();
[821]73 const char *p;
74 int ch;
[2725]75#if ENABLE_FEATURE_HEXDUMP_REVERSE
76 FILE *fp;
77 smallint rdump = 0;
78#endif
[821]79
[2725]80 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
81 ch = 'C';
82 goto hd_applet;
83 }
[821]84
[2725]85 /* We cannot use getopt32: in hexdump options are cumulative.
86 * E.g. "hexdump -C -C file" should dump each line twice */
[821]87 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
[1765]88 p = strchr(hexdump_opts, ch);
89 if (!p)
90 bb_show_usage();
91 if ((p - hexdump_opts) < 5) {
[2725]92 bb_dump_add(dumper, add_first);
93 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
[821]94 }
[2725]95 /* Save a little bit of space below by omitting the 'else's. */
96 if (ch == 'C') {
97 hd_applet:
98 bb_dump_add(dumper, "\"%08.8_Ax\n\"");
99 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
100 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
101 }
102 if (ch == 'e') {
103 bb_dump_add(dumper, optarg);
104 } /* else */
105 if (ch == 'f') {
106 bb_dump_addfile(dumper, optarg);
107 } /* else */
108 if (ch == 'n') {
109 dumper->dump_length = xatoi_positive(optarg);
110 } /* else */
111 if (ch == 's') { /* compat: -s accepts hex numbers too */
[3621]112 dumper->dump_skip = xstrtoull_range_sfx(
113 optarg,
114 /*base:*/ 0,
115 /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
116 bkm_suffixes
117 );
[2725]118 } /* else */
119 if (ch == 'v') {
120 dumper->dump_vflag = ALL;
121 }
122#if ENABLE_FEATURE_HEXDUMP_REVERSE
123 if (ch == 'R') {
124 rdump = 1;
125 }
126#endif
[821]127 }
128
[2725]129 if (!dumper->fshead) {
130 bb_dump_add(dumper, add_first);
131 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
[821]132 }
133
134 argv += optind;
135
[2725]136#if !ENABLE_FEATURE_HEXDUMP_REVERSE
137 return bb_dump_dump(dumper, argv);
138#else
139 if (!rdump) {
140 return bb_dump_dump(dumper, argv);
141 }
142
143 /* -R: reverse of 'hexdump -Cv' */
144 fp = stdin;
145 if (!*argv) {
146 argv--;
147 goto jump_in;
148 }
149
150 do {
151 char *buf;
152 fp = xfopen_for_read(*argv);
153 jump_in:
154 while ((buf = xmalloc_fgetline(fp)) != NULL) {
155 p = buf;
156 while (1) {
157 /* skip address or previous byte */
158 while (isxdigit(*p)) p++;
159 while (*p == ' ') p++;
160 /* '|' char will break the line */
161 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
162 break;
163 putchar(ch);
164 }
165 free(buf);
166 }
167 fclose(fp);
168 } while (*++argv);
169
170 fflush_stdout_and_exit(EXIT_SUCCESS);
171#endif
[821]172}
Note: See TracBrowser for help on using the repository browser.