You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

padimg.pl 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use Fcntl;
  6. my $verbosity = 0;
  7. my $blksize = 512;
  8. my $byte = 0;
  9. my %opts = (
  10. 'verbose|v+' => sub { $verbosity++; },
  11. 'quiet|q+' => sub { $verbosity--; },
  12. 'blksize|s=o' => sub { $blksize = $_[1]; },
  13. 'byte|b=o' => sub { $byte = $_[1]; },
  14. );
  15. Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
  16. GetOptions ( %opts ) or die "Could not parse command-line options\n";
  17. while ( my $filename = shift ) {
  18. die "$filename is not a file\n" unless -f $filename;
  19. my $oldsize = -s $filename;
  20. my $padsize = ( ( -$oldsize ) % $blksize );
  21. my $newsize = ( $oldsize + $padsize );
  22. next unless $padsize;
  23. if ( $verbosity >= 1 ) {
  24. printf "Padding %s from %d to %d bytes with %d x 0x%02x\n",
  25. $filename, $oldsize, $newsize, $padsize, $byte;
  26. }
  27. if ( $byte ) {
  28. sysopen ( my $fh, $filename, ( O_WRONLY | O_APPEND ) )
  29. or die "Could not open $filename for appending: $!\n";
  30. syswrite $fh, ( chr ( $byte ) x $padsize )
  31. or die "Could not append to $filename: $!\n";
  32. close ( $fh );
  33. } else {
  34. truncate $filename, $newsize
  35. or die "Could not resize $filename: $!\n";
  36. }
  37. die "Failed to pad $filename\n"
  38. unless ( ( ( -s $filename ) % $blksize ) == 0 );
  39. }