diff options
Diffstat (limited to 'util-linux/hexdump.c')
-rw-r--r-- | util-linux/hexdump.c | 149 |
1 files changed, 63 insertions, 86 deletions
diff --git a/util-linux/hexdump.c b/util-linux/hexdump.c index 33648f918..1858b08d4 100644 --- a/util-linux/hexdump.c +++ b/util-linux/hexdump.c | |||
@@ -25,115 +25,92 @@ | |||
25 | #include <getopt.h> | 25 | #include <getopt.h> |
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | #include <string.h> | 27 | #include <string.h> |
28 | #include "dump.h" | ||
29 | #include "busybox.h" | 28 | #include "busybox.h" |
29 | #include "dump.h" | ||
30 | 30 | ||
31 | extern off_t skip; /* bytes to skip */ | 31 | static void bb_dump_addfile(char *name) |
32 | |||
33 | extern FS *fshead; /* head of format strings */ | ||
34 | extern int blocksize; /* data block size */ | ||
35 | extern int length; /* max bytes to read */ | ||
36 | |||
37 | void addfile(char *name) | ||
38 | { | 32 | { |
39 | register char *p; | 33 | register char *p; |
40 | FILE *fp; | 34 | FILE *fp; |
41 | int ch; | 35 | char *buf; |
42 | char buf[2048 + 1]; | ||
43 | 36 | ||
44 | if (!(fp = fopen(name, "r"))) { | 37 | fp = bb_xfopen(name, "r"); |
45 | error_msg_and_die("hexdump: can't read %s.\n", name); | 38 | |
46 | } | 39 | while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) { |
47 | while (fgets(buf, sizeof(buf), fp)) { | 40 | p = (char *) bb_skip_whitespace(buf); |
48 | if (!(p = index(buf, '\n'))) { | 41 | |
49 | error_msg("hexdump: line too long.\n"); | 42 | if (*p && (*p != '#')) { |
50 | while ((ch = getchar()) != '\n' && ch != EOF); | 43 | bb_dump_add(p); |
51 | continue; | ||
52 | } | ||
53 | *p = '\0'; | ||
54 | for (p = buf; *p && isspace(*p); ++p); | ||
55 | if (!*p || *p == '#') { | ||
56 | continue; | ||
57 | } | 44 | } |
58 | add(p); | 45 | free(buf); |
59 | } | 46 | } |
60 | (void)fclose(fp); | 47 | fclose(fp); |
61 | } | 48 | } |
62 | 49 | ||
50 | static const char * const add_strings[] = { | ||
51 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ | ||
52 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ | ||
53 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ | ||
54 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ | ||
55 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ | ||
56 | }; | ||
57 | |||
58 | static const char add_first[] = "\"%07.7_Ax\n\""; | ||
59 | |||
60 | static const char hexdump_opts[] = "bcdoxe:f:n:s:v"; | ||
61 | |||
62 | static const struct suffix_mult suffixes[] = { | ||
63 | {"b", 512 }, | ||
64 | {"k", 1024 }, | ||
65 | {"m", 1024*1024 }, | ||
66 | {NULL, 0 } | ||
67 | }; | ||
68 | |||
63 | int hexdump_main(int argc, char **argv) | 69 | int hexdump_main(int argc, char **argv) |
64 | { | 70 | { |
65 | // register FS *tfs; | 71 | // register FS *tfs; |
66 | char *p; | 72 | const char *p; |
67 | int ch; | 73 | int ch; |
68 | extern enum _vflag vflag; | ||
69 | vflag = FIRST; | ||
70 | length = -1; | ||
71 | 74 | ||
72 | while ((ch = getopt(argc, argv, "bcde:f:n:os:vx")) != EOF) { | 75 | bb_dump_vflag = FIRST; |
73 | switch (ch) { | 76 | bb_dump_length = -1; |
74 | case 'b': | 77 | |
75 | add("\"%07.7_Ax\n\""); | 78 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
76 | add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\""); | 79 | if ((p = strchr(hexdump_opts, ch)) != NULL) { |
77 | break; | 80 | if ((p - hexdump_opts) < 5) { |
78 | case 'c': | 81 | bb_dump_add(add_first); |
79 | add("\"%07.7_Ax\n\""); | 82 | bb_dump_add(add_strings[(int)(p - hexdump_opts)]); |
80 | add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\""); | 83 | } else { |
81 | break; | 84 | /* Sae a little bit of space below by omitting the 'else's. */ |
82 | case 'd': | 85 | if (ch == 'e') { |
83 | add("\"%07.7_Ax\n\""); | 86 | bb_dump_add(optarg); |
84 | add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\""); | 87 | } /* else */ |
85 | break; | 88 | if (ch == 'f') { |
86 | case 'e': | 89 | bb_dump_addfile(optarg); |
87 | add(optarg); | 90 | } /* else */ |
88 | break; | 91 | if (ch == 'n') { |
89 | case 'f': | 92 | bb_dump_length = bb_xgetularg10_bnd(optarg, 0, INT_MAX); |
90 | addfile(optarg); | 93 | } /* else */ |
91 | break; | 94 | if (ch == 's') { |
92 | case 'n': | 95 | bb_dump_skip = bb_xgetularg_bnd_sfx(optarg, 10, 0, LONG_MAX, suffixes); |
93 | if ((length = atoi(optarg)) < 0) { | 96 | } /* else */ |
94 | error_msg_and_die("hexdump: bad length value.\n"); | 97 | if (ch == 'v') { |
95 | } | 98 | bb_dump_vflag = ALL; |
96 | break; | 99 | } |
97 | case 'o': | ||
98 | add("\"%07.7_Ax\n\""); | ||
99 | add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\""); | ||
100 | break; | ||
101 | case 's': | ||
102 | if ((skip = strtol(optarg, &p, 0)) < 0) { | ||
103 | error_msg_and_die("hexdump: bad skip value.\n"); | ||
104 | } | ||
105 | switch(*p) { | ||
106 | case 'b': | ||
107 | skip *= 512; | ||
108 | break; | ||
109 | case 'k': | ||
110 | skip *= 1024; | ||
111 | break; | ||
112 | case 'm': | ||
113 | skip *= 1048576; | ||
114 | break; | ||
115 | } | 100 | } |
116 | break; | 101 | } else { |
117 | case 'v': | 102 | bb_show_usage(); |
118 | vflag = ALL; | ||
119 | break; | ||
120 | case 'x': | ||
121 | add("\"%07.7_Ax\n\""); | ||
122 | add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\""); | ||
123 | break; | ||
124 | case '?': | ||
125 | show_usage(); | ||
126 | } | 103 | } |
127 | } | 104 | } |
128 | 105 | ||
129 | if (!fshead) { | 106 | if (!bb_dump_fshead) { |
130 | add("\"%07.7_Ax\n\""); | 107 | bb_dump_add(add_first); |
131 | add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); | 108 | bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
132 | } | 109 | } |
133 | 110 | ||
134 | argv += optind; | 111 | argv += optind; |
135 | 112 | ||
136 | return(dump(argv)); | 113 | return(bb_dump_dump(argv)); |
137 | } | 114 | } |
138 | /* | 115 | /* |
139 | * Copyright (c) 1989 The Regents of the University of California. | 116 | * Copyright (c) 1989 The Regents of the University of California. |