blob: cddd185e275575b74283d0c1c20d4250c9ef9d7c [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
Glenn L McGrath60281112001-11-02 11:39:46 +000012#include "busybox.h"
Rob Landleyea224be2006-06-18 20:20:07 +000013#include <getopt.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000014#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000015
Manuel Novoa III cad53642003-03-19 09:13:01 +000016static void bb_dump_addfile(char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000017{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000018 char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000019 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 char *buf;
Glenn L McGrath60281112001-11-02 11:39:46 +000021
Rob Landleyd921b2e2006-08-03 15:41:12 +000022 fp = xfopen(name, "r");
Manuel Novoa III cad53642003-03-19 09:13:01 +000023
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000024 while ((buf = xmalloc_getline(fp)) != NULL) {
Rob Landleyea224be2006-06-18 20:20:07 +000025 p = skip_whitespace(buf);
Manuel Novoa III cad53642003-03-19 09:13:01 +000026
27 if (*p && (*p != '#')) {
28 bb_dump_add(p);
Glenn L McGrath60281112001-11-02 11:39:46 +000029 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000031 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000033}
34
Manuel Novoa III cad53642003-03-19 09:13:01 +000035static const char * const add_strings[] = {
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000036 "\"%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 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000041};
42
43static const char add_first[] = "\"%07.7_Ax\n\"";
44
Paul Fox969af892005-11-28 21:06:00 +000045static const char hexdump_opts[] = "bcdoxCe:f:n:s:v";
Manuel Novoa III cad53642003-03-19 09:13:01 +000046
47static const struct suffix_mult suffixes[] = {
48 {"b", 512 },
49 {"k", 1024 },
50 {"m", 1024*1024 },
51 {NULL, 0 }
52};
53
Denis Vlasenko06af2162007-02-03 17:28:39 +000054int hexdump_main(int argc, char **argv);
Glenn L McGrath60281112001-11-02 11:39:46 +000055int hexdump_main(int argc, char **argv)
56{
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000058 int ch;
Glenn L McGrath60281112001-11-02 11:39:46 +000059
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 bb_dump_vflag = FIRST;
61 bb_dump_length = -1;
62
63 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
Denis Vlasenko4c196a82006-09-23 15:53:01 +000064 p = strchr(hexdump_opts, ch);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000065 if (!p)
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_show_usage();
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000067 if ((p - hexdump_opts) < 5) {
68 bb_dump_add(add_first);
69 bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
70 } else if (ch == 'C') {
71 bb_dump_add("\"%08.8_Ax\n\"");
72 bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
73 bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
74 } else {
75 /* Save a little bit of space below by omitting the 'else's. */
76 if (ch == 'e') {
77 bb_dump_add(optarg);
78 } /* else */
79 if (ch == 'f') {
80 bb_dump_addfile(optarg);
81 } /* else */
82 if (ch == 'n') {
Denis Vlasenko13858992006-10-08 12:49:22 +000083 bb_dump_length = xatoi_u(optarg);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000084 } /* else */
85 if (ch == 's') {
Denis Vlasenko13858992006-10-08 12:49:22 +000086 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +000087 } /* else */
88 if (ch == 'v') {
89 bb_dump_vflag = ALL;
90 }
Glenn L McGrath60281112001-11-02 11:39:46 +000091 }
92 }
93
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 if (!bb_dump_fshead) {
95 bb_dump_add(add_first);
96 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +000097 }
98
99 argv += optind;
100
Denis Vlasenko2dbeaa92006-09-23 13:31:46 +0000101 return bb_dump_dump(argv);
Glenn L McGrath60281112001-11-02 11:39:46 +0000102}