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