/*****************************************************/
/* simple program to do a hex dump of a file         */
/*****************************************************/
#include <stdio.h>

main(argc,argv,envp)
int argc;
char **argv, **envp;
    {
	FILE *fp;
	int col,pos;
        char buf[33];

	/* for each file on the command line */
	while(argc-- > 1) {
		if((fp=fopen(argv[argc],"rb")) == NULL) {
			printf("hex: warning - unable to open file %s\n",argv[argc]);
			continue;
		    }

		printf("hex: dumping file %s\n\n",argv[argc]);

	        /* read and dump the file */
		pos=0;
		while(feof(fp) == 0) {
			printf("%4.4x | ",pos);
			for(buf[0]=fgetc(fp),
                            buf[1]=fgetc(fp),
			    col=1; 
			    col < 16;
			    pos++,
			    buf[++col] = fgetc(fp),
			    buf[++col] = fgetc(fp)) {
				printf("%4.4x ",(unsigned short)
				       (buf[col-1]*256+buf[col]));
			    }
			printf(" | ");
			for(col=0; col <= 16; col++) {
				if(buf[col] >= 0x20 && buf[col] <= 0x7e) {
					printf("%c",buf[col]);
				    }
				else {
					printf(" ");
				    }
			    }
			printf("\n");
		    }
		fclose(fp);
	    }
    }

