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