home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / languages / perl / nutshell / ch6 / xdump < prev    next >
Encoding:
Text File  |  1992-10-18  |  799 b   |  34 lines

  1. #!/usr/bin/perl
  2.  
  3. # Usage: xdump [file]
  4.  
  5. # Use the file they specified, if specified
  6.  
  7. open(STDIN,$ARGV[0]) || die "Can't open $ARGV[0]: $!\n"
  8.     if $ARGV[0];
  9.  
  10. # Do it optimally as long as we can read 16 bytes at a time.
  11.  
  12. while (($len = read(STDIN,$data,16)) == 16) {
  13.     @array = unpack('N4', $data);
  14.     $data =~ tr/\0-\37\177-\377/./;
  15.     printf "%8.8lx    %8.8lx %8.8lx %8.8lx %8.8lx    %s\n",
  16.     $offset, @array, $data;
  17.     $offset += 16;
  18. }
  19.  
  20. # Now finish up the end a byte at a time.
  21.  
  22. if ($len) {
  23.     @array = unpack('C*', $data);
  24.     $data =~ y/\0-\37\177-\377/./;
  25.     for (@array) {
  26.     $_ = sprintf('%2.2x',$_);
  27.     }
  28.     push(@array, '  ') while $len++ < 16;
  29.     $data =~ s/[^ -~]/./g;
  30.     printf "%8.8lx    ", $offset;
  31.     printf "%s%s%s%s %s%s%s%s %s%s%s%s %s%s%s%s    %s\n",
  32.     @array, $data;
  33. }
  34.