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