[alt.sources] od -cx clone

clarke@acheron.UUCP (Ed Clarke) (02/18/89)

/*
 * Function:
 *
 *	Similar to 'od -cx' but with more concise format
 *
 *	Dump a file in hex and printable on the same line.
 *	Quick, dirty, works.  Non printing chars are displayed
 *	using the NONPRINT char, and the 16 byte printable is
 *	delimited by the WALLS char.
 *
 *	Copyright 1989 by E. Clarke.  Released to Public Domain
 *	with no restrictions.  Sell it if you can find anyone
 *	dumb enough to buy it.
*/

#include <stdio.h>
#include <ctype.h>
#ifndef NONPRINT
#define	NONPRINT	'.'
#endif
#ifndef	WALLS
#define WALLS		'|'
#endif

void perror();
void exit();

int  main(argc,argv)
int  argc;
char **argv;
{
FILE	*f;
char	buff[16];
int	i;
static int cnt = -16;

	if(argc == 1)
		f = stdin;
	else
		f = fopen(argv[1],"r");
	if(f == NULL) {
		perror("Can't open file");
		exit(1);
		}
	for(i=0;i<16;i++)
		buff[i] = '\0';

	while(fread(buff,sizeof(char),sizeof(buff),f)) {
		cnt += 16;			/* increment address ptr */
		printf("%06.6X\t",cnt);		/* display it */
		for(i=0;i<8;i++)
			printf("%02.2X ",buff[i]);
		printf("  ");			/* help in counting */
		for(i=8;i<16;i++)
			printf("%02.2X ",buff[i]);
		printf("  %c",WALLS);
		for(i=0;i<16;i++) {
			printf("%c",isprint(buff[i]) ? buff[i] : NONPRINT);
			buff[i] = '\0';
			}
		printf("%c\n",WALLS);
		}
	return(fclose(f));
}
-- 
Ed Clarke
uunet!bywater!acheron!clarke