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.3KB

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 ( { map { /^(\w+)/; $1 => $opts->{$_} } keys %$opts }, keys %$opts )
  17. or die "Could not parse command-line options\n";
  18. while ( my $filename = shift ) {
  19. die "$filename is not a file\n" unless -f $filename;
  20. my $oldsize = -s $filename;
  21. my $newsize = ( ( $oldsize + $blksize - 1 ) & ~( $blksize - 1 ) );
  22. my $padsize = ( $newsize - $oldsize );
  23. next unless $padsize;
  24. if ( $verbosity >= 1 ) {
  25. printf "Padding %s from %d to %d bytes with %d x 0x%02x\n",
  26. $filename, $oldsize, $newsize, $padsize, $byte;
  27. }
  28. if ( $byte ) {
  29. sysopen ( my $fh, $filename, ( O_WRONLY | O_APPEND ) )
  30. or die "Could not open $filename for appending: $!\n";
  31. syswrite $fh, ( chr ( $byte ) x $padsize )
  32. or die "Could not append to $filename: $!\n";
  33. close ( $fh );
  34. } else {
  35. truncate $filename, $newsize
  36. or die "Could not resize $filename: $!\n";
  37. }
  38. die "Failed to pad $filename\n" unless -s $filename == $newsize;
  39. }