libbb: get rid of statics in dump.c; code shrinks a lot too
[oweals/busybox.git] / util-linux / hexdump.c
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 tarball for details.
10  */
11
12 #include "libbb.h"
13 #include "dump.h"
14
15 /* This is a NOEXEC applet. Be very careful! */
16
17
18 static void bb_dump_addfile(dumper_t *dumper, char *name)
19 {
20         char *p;
21         FILE *fp;
22         char *buf;
23
24         fp = xfopen(name, "r");
25
26         while ((buf = xmalloc_fgetline(fp)) != NULL) {
27                 p = skip_whitespace(buf);
28
29                 if (*p && (*p != '#')) {
30                         bb_dump_add(dumper, p);
31                 }
32                 free(buf);
33         }
34         fclose(fp);
35 }
36
37 static const char *const add_strings[] = {
38         "\"%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 */
43 };
44
45 static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
46
47 static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" USE_FEATURE_HEXDUMP_REVERSE("R");
48
49 static const struct suffix_mult suffixes[] = {
50         { "b", 512 },
51         { "k", 1024 },
52         { "m", 1024*1024 },
53         { }
54 };
55
56 int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
57 int hexdump_main(int argc, char **argv)
58 {
59         dumper_t *dumper = alloc_dumper();
60         const char *p;
61         int ch;
62 #if ENABLE_FEATURE_HEXDUMP_REVERSE
63         FILE *fp;
64         smallint rdump = 0;
65 #endif
66
67         if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
68                 ch = 'C';
69                 goto hd_applet;
70         }
71
72         /* We cannot use getopt32: in hexdump options are cumulative.
73          * E.g. "hexdump -C -C file" should dump each line twice */
74         while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
75                 p = strchr(hexdump_opts, ch);
76                 if (!p)
77                         bb_show_usage();
78                 if ((p - hexdump_opts) < 5) {
79                         bb_dump_add(dumper, add_first);
80                         bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
81                 }
82                 /* Save a little bit of space below by omitting the 'else's. */
83                 if (ch == 'C') {
84  hd_applet:
85                         bb_dump_add(dumper, "\"%08.8_Ax\n\"");
86                         bb_dump_add(dumper, "\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
87                         bb_dump_add(dumper, "\"  |\" 16/1 \"%_p\" \"|\\n\"");
88                 }
89                 if (ch == 'e') {
90                         bb_dump_add(dumper, optarg);
91                 } /* else */
92                 if (ch == 'f') {
93                         bb_dump_addfile(dumper, optarg);
94                 } /* else */
95                 if (ch == 'n') {
96                         dumper->dump_length = xatoi_u(optarg);
97                 } /* else */
98                 if (ch == 's') {
99                         dumper->dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
100                 } /* else */
101                 if (ch == 'v') {
102                         dumper->dump_vflag = ALL;
103                 }
104 #if ENABLE_FEATURE_HEXDUMP_REVERSE
105                 if (ch == 'R') {
106                         rdump = 1;
107                 }
108 #endif
109         }
110
111         if (!dumper->fshead) {
112                 bb_dump_add(dumper, add_first);
113                 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
114         }
115
116         argv += optind;
117
118 #if !ENABLE_FEATURE_HEXDUMP_REVERSE
119         return bb_dump_dump(dumper, argv);
120 #else
121         if (!rdump) {
122                 return bb_dump_dump(dumper, argv);
123         }
124
125         /* -R: reverse of 'hexdump -Cv' */
126         fp = stdin;
127         if (!*argv) {
128                 argv--;
129                 goto jump_in;
130         }
131
132         do {
133                 char *buf;
134                 fp = xfopen(*argv, "r");
135  jump_in:
136                 while ((buf = xmalloc_fgetline(fp)) != NULL) {
137                         p = buf;
138                         while (1) {
139                                 /* skip address or previous byte */
140                                 while (isxdigit(*p)) p++;
141                                 while (*p == ' ') p++;
142                                 /* '|' char will break the line */
143                                 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
144                                         break;
145                                 putchar(ch);
146                         }
147                         free(buf);
148                 }
149                 fclose(fp);
150         } while (*++argv);
151
152         fflush_stdout_and_exit(EXIT_SUCCESS);
153 #endif
154 }