2 * dump.exe HEXDUMP utility
4 * Copyright 2001,... by Charles Wilson <cwilson@ece.gatech.edu>.
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.
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.
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.
21 * See the COPYING file for license information.
30 #if defined(__WIN32__) && !defined(__CYGWIN__)
35 static const char versionID
[] = PACKAGE_VERSION
;
36 static const char revID
[] =
38 static const char copyrightID
[] =
39 "Copyright (c) 2001,...\nCharles S. Wilson. All rights reserved.\nLicensed under GPL v2.0\n";
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
);
51 static char * program_name
;
55 int main(int argc
, const char ** argv
)
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
}
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
}
80 if( (program_name
= strdup(argv
[0])) == NULL
) {
81 fprintf(stderr
, "%s: memory allocation error\n", argv
[0]);
84 optCon
= poptGetContext(NULL
, argc
, argv
, opt
, 0);
85 poptSetOtherOptionHelp(optCon
, "[OPTION...] [files...]");
86 while ((rc
= poptGetNextOpt(optCon
)) > 0) {
88 case '?': help(optCon
, stderr
, program_name
);
90 case 'u': usage(optCon
, stderr
, program_name
);
92 case 'v': version(optCon
, stderr
, program_name
);
94 case 'l': license(optCon
, stderr
, program_name
);
99 fprintf(stderr
, "%s: bad argument %s: %s\n",
100 program_name
, poptBadOption(optCon
, POPT_BADOPTION_NOALIAS
),
105 rest
= poptGetArgs(optCon
);
111 FILE *f
= fopen(*rest
, "rb");
112 printf("%s:\n",*rest
);
118 printf("*** Can't open %s!!\n", *rest
);
123 if (argc < 2) dumpfile(stdin);
126 FILE *f = fopen(*++argv, "rb");
127 printf("%s:\n",*argv);
132 else printf("*** Can't open %s!!\n", *argv);
137 poptFreeContext(optCon
);
142 static const char * getVersion()
147 static void printTopDescription(FILE * f
, char * name
)
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");
153 static void printBottomDescription(FILE * f
, char * name
)
156 fprintf(f
, "Other arguments\n");
157 fprintf(f
, " [files...] dump each file specified; if none, use stdin\n");
160 static printLicense(FILE * f
, char * name
)
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");
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");
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");
176 fprintf(f
, "See the COPYING file for license information.\n");
179 static void usage(poptContext optCon
, FILE * f
, char * name
)
181 poptPrintUsage(optCon
, f
, 0);
184 static void help(poptContext optCon
, FILE * f
, char * name
)
186 printTopDescription(f
, name
);
187 poptPrintHelp(optCon
, f
, 0);
188 printBottomDescription(f
, name
);
191 static void version(poptContext optCon
, FILE * f
, char * name
)
193 printTopDescription(f
, name
);
196 static void license(poptContext optCon
, FILE * f
, char * name
)
198 printTopDescription(f
, name
);
199 printLicense(f
, name
);
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];
207 static void dumpfile(FILE *f
) {
212 for (i
=0;i
<50;i
++) line
[i
]=' ';
213 for (;i
<80;i
++) line
[i
]=0;
215 if (c
== EOF
) return;
217 puthex(c
& 0xff,2, 10 + i
*2 + i
/2);
219 if (isprint(c
& 0x7f)) line
[50+i
] = c
& 0x7f;
220 if ((c
=getc(f
)) == EOF
) break;
222 if (address
&& ((address
% 256) == 0)) {
223 #if defined(__WIN32__) && !defined(__CYGWIN__)
224 if (isatty (STDOUT_FILENO
)) {
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("-------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------");