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 static const char versionID
[] =
33 static const char revID
[] =
35 static const char copyrightID
[] =
36 "Copyright (c) 2001,...\nCharles S. Wilson. All rights reserved.\nLicensed under GPL v2.0\n";
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
);
48 static char * program_name
;
52 int main(int argc
, const char ** argv
)
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
}
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
}
77 if( (program_name
= strdup(argv
[0])) == NULL
) {
78 fprintf(stderr
, "%s: memory allocation error\n", argv
[0]);
81 optCon
= poptGetContext(NULL
, argc
, argv
, opt
, 0);
82 poptSetOtherOptionHelp(optCon
, "[OPTION...] [files...]");
83 while ((rc
= poptGetNextOpt(optCon
)) > 0) {
85 case '?': help(optCon
, stderr
, program_name
);
87 case 'u': usage(optCon
, stderr
, program_name
);
89 case 'v': version(optCon
, stderr
, program_name
);
91 case 'l': license(optCon
, stderr
, program_name
);
96 fprintf(stderr
, "%s: bad argument %s: %s\n",
97 program_name
, poptBadOption(optCon
, POPT_BADOPTION_NOALIAS
),
102 rest
= poptGetArgs(optCon
);
108 FILE *f
= fopen(*rest
, "rb");
109 printf("%s:\n",*rest
);
115 printf("*** Can't open %s!!\n", *rest
);
120 if (argc < 2) dumpfile(stdin);
123 FILE *f = fopen(*++argv, "rb");
124 printf("%s:\n",*argv);
129 else printf("*** Can't open %s!!\n", *argv);
134 poptFreeContext(optCon
);
139 static const char * getVersion()
144 static void printTopDescription(FILE * f
, char * name
)
146 fprintf(f
, "%s version %s\n", name
, getVersion());
147 fprintf(f
, " Prints a hexdump of stdin or specified files to stdout\n\n");
150 static void printBottomDescription(FILE * f
, char * name
)
153 fprintf(f
, "Other arguments\n");
154 fprintf(f
, " [files...] dump each file specified; if none, use stdin\n");
157 static printLicense(FILE * f
, char * name
)
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");
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");
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");
173 fprintf(f
, "See the COPYING file for license information.\n");
176 static void usage(poptContext optCon
, FILE * f
, char * name
)
178 poptPrintUsage(optCon
, f
, 0);
181 static void help(poptContext optCon
, FILE * f
, char * name
)
183 printTopDescription(f
, name
);
184 poptPrintHelp(optCon
, f
, 0);
185 printBottomDescription(f
, name
);
188 static void version(poptContext optCon
, FILE * f
, char * name
)
190 printTopDescription(f
, name
);
193 static void license(poptContext optCon
, FILE * f
, char * name
)
195 printTopDescription(f
, name
);
196 printLicense(f
, name
);
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];
204 static void dumpfile(FILE *f
) {
209 for (i
=0;i
<50;i
++) line
[i
]=' ';
210 for (;i
<80;i
++) line
[i
]=0;
212 if (c
== EOF
) return;
214 puthex(c
& 0xff,2, 10 + i
*2 + i
/2);
216 if (isprint(c
& 0x7f)) line
[50+i
] = c
& 0x7f;
217 if ((c
=getc(f
)) == EOF
) break;
219 if ((address
% 256) == 0) {
220 #if defined(WIN32) && !defined(__CYGWIN__)
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("-------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------");