]>
Commit | Line | Data |
---|---|---|
d4a28ab0 | 1 | /** |
fe3a7d70 | 2 | * dump.c HEXDUMP utility |
d4a28ab0 | 3 | * |
7156ebbc | 4 | * Copyright 2001,2002,2005,2009 by Charles Wilson |
d4a28ab0 CW |
5 | * All rights reserved. |
6 | * | |
7156ebbc CW |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
d4a28ab0 CW |
11 | * |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
7156ebbc | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
d4a28ab0 | 19 | * |
7156ebbc | 20 | * See the COPYING file for full license information. |
d4a28ab0 CW |
21 | */ |
22 | ||
bd695173 | 23 | #if HAVE_CONFIG_H |
fe3a7d70 | 24 | # include "config.h" |
bd695173 CW |
25 | #endif |
26 | ||
d2b03e6a | 27 | #include "common.h" |
d4a28ab0 | 28 | |
c09847dc CW |
29 | #if defined(__WIN32__) && !defined(__CYGWIN__) |
30 | # include <io.h> | |
31 | # include <conio.h> | |
32 | #endif | |
33 | ||
b96d6602 | 34 | static const char versionID[] = PACKAGE_VERSION; |
d4a28ab0 | 35 | static const char revID[] = |
fe3a7d70 | 36 | "$Id$"; |
d4a28ab0 | 37 | static const char copyrightID[] = |
7156ebbc | 38 | "Copyright (c) 2009\nCharles S. Wilson. All rights reserved.\nLicensed under GPLv3+\n"; |
fe3a7d70 CW |
39 | |
40 | static void printTopDescription (FILE * f, char *name); | |
41 | static void printBottomDescription (FILE * f, char *name); | |
42 | static const char *getVersion (void); | |
a4905427 CW |
43 | static void usage (FILE * f, char *name); |
44 | static void help (FILE * f, char *name); | |
45 | static void version (FILE * f, char *name); | |
46 | static void license (FILE * f, char *name); | |
fe3a7d70 CW |
47 | static void puthex (long n, int digits, int pos); |
48 | static void dumpfile (FILE * f); | |
49 | ||
50 | static char *program_name; | |
a4905427 | 51 | static poptContext optCon; |
d4a28ab0 CW |
52 | static char line[80]; |
53 | static long address; | |
54 | ||
fe3a7d70 CW |
55 | int |
56 | main (int argc, const char **argv) | |
d4a28ab0 | 57 | { |
fe3a7d70 | 58 | const char **rest; |
d4a28ab0 CW |
59 | int rc; |
60 | int ec = 0; | |
61 | ||
62 | struct poptOption helpOptionsTable[] = { | |
fe3a7d70 CW |
63 | {"help", '?', POPT_ARG_NONE, NULL, '?', |
64 | "Show this help message", NULL}, | |
65 | {"usage", '\0', POPT_ARG_NONE, NULL, 'u', | |
66 | "Display brief usage message", NULL}, | |
67 | {"version", '\0', POPT_ARG_NONE, NULL, 'v', | |
68 | "Display version information", NULL}, | |
69 | {"license", '\0', POPT_ARG_NONE, NULL, 'l', | |
70 | "Display licensing information", NULL}, | |
71 | {NULL, '\0', 0, NULL, 0, NULL, NULL} | |
d4a28ab0 CW |
72 | }; |
73 | ||
74 | struct poptOption opt[] = { | |
fe3a7d70 CW |
75 | {NULL, '\0', POPT_ARG_INCLUDE_TABLE, helpOptionsTable, 0, |
76 | "Help options", NULL}, | |
77 | {NULL, '\0', 0, NULL, 0, NULL, NULL} | |
d4a28ab0 CW |
78 | }; |
79 | ||
fe3a7d70 CW |
80 | if ((program_name = strdup (argv[0])) == NULL) |
81 | { | |
82 | fprintf (stderr, "%s: memory allocation error\n", argv[0]); | |
83 | exit (1); | |
d4a28ab0 | 84 | } |
fe3a7d70 CW |
85 | optCon = poptGetContext (NULL, argc, argv, opt, 0); |
86 | poptSetOtherOptionHelp (optCon, "[OPTION...] [files...]"); | |
87 | while ((rc = poptGetNextOpt (optCon)) > 0) | |
88 | { | |
89 | switch (rc) | |
90 | { | |
91 | case '?': | |
a4905427 | 92 | help (stdout, program_name); |
fe3a7d70 CW |
93 | goto exit; |
94 | case 'u': | |
a4905427 | 95 | usage (stdout, program_name); |
fe3a7d70 CW |
96 | goto exit; |
97 | case 'v': | |
a4905427 | 98 | version (stdout, program_name); |
fe3a7d70 CW |
99 | goto exit; |
100 | case 'l': | |
a4905427 | 101 | license (stdout, program_name); |
fe3a7d70 CW |
102 | goto exit; |
103 | } | |
104 | } | |
105 | if (rc < -1) | |
106 | { | |
107 | fprintf (stderr, "%s: bad argument %s: %s\n", | |
108 | program_name, poptBadOption (optCon, POPT_BADOPTION_NOALIAS), | |
109 | poptStrerror (rc)); | |
110 | ec = 2; | |
111 | goto exit; | |
112 | } | |
113 | rest = poptGetArgs (optCon); | |
d4a28ab0 CW |
114 | |
115 | if (rest == NULL) | |
fe3a7d70 CW |
116 | dumpfile (stdin); |
117 | else | |
118 | { | |
119 | while (*rest) | |
120 | { | |
121 | FILE *f = fopen (*rest, "rb"); | |
122 | printf ("%s:\n", *rest); | |
123 | if (f) | |
124 | { | |
125 | dumpfile (f); | |
126 | fclose (f); | |
127 | } | |
128 | else | |
129 | printf ("*** Can't open %s!!\n", *rest); | |
130 | rest++; | |
131 | } | |
d4a28ab0 | 132 | } |
d4a28ab0 CW |
133 | /* |
134 | if (argc < 2) dumpfile(stdin); | |
135 | else { | |
136 | while (--argc > 0) { | |
137 | FILE *f = fopen(*++argv, "rb"); | |
138 | printf("%s:\n",*argv); | |
139 | if (f) { | |
140 | dumpfile(f); | |
141 | fclose(f); | |
142 | } | |
143 | else printf("*** Can't open %s!!\n", *argv); | |
144 | } | |
145 | } | |
146 | */ | |
147 | exit: | |
fe3a7d70 CW |
148 | poptFreeContext (optCon); |
149 | free (program_name); | |
150 | return (ec); | |
d4a28ab0 CW |
151 | } |
152 | ||
fe3a7d70 CW |
153 | static const char * |
154 | getVersion () | |
d4a28ab0 CW |
155 | { |
156 | return versionID; | |
157 | } | |
158 | ||
fe3a7d70 CW |
159 | static void |
160 | printTopDescription (FILE * f, char *name) | |
d4a28ab0 | 161 | { |
fe3a7d70 CW |
162 | fprintf (f, "%s is part of cygutils version %s\n", name, getVersion ()); |
163 | fprintf (f, " Prints a hexdump of stdin or specified files to stdout\n\n"); | |
d4a28ab0 CW |
164 | } |
165 | ||
fe3a7d70 CW |
166 | static void |
167 | printBottomDescription (FILE * f, char *name) | |
d4a28ab0 | 168 | { |
fe3a7d70 CW |
169 | fprintf (f, "\n"); |
170 | fprintf (f, "Other arguments\n"); | |
171 | fprintf (f, | |
172 | " [files...] dump each file specified; if none, use stdin\n"); | |
25a32756 CW |
173 | fprintf (f, |
174 | "\nSimilar in behavior to 'od -Ax -tx2z [a file]'\n"); | |
d4a28ab0 CW |
175 | } |
176 | ||
fe3a7d70 CW |
177 | static |
178 | printLicense (FILE * f, char *name) | |
d4a28ab0 | 179 | { |
fe3a7d70 | 180 | fprintf (f, |
25a32756 CW |
181 | "This program is free software: you can redistribute it and/or modify\n" |
182 | "it under the terms of the GNU General Public License as published by\n" | |
183 | "the Free Software Foundation, either version 3 of the License, or\n" | |
184 | "(at your option) any later version.\n\n" | |
185 | "This program is distributed in the hope that it will be useful,\n" | |
186 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | |
187 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | |
188 | "GNU General Public License for more details.\n\n" | |
189 | "You should have received a copy of the GNU General Public License\n" | |
190 | "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n" | |
191 | "See the COPYING file for full license information.\n"); | |
d4a28ab0 CW |
192 | } |
193 | ||
fe3a7d70 | 194 | static void |
a4905427 | 195 | usage (FILE * f, char *name) |
d4a28ab0 | 196 | { |
fe3a7d70 | 197 | poptPrintUsage (optCon, f, 0); |
d4a28ab0 CW |
198 | } |
199 | ||
fe3a7d70 | 200 | static void |
a4905427 | 201 | help (FILE * f, char *name) |
d4a28ab0 | 202 | { |
fe3a7d70 CW |
203 | printTopDescription (f, name); |
204 | poptPrintHelp (optCon, f, 0); | |
205 | printBottomDescription (f, name); | |
d4a28ab0 CW |
206 | } |
207 | ||
fe3a7d70 | 208 | static void |
a4905427 | 209 | version (FILE * f, char *name) |
d4a28ab0 | 210 | { |
fe3a7d70 | 211 | printTopDescription (f, name); |
d4a28ab0 CW |
212 | } |
213 | ||
fe3a7d70 | 214 | static void |
a4905427 | 215 | license (FILE * f, char *name) |
d4a28ab0 | 216 | { |
fe3a7d70 CW |
217 | printTopDescription (f, name); |
218 | printLicense (f, name); | |
219 | } | |
d4a28ab0 | 220 | |
fe3a7d70 CW |
221 | static void |
222 | puthex (long n, int digits, int pos) | |
223 | { | |
224 | if (digits > 1) | |
225 | puthex (n / 16, digits - 1, pos); | |
226 | line[pos + digits - 1] = "0123456789abcdef"[n % 16]; | |
d4a28ab0 CW |
227 | } |
228 | ||
fe3a7d70 CW |
229 | static void |
230 | dumpfile (FILE * f) | |
231 | { | |
232 | int c, i; | |
d4a28ab0 | 233 | address = 0; |
fe3a7d70 CW |
234 | c = getc (f); |
235 | while (1) | |
236 | { | |
237 | for (i = 0; i < 50; i++) | |
238 | line[i] = ' '; | |
239 | for (; i < 80; i++) | |
240 | line[i] = 0; | |
241 | puthex (address, 8, 0); | |
242 | if (c == EOF) | |
243 | return; | |
244 | for (i = 0; i < 16; i++) | |
245 | { | |
246 | puthex (c & 0xff, 2, 10 + i * 2 + i / 2); | |
247 | line[50 + i] = '.'; | |
248 | if (isprint (c & 0x7f)) | |
249 | line[50 + i] = c & 0x7f; | |
250 | if ((c = getc (f)) == EOF) | |
251 | break; | |
252 | } | |
253 | if (address && ((address % 256) == 0)) | |
254 | { | |
c09847dc | 255 | #if defined(__WIN32__) && !defined(__CYGWIN__) |
fe3a7d70 CW |
256 | if (isatty (STDOUT_FILENO)) |
257 | { | |
258 | while (kbhit ()) | |
259 | getch (); | |
260 | getch (); | |
261 | } | |
d4a28ab0 | 262 | #endif |
fe3a7d70 CW |
263 | puts (""); |
264 | puts | |
265 | (" Addr 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 2 4 6 8 A C E "); | |
266 | puts | |
267 | ("-------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------"); | |
268 | } | |
269 | puts (line); | |
270 | address += 16; | |
d4a28ab0 | 271 | } |
d4a28ab0 | 272 | } |