]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/dump/dump.c
Correct licensing oversights
[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 fprintf (f,
174 "\nSimilar in behavior to 'od -Ax -tx2z [a file]'\n");
175 }
176
177 static
178 printLicense (FILE * f, char *name)
179 {
180 fprintf (f,
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");
192 }
193
194 static void
195 usage (FILE * f, char *name)
196 {
197 poptPrintUsage (optCon, f, 0);
198 }
199
200 static void
201 help (FILE * f, char *name)
202 {
203 printTopDescription (f, name);
204 poptPrintHelp (optCon, f, 0);
205 printBottomDescription (f, name);
206 }
207
208 static void
209 version (FILE * f, char *name)
210 {
211 printTopDescription (f, name);
212 }
213
214 static void
215 license (FILE * f, char *name)
216 {
217 printTopDescription (f, name);
218 printLicense (f, name);
219 }
220
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];
227 }
228
229 static void
230 dumpfile (FILE * f)
231 {
232 int c, i;
233 address = 0;
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 {
255 #if defined(__WIN32__) && !defined(__CYGWIN__)
256 if (isatty (STDOUT_FILENO))
257 {
258 while (kbhit ())
259 getch ();
260 getch ();
261 }
262 #endif
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;
271 }
272 }
This page took 0.047192 seconds and 5 git commands to generate.