]>
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"); | |
d4a28ab0 CW |
173 | } |
174 | ||
fe3a7d70 CW |
175 | static |
176 | printLicense (FILE * f, char *name) | |
d4a28ab0 | 177 | { |
fe3a7d70 CW |
178 | fprintf (f, |
179 | "This program is free software; you can redistribute it and/or\n"); | |
180 | fprintf (f, | |
181 | "modify it under the terms of the GNU General Public License\n"); | |
182 | fprintf (f, | |
183 | "as published by the Free Software Foundation; either version 2\n"); | |
184 | fprintf (f, "of the License, or (at your option) any later version.\n"); | |
185 | fprintf (f, "\n"); | |
186 | fprintf (f, | |
187 | "This program is distributed in the hope that it will be useful,\n"); | |
188 | fprintf (f, | |
189 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"); | |
190 | fprintf (f, | |
191 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"); | |
192 | fprintf (f, "GNU General Public License for more details.\n"); | |
193 | fprintf (f, "\n"); | |
194 | fprintf (f, | |
195 | "You should have received a copy of the GNU General Public License\n"); | |
196 | fprintf (f, | |
197 | "along with this program; if not, write to the Free Software\n"); | |
198 | fprintf (f, | |
199 | "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"); | |
200 | fprintf (f, "\n"); | |
201 | fprintf (f, "See the COPYING file for license information.\n"); | |
d4a28ab0 CW |
202 | } |
203 | ||
fe3a7d70 | 204 | static void |
a4905427 | 205 | usage (FILE * f, char *name) |
d4a28ab0 | 206 | { |
fe3a7d70 | 207 | poptPrintUsage (optCon, f, 0); |
d4a28ab0 CW |
208 | } |
209 | ||
fe3a7d70 | 210 | static void |
a4905427 | 211 | help (FILE * f, char *name) |
d4a28ab0 | 212 | { |
fe3a7d70 CW |
213 | printTopDescription (f, name); |
214 | poptPrintHelp (optCon, f, 0); | |
215 | printBottomDescription (f, name); | |
d4a28ab0 CW |
216 | } |
217 | ||
fe3a7d70 | 218 | static void |
a4905427 | 219 | version (FILE * f, char *name) |
d4a28ab0 | 220 | { |
fe3a7d70 | 221 | printTopDescription (f, name); |
d4a28ab0 CW |
222 | } |
223 | ||
fe3a7d70 | 224 | static void |
a4905427 | 225 | license (FILE * f, char *name) |
d4a28ab0 | 226 | { |
fe3a7d70 CW |
227 | printTopDescription (f, name); |
228 | printLicense (f, name); | |
229 | } | |
d4a28ab0 | 230 | |
fe3a7d70 CW |
231 | static void |
232 | puthex (long n, int digits, int pos) | |
233 | { | |
234 | if (digits > 1) | |
235 | puthex (n / 16, digits - 1, pos); | |
236 | line[pos + digits - 1] = "0123456789abcdef"[n % 16]; | |
d4a28ab0 CW |
237 | } |
238 | ||
fe3a7d70 CW |
239 | static void |
240 | dumpfile (FILE * f) | |
241 | { | |
242 | int c, i; | |
d4a28ab0 | 243 | address = 0; |
fe3a7d70 CW |
244 | c = getc (f); |
245 | while (1) | |
246 | { | |
247 | for (i = 0; i < 50; i++) | |
248 | line[i] = ' '; | |
249 | for (; i < 80; i++) | |
250 | line[i] = 0; | |
251 | puthex (address, 8, 0); | |
252 | if (c == EOF) | |
253 | return; | |
254 | for (i = 0; i < 16; i++) | |
255 | { | |
256 | puthex (c & 0xff, 2, 10 + i * 2 + i / 2); | |
257 | line[50 + i] = '.'; | |
258 | if (isprint (c & 0x7f)) | |
259 | line[50 + i] = c & 0x7f; | |
260 | if ((c = getc (f)) == EOF) | |
261 | break; | |
262 | } | |
263 | if (address && ((address % 256) == 0)) | |
264 | { | |
c09847dc | 265 | #if defined(__WIN32__) && !defined(__CYGWIN__) |
fe3a7d70 CW |
266 | if (isatty (STDOUT_FILENO)) |
267 | { | |
268 | while (kbhit ()) | |
269 | getch (); | |
270 | getch (); | |
271 | } | |
d4a28ab0 | 272 | #endif |
fe3a7d70 CW |
273 | puts (""); |
274 | puts | |
275 | (" 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 "); | |
276 | puts | |
277 | ("-------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------"); | |
278 | } | |
279 | puts (line); | |
280 | address += 16; | |
d4a28ab0 | 281 | } |
d4a28ab0 | 282 | } |