blob: 670867054ba85e644285df0e0b6047370a71140f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath60281112001-11-02 11:39:46 +00002/*
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 *
Rob Landleyea224be2006-06-18 20:20:07 +00009 * Licensed under GPLv2 or later, see file License in this tarball for details.
Glenn L McGrath60281112001-11-02 11:39:46 +000010 */
11
Rob Landleyea224be2006-06-18 20:20:07 +000012#include <getopt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000014#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000015
Denis Vlasenko99912ca2007-04-10 15:43:37 +000016/* This is a NOEXEC applet. Be very careful! */
17
18
Manuel Novoa III cad53642003-03-19 09:13:01 +000019static void bb_dump_addfile(char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000020{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000021 char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000022 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 char *buf;
Glenn L McGrath60281112001-11-02 11:39:46 +000024
Rob Landleyd921b2e2006-08-03 15:41:12 +000025 fp = xfopen(name, "r");
Manuel Novoa III cad53642003-03-19 09:13:01 +000026
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000027 while ((buf = xmalloc_getline(fp)) != NULL) {
Rob Landleyea224be2006-06-18 20:20:07 +000028 p = skip_whitespace(buf);
Manuel Novoa III cad53642003-03-19 09:13:01 +000029
30 if (*p && (*p != '#')) {
31 bb_dump_add(p);
Glenn L McGrath60281112001-11-02 11:39:46 +000032 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000034 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000036}
37
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000038static const char *const add_strings[] = {
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000039 "\"%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 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000044};
45
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000046static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000047
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000048static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" USE_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000049
50static const struct suffix_mult suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +000051 { "b", 512 },
52 { "k", 1024 },
53 { "m", 1024*1024 },
54 { }
Manuel Novoa III cad53642003-03-19 09:13:01 +000055};
56
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000057int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000058int hexdump_main(int argc, char **argv)
59{
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000061 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000062#if ENABLE_FEATURE_HEXDUMP_REVERSE
63 FILE *fp;
64 smallint rdump = 0;
65#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000066
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 bb_dump_vflag = FIRST;
68 bb_dump_length = -1;
69
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000070 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
71 ch = 'C';
72 goto hd_applet;
73 }
74
75 /* We cannot use getopt32: in hexdump options are cumulative.
76 * E.g. hexdump -C -C file should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000078 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000079 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000081 if ((p - hexdump_opts) < 5) {
82 bb_dump_add(add_first);
83 bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000084 }
85 /* Save a little bit of space below by omitting the 'else's. */
86 if (ch == 'C') {
87 hd_applet:
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000088 bb_dump_add("\"%08.8_Ax\n\"");
89 bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
90 bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +000091 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000092 if (ch == 'e') {
93 bb_dump_add(optarg);
94 } /* else */
95 if (ch == 'f') {
96 bb_dump_addfile(optarg);
97 } /* else */
98 if (ch == 'n') {
99 bb_dump_length = xatoi_u(optarg);
100 } /* else */
101 if (ch == 's') {
102 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
103 } /* else */
104 if (ch == 'v') {
105 bb_dump_vflag = ALL;
106 }
107#if ENABLE_FEATURE_HEXDUMP_REVERSE
108 if (ch == 'R') {
109 rdump = 1;
110 }
111#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000112 }
113
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 if (!bb_dump_fshead) {
115 bb_dump_add(add_first);
116 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000117 }
118
119 argv += optind;
120
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000121#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000122 return bb_dump_dump(argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000123#else
124 if (!rdump) {
125 return bb_dump_dump(argv);
126 }
127
128 /* -R: reverse of 'hexdump -Cv' */
129 fp = stdin;
130 if (!*argv) {
131 argv--;
132 goto jump_in;
133 }
134
135 do {
136 char *buf;
137 fp = xfopen(*argv, "r");
138 jump_in:
139 while ((buf = xmalloc_getline(fp)) != NULL) {
140 p = buf;
141 while (1) {
142 /* skip address or previous byte */
143 while (isxdigit(*p)) p++;
144 while (*p == ' ') p++;
145 /* '|' char will break the line */
146 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
147 break;
148 putchar(ch);
149 }
150 free(buf);
151 }
152 fclose(fp);
153 } while (*++argv);
154
155 fflush_stdout_and_exit(EXIT_SUCCESS);
156#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000157}