EXEC_PREFER_APPLETS support by Gabriel L. Somlo <somlo@cmu.edu>
[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 "busybox.h"
13 #include <getopt.h>
14 #include "dump.h"
15
16 static void bb_dump_addfile(char *name)
17 {
18         char *p;
19         FILE *fp;
20         char *buf;
21
22         fp = xfopen(name, "r");
23
24         while ((buf = xmalloc_getline(fp)) != NULL) {
25                 p = skip_whitespace(buf);
26
27                 if (*p && (*p != '#')) {
28                         bb_dump_add(p);
29                 }
30                 free(buf);
31         }
32         fclose(fp);
33 }
34
35 static const char * const add_strings[] = {
36         "\"%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 */
41 };
42
43 static const char add_first[] = "\"%07.7_Ax\n\"";
44
45 static const char hexdump_opts[] = "bcdoxCe:f:n:s:v";
46
47 static const struct suffix_mult suffixes[] = {
48         {"b",  512 },
49         {"k",  1024 },
50         {"m",  1024*1024 },
51         {NULL, 0 }
52 };
53
54 int hexdump_main(int argc, char **argv);
55 int hexdump_main(int argc, char **argv)
56 {
57         const char *p;
58         int ch;
59
60         bb_dump_vflag = FIRST;
61         bb_dump_length = -1;
62
63         while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
64                 p = strchr(hexdump_opts, ch);
65                 if (!p)
66                         bb_show_usage();
67                 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') {
83                                 bb_dump_length = xatoi_u(optarg);
84                         } /* else */
85                         if (ch == 's') {
86                                 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
87                         } /* else */
88                         if (ch == 'v') {
89                                 bb_dump_vflag = ALL;
90                         }
91                 }
92         }
93
94         if (!bb_dump_fshead) {
95                 bb_dump_add(add_first);
96                 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
97         }
98
99         argv += optind;
100
101         return bb_dump_dump(argv);
102 }