/*
* hexdump.c
*/
#include <stdio.h>
/*----------------------------------------------------------*/
void yydump(FILE *yyin, int length)
{
int address = 0;
int d;
char data[17];
int i;
data[16] = 0; /* for string terminator */
for(;;){
printf("%05X :", address); /* offset display */
for(i=0;i<16;i++){
d = getc(yyin);
if(EOF == d){
printf("\n");
return;
}
data[i] = (char)d;
if(i % length == 0){
printf(" ");
}
printf("%02X", (unsigned char)data[i]);
/* eliminate undisplayable character */
if((data[i] < 0x20) || (0x7E < data[i])){
data[i] = '.';
}
}
printf(" : %s", data);
printf("\n");
address += 16;
}
}
/*----------------------------------------------------------*/
main(int argc, char *argv[])
{
FILE *yyin;
char *s;
int i;
int length = 1;
/* parse command line arguments */
if(--argc > 0 && (char)(*++argv)[0] == '-'){
length = atoi(*argv+1);
argc--;
argv++;
}
/* open source file */
yyin = fopen(*argv, "r");
if(yyin == NULL){
fprintf(stderr, "Source file cannot be opened.");
exit(-1);
}
/* dump input file */
yydump(yyin, length);
/* close source file */
fclose(yyin);
}