#!/usr/bin/perl 
# CALL SYNTAX: [perl] dump.pl [-decimal] [-cpl <n>] <file>
# display file as hex dump

use Getopt::Long;

# ##### parse command line #########
GetOptions("h", \$help, "cpl=i", \$cpl, "decimal", \$opt_dec);

if ($help != 0)
{
    print "usage: dump.pl [opt...] <file>\n";
    print "                 -cpl <n>  characters (bytes) per line\n";
    print "                           default is 8\n";
    print "                 -decimal  print also decimal addresses\n";
    print "\n\n";
    exit(0);
}

$file = shift;
$cpl = 8 unless ($cpl>0);	# bytes per line

open(file) || die "cannot find $file";

my $format = "%04x: " . ("%02x ")x$cpl ."%$cpl"."s " .
	($opt_dec ? " (%5d)" : "") . "\n";

while ($n=read(file, $buffer, $cpl))
{
	if ($n<$cpl) 
	{
		$buffer .= "\0"x($cpl-$n);
	}
	@c = unpack("C$cpl",$buffer);
	$buffer =~ s/[^\w\(\)!\$=&?+\-#{}\[\];@%*",:\/\\ ]/\./g;
	printf($format, $adr, @c, $buffer, $opt_dec ? $adr : "");
	$adr += $cpl;
}

close(file);

