#!/usr/bin/perl use strict; use warnings; # peekFile # This script displays the values of the bytes at specified offsets # in a specified file. # The name 'peekFile' is supposed to suggest the 'peek' command of BASIC # The first command-line arg is the name of the file to be used # The remaining args supplies an array of offsets # For example: # peekFile myDataFile 53 105 # would print out the value of the bytes at offsets 53 and 105 # Offsets can be given in decimal, octal, or hexadecimal # Cameron Hayne (macdev@hayne.net) August 2006 my $scriptName = "peekFile"; if (scalar(@ARGV) < 2) { die "Usage: $scriptName filename offset1 [offset2 ...]\n"; } my $verbose = 1; my $filename = shift @ARGV; my @offsets = @ARGV; sub convertToDec($) { my ($value) = @_; if ($value =~ /^0/) { return oct($value); # does both oct and hex } else { return $value; } } use Fcntl qw(:seek); open(FILE, "<$filename") or die "Can't open file $filename: $!\n"; foreach my $offset (@offsets) { my $decOffset = convertToDec($offset); seek(FILE, $decOffset, SEEK_SET) or die "seek failed: $!"; my $numBytesToRead = 1; my $value; read(FILE, $value, $numBytesToRead) == $numBytesToRead or die "read failed: $!"; printf("Offset $offset: 0x%x\n", unpack("C", $value)); } close(FILE);