1#!/usr/bin/perl -w 2 3use strict; 4use warnings; 5use Getopt::Long; 6use Fcntl; 7 8my $verbosity = 0; 9my $blksize = 512; 10my $byte = 0; 11 12my %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 19Getopt::Long::Configure ( 'bundling', 'auto_abbrev' ); 20GetOptions ( %opts ) or die "Could not parse command-line options\n"; 21 22while ( 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