]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/dump/dump.c
Various code cleanups
[cygwin-apps/cygutils.git] / src / dump / dump.c
1 /**
2 * dump.c HEXDUMP utility
3 *
4 * Copyright 2001,2002,2005,2009 by Charles Wilson
5 * All rights reserved.
6 *
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.
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, see <http://www.gnu.org/licenses/>.
19 *
20 * See the COPYING file for full license information.
21 */
22
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "common.h"
28
29 #if defined(__WIN32__) && !defined(__CYGWIN__)
30 # include <io.h>
31 # include <conio.h>
32 #endif
33
34 static const char versionID[] = PACKAGE_VERSION;
35 static const char revID[] =
36 "$Id$";
37 static const char copyrightID[] =
38 "Copyright (c) 2009\nCharles S. Wilson. All rights reserved.\nLicensed under GPLv3+\n";
39
40 static void printTopDescription (FILE * f, char *name);
41 static void printBottomDescription (FILE * f, char *name);
42 static const char *getVersion (void);
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);
47 static void puthex (long n, int digits, int pos);
48 static void dumpfile (FILE * f);
49
50 static char *program_name;
51 static poptContext optCon;
52 static char line[80];
53 static long address;
54
55 int
56 main (int argc, const char **argv)
57 {
58 const char **rest;
59 int rc;
60 int ec = 0;
61
62 struct poptOption helpOptionsTable[] = {
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}
72 };
73
74 struct poptOption opt[] = {
75 {NULL, '\0', POPT_ARG_INCLUDE_TABLE, helpOptionsTable, 0,
76 "Help options", NULL},
77 {NULL, '\0', 0, NULL, 0, NULL, NULL}
78 };
79
80 if ((program_name = strdup (argv[0])) == NULL)
81 {
82 fprintf (stderr, "%s: memory allocation error\n", argv[0]);
83 exit (1);
84 }
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 '?':
92 help (stdout, program_name);
93 goto exit;
94 case 'u':
95 usage (stdout, program_name);
96 goto exit;
97 case 'v':
98 version (stdout, program_name);
99 goto exit;
100 case 'l':
101 license (stdout, program_name);
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);
114
115 if (rest == NULL)
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 }
132 }
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:
148 poptFreeContext (optCon);
149 free (program_name);
150 return (ec);
151 }
152
153 static const char *
154 getVersion ()
155 {
156 return versionID;
157 }
158
159 static void
160 printTopDescription (FILE * f, char *name)
161 {
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");
164 }
165
166 static void
167 printBottomDescription (FILE * f, char *name)
168 {
169 fprintf (f, "\n");
170 fprintf (f, "Other arguments\n");
171 fprintf (f,
172 " [files...] dump each file specified; if none, use stdin\n");
173 }
174
175 static
176 printLicense (FILE * f, char *name)
177 {
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");
202 }
203
204 static void
205 usage (FILE * f, char *name)
206 {
207 poptPrintUsage (optCon, f, 0);
208 }
209
210 static void
211 help (FILE * f, char *name)
212 {
213 printTopDescription (f, name);
214 poptPrintHelp (optCon, f, 0);
215 printBottomDescription (f, name);
216 }
217
218 static void
219 version (FILE * f, char *name)
220 {
221 printTopDescription (f, name);
222 }
223
224 static void
225 license (FILE * f, char *name)
226 {
227 printTopDescription (f, name);
228 printLicense (f, name);
229 }
230
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];
237 }
238
239 static void
240 dumpfile (FILE * f)
241 {
242 int c, i;
243 address = 0;
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 {
265 #if defined(__WIN32__) && !defined(__CYGWIN__)
266 if (isatty (STDOUT_FILENO))
267 {
268 while (kbhit ())
269 getch ();
270 getch ();
271 }
272 #endif
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;
281 }
282 }
This page took 0.047639 seconds and 5 git commands to generate.