blob: 12f540a5328f5d7a329c6113cd8f89062db790aa [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>
14#include <string.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000015#include "dump.h"
Glenn L McGrath60281112001-11-02 11:39:46 +000016
Manuel Novoa III cad53642003-03-19 09:13:01 +000017static void bb_dump_addfile(char *name)
Glenn L McGrath60281112001-11-02 11:39:46 +000018{
"Robert P. J. Day"68229832006-07-01 13:08:46 +000019 char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000020 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 char *buf;
Glenn L McGrath60281112001-11-02 11:39:46 +000022
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 fp = bb_xfopen(name, "r");
24
25 while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) {
Rob Landleyea224be2006-06-18 20:20:07 +000026 p = skip_whitespace(buf);
Manuel Novoa III cad53642003-03-19 09:13:01 +000027
28 if (*p && (*p != '#')) {
29 bb_dump_add(p);
Glenn L McGrath60281112001-11-02 11:39:46 +000030 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 free(buf);
Glenn L McGrath60281112001-11-02 11:39:46 +000032 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 fclose(fp);
Glenn L McGrath60281112001-11-02 11:39:46 +000034}
35
Manuel Novoa III cad53642003-03-19 09:13:01 +000036static const char * const add_strings[] = {
37 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
38 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
39 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
40 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
41 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
42};
43
44static const char add_first[] = "\"%07.7_Ax\n\"";
45
Paul Fox969af892005-11-28 21:06:00 +000046static const char hexdump_opts[] = "bcdoxCe:f:n:s:v";
Manuel Novoa III cad53642003-03-19 09:13:01 +000047
48static const struct suffix_mult suffixes[] = {
49 {"b", 512 },
50 {"k", 1024 },
51 {"m", 1024*1024 },
52 {NULL, 0 }
53};
54
Glenn L McGrath60281112001-11-02 11:39:46 +000055int hexdump_main(int argc, char **argv)
56{
57// register FS *tfs;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 const char *p;
Glenn L McGrath60281112001-11-02 11:39:46 +000059 int ch;
Glenn L McGrath60281112001-11-02 11:39:46 +000060
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_dump_vflag = FIRST;
62 bb_dump_length = -1;
63
64 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
65 if ((p = strchr(hexdump_opts, ch)) != NULL) {
66 if ((p - hexdump_opts) < 5) {
67 bb_dump_add(add_first);
68 bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
Paul Fox969af892005-11-28 21:06:00 +000069 } else if (ch == 'C') {
70 bb_dump_add("\"%08.8_Ax\n\"");
71 bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
72 bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 } else {
74 /* Sae a little bit of space below by omitting the 'else's. */
75 if (ch == 'e') {
76 bb_dump_add(optarg);
77 } /* else */
78 if (ch == 'f') {
79 bb_dump_addfile(optarg);
80 } /* else */
81 if (ch == 'n') {
82 bb_dump_length = bb_xgetularg10_bnd(optarg, 0, INT_MAX);
83 } /* else */
84 if (ch == 's') {
85 bb_dump_skip = bb_xgetularg_bnd_sfx(optarg, 10, 0, LONG_MAX, suffixes);
86 } /* else */
87 if (ch == 'v') {
88 bb_dump_vflag = ALL;
89 }
Glenn L McGrath60281112001-11-02 11:39:46 +000090 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 } else {
92 bb_show_usage();
Glenn L McGrath60281112001-11-02 11:39:46 +000093 }
94 }
95
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 if (!bb_dump_fshead) {
97 bb_dump_add(add_first);
98 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
Glenn L McGrath60281112001-11-02 11:39:46 +000099 }
100
101 argv += optind;
102
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 return(bb_dump_dump(argv));
Glenn L McGrath60281112001-11-02 11:39:46 +0000104}