~vcs-imports/busybox/trunk

« back to all changes in this revision

Viewing changes to util-linux/hexdump.c

  • Committer: Eric Andersen
  • Date: 1999-11-24 09:04:33 UTC
  • Revision ID: git-v1:b99df0fd65abe3245fa2d04115326100847f865e
First draft

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vi: set sw=4 ts=4: */
2
 
/*
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
 
 *
9
 
 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10
 
 */
11
 
//config:config HEXDUMP
12
 
//config:       bool "hexdump (8.7 kb)"
13
 
//config:       default y
14
 
//config:       help
15
 
//config:       The hexdump utility is used to display binary data in a readable
16
 
//config:       way that is comparable to the output from most hex editors.
17
 
//config:
18
 
//config:config HD
19
 
//config:       bool "hd (8.3 kb)"
20
 
//config:       default y
21
 
//config:       help
22
 
//config:       hd is an alias to hexdump -C.
23
 
 
24
 
//applet:IF_HEXDUMP(APPLET_NOEXEC(hexdump, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hexdump))
25
 
//applet:IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))
26
 
 
27
 
//kbuild:lib-$(CONFIG_HEXDUMP) += hexdump.o
28
 
//kbuild:lib-$(CONFIG_HD) += hexdump.o
29
 
 
30
 
//usage:#define hexdump_trivial_usage
31
 
//usage:       "[-bcdoxCv] [-e FMT] [-f FMT_FILE] [-n LEN] [-s OFS] [FILE]..."
32
 
//usage:#define hexdump_full_usage "\n\n"
33
 
//usage:       "Display FILEs (or stdin) in a user specified format\n"
34
 
//usage:     "\n        -b              1-byte octal display"
35
 
//usage:     "\n        -c              1-byte character display"
36
 
//usage:     "\n        -d              2-byte decimal display"
37
 
//usage:     "\n        -o              2-byte octal display"
38
 
//usage:     "\n        -x              2-byte hex display"
39
 
//usage:     "\n        -C              hex+ASCII 16 bytes per line"
40
 
//usage:     "\n        -v              Show all (no dup folding)"
41
 
//usage:     "\n        -e FORMAT_STR   Example: '16/1 \"%02x|\"\"\\n\"'"
42
 
//usage:     "\n        -f FORMAT_FILE"
43
 
// exactly the same help text lines in hexdump and xxd:
44
 
//usage:     "\n        -n LENGTH       Show only first LENGTH bytes"
45
 
//usage:     "\n        -s OFFSET       Skip OFFSET bytes"
46
 
//usage:
47
 
//usage:#define hd_trivial_usage
48
 
//usage:       "FILE..."
49
 
//usage:#define hd_full_usage "\n\n"
50
 
//usage:       "hd is an alias for hexdump -C"
51
 
 
52
 
#include "libbb.h"
53
 
#include "dump.h"
54
 
 
55
 
/* This is a NOEXEC applet. Be very careful! */
56
 
 
57
 
static void bb_dump_addfile(dumper_t *dumper, char *name)
58
 
{
59
 
        char *p;
60
 
        FILE *fp;
61
 
        char *buf;
62
 
 
63
 
        fp = xfopen_for_read(name);
64
 
        while ((buf = xmalloc_fgetline(fp)) != NULL) {
65
 
                p = skip_whitespace(buf);
66
 
                if (*p && (*p != '#')) {
67
 
                        bb_dump_add(dumper, p);
68
 
                }
69
 
                free(buf);
70
 
        }
71
 
        fclose(fp);
72
 
}
73
 
 
74
 
static const char *const add_strings[] ALIGN_PTR = {
75
 
        "16/1 \" %03o"  , /* b */
76
 
        "16/1 \" %3_c"  , /* c */
77
 
        "8/2 \"   %05u" , /* d */
78
 
        "8/2 \"  %06o"  , /* o */
79
 
        "8/2 \"    %04x", /* x */
80
 
};
81
 
 
82
 
static void add_format(dumper_t *dumper, const char *fmt)
83
 
{
84
 
        char fmtbuf[sizeof("\"%07_ax\"" "%s\"" "\"\n\"") + 16];
85
 
        sprintf(fmtbuf, "\"%%07_ax\"" "%s\"" "\"\n\"", fmt);
86
 
        bb_dump_add(dumper, "\"%07_Ax\n\"");
87
 
        bb_dump_add(dumper, fmtbuf);
88
 
}
89
 
 
90
 
static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v";
91
 
 
92
 
int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
93
 
int hexdump_main(int argc, char **argv)
94
 
{
95
 
        dumper_t *dumper = alloc_dumper();
96
 
        const char *p;
97
 
        int ch;
98
 
 
99
 
        if (ENABLE_HD
100
 
         && (!ENABLE_HEXDUMP || !applet_name[2])
101
 
        ) { /* we are "hd" */
102
 
                ch = 'C';
103
 
                goto hd_applet;
104
 
        }
105
 
 
106
 
        /* We cannot use getopt32: in hexdump options are cumulative.
107
 
         * E.g. "hexdump -C -C file" should dump each line twice */
108
 
        while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
109
 
                p = strchr(hexdump_opts, ch);
110
 
                if (!p)
111
 
                        bb_show_usage();
112
 
                if ((p - hexdump_opts) < 5) {
113
 
                        add_format(dumper, add_strings[(int)(p - hexdump_opts)]);
114
 
                }
115
 
                /* Save a little bit of space below by omitting the 'else's. */
116
 
                if (ch == 'C') {
117
 
 hd_applet:
118
 
                        bb_dump_add(dumper, "\"%08_Ax\n\""); // final address line after dump
119
 
                        //------------------- "address "   8 * " xx"  " "  8 * " xx"
120
 
                        bb_dump_add(dumper, "\"%08_ax \"8/1 \" %02x\"\" \"8/1 \" %02x\"");
121
 
                        //------------------- "  |ASCII...........|\n"
122
 
                        bb_dump_add(dumper, "\"  |\"16/1 \"%_p\"\"|\n\"");
123
 
                }
124
 
                if (ch == 'e') {
125
 
                        bb_dump_add(dumper, optarg);
126
 
                } /* else */
127
 
                if (ch == 'f') {
128
 
                        bb_dump_addfile(dumper, optarg);
129
 
                } /* else */
130
 
                if (ch == 'n') {
131
 
                        dumper->dump_length = xatoi_positive(optarg);
132
 
                } /* else */
133
 
                if (ch == 's') { /* compat: -s accepts hex numbers too */
134
 
                        dumper->dump_skip = xstrtoull_range_sfx(
135
 
                                optarg,
136
 
                                /*base:*/ 0,
137
 
                                /*lo:*/ 0, /*hi:*/ OFF_T_MAX,
138
 
                                bkm_suffixes
139
 
                        );
140
 
                } /* else */
141
 
                if (ch == 'v') {
142
 
                        dumper->dump_vflag = ALL;
143
 
                }
144
 
        }
145
 
 
146
 
        if (!dumper->fshead) {
147
 
                add_format(dumper, "8/2 \" %04x");
148
 
        }
149
 
 
150
 
        argv += optind;
151
 
 
152
 
        return bb_dump_dump(dumper, argv);
153
 
}