/* hdump file_spec reads and dumps a file in hex */ #include #include main(int argc,char *argv[]) { FILE *input; size_t size_read; long length; long i=0; int j; char ch,ch1,ch2,ch3,ch4,ch5,ch6; int chi,chi1,chi2; char ascii[17]; ascii[16]='\0'; /* terminate string */ if(argc != 2) {printf("There must be exactly one file_spec \n"),exit(1);} printf("Dumping file %s \n",argv[1]); if((input=fopen(argv[1],"rb"))==NULL) /* read binary mode */ {printf("Could not open file \n");exit(1);} /*if((length=filelength(fileno(input))) == -1L) printf("File length failed \n"); else printf("File length %ld \n",length); */ printf( " ADDR 0 1 2 3 4 5 6 7 8 9 A B C D E F ASCII printable\n"); while(!feof(input)){ if((i&15)==0){ch1=((i/1048576L)&15L)+48; ch1=ch1<58?ch1:ch1+7; ch2=((i/65536L)&15L)+48; ch2=ch2<58?ch2:ch2+7; ch3=((i/4096)&15)+48; ch3=ch3<58?ch3:ch3+7; ch4=((i/256)&15)+48; ch4=ch4<58?ch4:ch4+7; ch5=((i/16)&15)+48; ch5=ch5<58?ch5:ch5+7; ch6=(i&15)+48; /* zero */ printf("%c%c%c%c%c%c ",ch1,ch2,ch3,ch4,ch5,ch6);} chi=fgetc(input); ch=chi; chi1=((chi/16)&15)+48; chi1=chi1<58?chi1:chi1+7; /* 0..9 and A..F Hex */ chi2=(chi&15)+48; chi2=chi2<58?chi2:chi2+7; ch=ch<32||ch>126?'.':ch; ascii[i&15]=ch; ch1=chi1; ch2=chi2; printf("%c%c ",ch1,ch2); i++; if((i/8)*8==i) printf(" "); if((i/16)*16==i) printf("%s\n",ascii); if(feof(input)) break; } /* done with data, cleanup and finish last line */ if((i/16)*16!=i){ j=i&15; while(j<16){ printf(" "); ascii[j]=' '; j++; if((j/8)*8==j)printf(" "); } printf("%s\n",ascii); } printf("end of dump of %s %ld bytes\n",argv[1],i); }