Commit | Line | Data |
---|---|---|
bdf34e49 JB |
1 | #include <stdio.h> |
2 | #include <ctype.h> | |
20333561 RS |
3 | #ifdef MSDOS |
4 | #include <fcntl.h> | |
5 | #endif | |
bdf34e49 JB |
6 | |
7 | #define DEFAULT_GROUPING 0x01 | |
8 | #define DEFAULT_BASE 16 | |
9 | ||
10 | #undef TRUE | |
11 | #undef FALSE | |
12 | #define TRUE (1) | |
13 | #define FALSE (0) | |
14 | ||
20333561 | 15 | extern void exit (), perror (); |
bdf34e49 JB |
16 | |
17 | int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1; | |
18 | int group_by = DEFAULT_GROUPING; | |
19 | char *progname; | |
20 | ||
20333561 RS |
21 | main (argc, argv) |
22 | int argc; | |
23 | char *argv[]; | |
bdf34e49 | 24 | { |
20333561 RS |
25 | register long address; |
26 | char string[18]; | |
27 | FILE *fp; | |
28 | ||
29 | progname = *argv++; --argc; | |
30 | ||
31 | /* | |
32 | ** -hex hex dump | |
33 | ** -oct Octal dump | |
34 | ** -group-by-8-bits | |
35 | ** -group-by-16-bits | |
36 | ** -group-by-32-bits | |
37 | ** -group-by-64-bits | |
38 | ** -iso iso character set. | |
39 | ** -big-endian Big Endian | |
40 | ** -little-endian Little Endian | |
41 | ** -un || -de from hexl format to binary. | |
42 | ** -- End switch list. | |
43 | ** <filename> dump filename | |
44 | ** - (as filename == stdin) | |
45 | */ | |
bdf34e49 | 46 | |
20333561 | 47 | while (*argv && *argv[0] == '-' && (*argv)[1]) |
bdf34e49 | 48 | { |
20333561 RS |
49 | /* A switch! */ |
50 | if (!strcmp (*argv, "--")) | |
bdf34e49 | 51 | { |
20333561 RS |
52 | --argc; argv++; |
53 | break; | |
54 | } | |
55 | else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de")) | |
bdf34e49 | 56 | { |
20333561 RS |
57 | un_flag = TRUE; |
58 | --argc; argv++; | |
59 | } | |
60 | else if (!strcmp (*argv, "-hex")) | |
bdf34e49 | 61 | { |
20333561 RS |
62 | base = 16; |
63 | --argc; argv++; | |
64 | } | |
65 | else if (!strcmp (*argv, "-iso")) | |
bdf34e49 | 66 | { |
20333561 RS |
67 | iso_flag = TRUE; |
68 | --argc; argv++; | |
69 | } | |
70 | else if (!strcmp (*argv, "-oct")) | |
bdf34e49 | 71 | { |
20333561 RS |
72 | base = 8; |
73 | --argc; argv++; | |
74 | } | |
75 | else if (!strcmp (*argv, "-big-endian")) | |
bdf34e49 | 76 | { |
20333561 RS |
77 | endian = 1; |
78 | --argc; argv++; | |
79 | } | |
80 | else if (!strcmp (*argv, "-little-endian")) | |
bdf34e49 | 81 | { |
20333561 RS |
82 | endian = 0; |
83 | --argc; argv++; | |
84 | } | |
85 | else if (!strcmp (*argv, "-group-by-8-bits")) | |
bdf34e49 | 86 | { |
20333561 RS |
87 | group_by = 0x00; |
88 | --argc; argv++; | |
89 | } | |
90 | else if (!strcmp (*argv, "-group-by-16-bits")) | |
bdf34e49 | 91 | { |
20333561 RS |
92 | group_by = 0x01; |
93 | --argc; argv++; | |
94 | } | |
95 | else if (!strcmp (*argv, "-group-by-32-bits")) | |
bdf34e49 | 96 | { |
20333561 RS |
97 | group_by = 0x03; |
98 | --argc; argv++; | |
99 | } | |
100 | else if (!strcmp (*argv, "-group-by-64-bits")) | |
bdf34e49 | 101 | { |
20333561 RS |
102 | group_by = 0x07; |
103 | endian = 0; | |
104 | --argc; argv++; | |
105 | } | |
106 | else | |
bdf34e49 | 107 | { |
20333561 RS |
108 | fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname, |
109 | *argv); | |
110 | usage (); | |
bdf34e49 JB |
111 | } |
112 | } | |
113 | ||
20333561 | 114 | do |
bdf34e49 | 115 | { |
20333561 RS |
116 | if (*argv == NULL) |
117 | fp = stdin; | |
118 | else | |
bdf34e49 | 119 | { |
20333561 | 120 | char *filename = *argv++; |
bdf34e49 | 121 | |
20333561 RS |
122 | if (!strcmp (filename, "-")) |
123 | fp = stdin; | |
124 | else if ((fp = fopen (filename, "r")) == NULL) | |
125 | { | |
126 | perror (filename); | |
127 | continue; | |
128 | } | |
bdf34e49 JB |
129 | } |
130 | ||
20333561 | 131 | if (un_flag) |
bdf34e49 | 132 | { |
20333561 | 133 | char buf[18]; |
bdf34e49 | 134 | |
20333561 RS |
135 | #ifdef MSDOS |
136 | (stdout)->_flag &= ~_IOTEXT; /* print binary */ | |
137 | _setmode (fileno (stdout), O_BINARY); | |
138 | #endif | |
139 | for (;;) | |
bdf34e49 | 140 | { |
20333561 | 141 | register int i, c, d; |
bdf34e49 | 142 | |
20333561 | 143 | #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10) |
bdf34e49 | 144 | |
20333561 | 145 | fread (buf, 1, 10, fp); /* skip 10 bytes */ |
bdf34e49 | 146 | |
20333561 | 147 | for (i=0; i < 16; ++i) |
bdf34e49 | 148 | { |
20333561 RS |
149 | if ((c = getc (fp)) == ' ' || c == EOF) |
150 | break; | |
bdf34e49 | 151 | |
20333561 RS |
152 | d = getc (fp); |
153 | c = hexchar (c) * 0x10 + hexchar (d); | |
154 | putchar (c); | |
bdf34e49 | 155 | |
20333561 RS |
156 | if ((i&group_by) == group_by) |
157 | getc (fp); | |
bdf34e49 JB |
158 | } |
159 | ||
20333561 | 160 | if (c == ' ') |
bdf34e49 | 161 | { |
20333561 RS |
162 | while ((c = getc (fp)) != '\n' && c != EOF) |
163 | ; | |
bdf34e49 | 164 | |
20333561 RS |
165 | if (c == EOF) |
166 | break; | |
bdf34e49 | 167 | } |
20333561 | 168 | else |
bdf34e49 | 169 | { |
20333561 RS |
170 | if (i < 16) |
171 | break; | |
bdf34e49 | 172 | |
20333561 | 173 | fread (buf, 1, 18, fp); /* skip 18 bytes */ |
bdf34e49 JB |
174 | } |
175 | } | |
176 | } | |
20333561 | 177 | else |
bdf34e49 | 178 | { |
20333561 RS |
179 | #ifdef MSDOS |
180 | (fp)->_flag &= ~_IOTEXT; /* read binary */ | |
181 | _setmode (fileno (fp), O_BINARY); | |
182 | #endif | |
183 | address = 0; | |
184 | string[0] = ' '; | |
185 | string[17] = '\0'; | |
186 | for (;;) | |
bdf34e49 | 187 | { |
20333561 | 188 | register int i, c; |
bdf34e49 | 189 | |
20333561 | 190 | for (i=0; i < 16; ++i) |
bdf34e49 | 191 | { |
20333561 | 192 | if ((c = getc (fp)) == EOF) |
bdf34e49 | 193 | { |
20333561 RS |
194 | if (!i) |
195 | break; | |
bdf34e49 | 196 | |
20333561 RS |
197 | fputs (" ", stdout); |
198 | string[i+1] = '\0'; | |
bdf34e49 | 199 | } |
20333561 | 200 | else |
bdf34e49 | 201 | { |
20333561 RS |
202 | if (!i) |
203 | printf ("%08x: ", address); | |
bdf34e49 | 204 | |
20333561 RS |
205 | if (iso_flag) |
206 | string[i+1] = | |
207 | (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c; | |
208 | else | |
209 | string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c; | |
bdf34e49 | 210 | |
20333561 | 211 | printf ("%02x", c); |
bdf34e49 JB |
212 | } |
213 | ||
20333561 RS |
214 | if ((i&group_by) == group_by) |
215 | putchar (' '); | |
bdf34e49 JB |
216 | } |
217 | ||
20333561 RS |
218 | if (i) |
219 | puts (string); | |
bdf34e49 | 220 | |
20333561 RS |
221 | if (c == EOF) |
222 | break; | |
bdf34e49 | 223 | |
20333561 | 224 | address += 0x10; |
bdf34e49 JB |
225 | |
226 | } | |
227 | } | |
228 | ||
20333561 RS |
229 | if (fp != stdin) |
230 | fclose (fp); | |
bdf34e49 JB |
231 | |
232 | } while (*argv != NULL); | |
20333561 | 233 | return 0; |
bdf34e49 JB |
234 | } |
235 | ||
20333561 | 236 | usage () |
bdf34e49 | 237 | { |
20333561 RS |
238 | fprintf (stderr, "usage: %s [-de] [-iso]\n", progname); |
239 | exit (1); | |
bdf34e49 | 240 | } |