blob: 7630153e7ac1cd1b79866aa03d5f441903ffcf01 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000014
Denis Vlasenko99912ca2007-04-10 15:43:37 +000015/* This is a NOEXEC applet. Be very careful! */
16
17
Manuel Novoa III cad53642003-03-19 09:13:01 +000018static void bb_dump_addfile(char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000019{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000020 char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000021 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 char *buf;
Glenn L McGrath60281112001-11-02 11:39:46 +000023
Rob Landleyd921b2e2006-08-03 15:41:12 +000024 fp = xfopen(name, "r");
Manuel Novoa III cad53642003-03-19 09:13:01 +000025
Denis Vlasenko8ee649a2008-03-26 20:04:27 +000026 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Rob Landleyea224be2006-06-18 20:20:07 +000027 p = skip_whitespace(buf);
Manuel Novoa III cad53642003-03-19 09:13:01 +000028
29 if (*p && (*p != '#')) {
30 bb_dump_add(p);
Glenn L McGrath60281112001-11-02 11:39:46 +000031 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000033 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000035}
36
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000037static const char *const add_strings[] = {
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000038 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
39 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
40 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
41 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
42 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000043};
44
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000045static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
Manuel Novoa III cad53642003-03-19 09:13:01 +000046
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000047static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" USE_FEATURE_HEXDUMP_REVERSE("R");
Manuel Novoa III cad53642003-03-19 09:13:01 +000048
49static const struct suffix_mult suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +000050 { "b", 512 },
51 { "k", 1024 },
52 { "m", 1024*1024 },
53 { }
Manuel Novoa III cad53642003-03-19 09:13:01 +000054};
55
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000056int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrath60281112001-11-02 11:39:46 +000057int hexdump_main(int argc, char **argv)
58{
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000060 int ch;
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000061#if ENABLE_FEATURE_HEXDUMP_REVERSE
62 FILE *fp;
63 smallint rdump = 0;
64#endif
Glenn L McGrath60281112001-11-02 11:39:46 +000065
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_dump_vflag = FIRST;
67 bb_dump_length = -1;
68
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000069 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
70 ch = 'C';
71 goto hd_applet;
72 }
73
74 /* We cannot use getopt32: in hexdump options are cumulative.
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000075 * E.g. "hexdump -C -C file" should dump each line twice */
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000077 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000078 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000080 if ((p - hexdump_opts) < 5) {
81 bb_dump_add(add_first);
82 bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000083 }
84 /* Save a little bit of space below by omitting the 'else's. */
85 if (ch == 'C') {
86 hd_applet:
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000087 bb_dump_add("\"%08.8_Ax\n\"");
88 bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
89 bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +000090 }
Denis Vlasenkofbe5f392007-11-18 05:36:50 +000091 if (ch == 'e') {
92 bb_dump_add(optarg);
93 } /* else */
94 if (ch == 'f') {
95 bb_dump_addfile(optarg);
96 } /* else */
97 if (ch == 'n') {
98 bb_dump_length = xatoi_u(optarg);
99 } /* else */
100 if (ch == 's') {
101 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
102 } /* else */
103 if (ch == 'v') {
104 bb_dump_vflag = ALL;
105 }
106#if ENABLE_FEATURE_HEXDUMP_REVERSE
107 if (ch == 'R') {
108 rdump = 1;
109 }
110#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000111 }
112
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 if (!bb_dump_fshead) {
114 bb_dump_add(add_first);
115 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +0000116 }
117
118 argv += optind;
119
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000120#if !ENABLE_FEATURE_HEXDUMP_REVERSE
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000121 return bb_dump_dump(argv);
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000122#else
123 if (!rdump) {
124 return bb_dump_dump(argv);
125 }
126
127 /* -R: reverse of 'hexdump -Cv' */
128 fp = stdin;
129 if (!*argv) {
130 argv--;
131 goto jump_in;
132 }
133
134 do {
135 char *buf;
136 fp = xfopen(*argv, "r");
137 jump_in:
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000138 while ((buf = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkofbe5f392007-11-18 05:36:50 +0000139 p = buf;
140 while (1) {
141 /* skip address or previous byte */
142 while (isxdigit(*p)) p++;
143 while (*p == ' ') p++;
144 /* '|' char will break the line */
145 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
146 break;
147 putchar(ch);
148 }
149 free(buf);
150 }
151 fclose(fp);
152 } while (*++argv);
153
154 fflush_stdout_and_exit(EXIT_SUCCESS);
155#endif
Glenn L McGrath60281112001-11-02 11:39:46 +0000156}