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