#
my ($ProgramName, $UsageLong, $UsageShort, $VERSION);
#
my ($ProgramName, $UsageLong, $UsageShort, $VERSION);
my ($MarkBegin, $MarkEnd, $Trailer, $ParamBegin, $ParamEnd, $Placement,
$Package, $File, $Block, $Multi, $InsertRemove, $Comment, $CommentEnd,
$MatchLine, $RegexpMatch, $StdinContent, $NewContent, $InPlace,
my ($MarkBegin, $MarkEnd, $Trailer, $ParamBegin, $ParamEnd, $Placement,
$Package, $File, $Block, $Multi, $InsertRemove, $Comment, $CommentEnd,
$MatchLine, $RegexpMatch, $StdinContent, $NewContent, $InPlace,
# If FILE does not have a trailing newline, be sure to add it
# before appending anything else.
# If FILE does not have a trailing newline, be sure to add it
# before appending anything else.
}
elsif (0 == $skip and $_ =~ /^$mybegin(?:$mytrailer)?$/o) {
push (@filtered, $StdinContent);
}
elsif (0 == $skip and $_ =~ /^$mybegin(?:$mytrailer)?$/o) {
push (@filtered, $StdinContent);
# safety exit in last second :)
die "$ProgramName: New content empty -- aborting file alteration!\n";
}
# safety exit in last second :)
die "$ProgramName: New content empty -- aborting file alteration!\n";
}
sysseek(*$FileHandle, 0, SEEK_SET)
or die "$ProgramName: Failed to seek to the begining of file ($!)\n";
}
sysseek(*$FileHandle, 0, SEEK_SET)
or die "$ProgramName: Failed to seek to the begining of file ($!)\n";
}
- my $wb = syswrite($FileHandle, $newContent);
- if (! $wb or length($newContent) != $wb) {
- # FIXME: try restoring backup copy
- die "$ProgramName: Failed to write the content to '$File' ($!)\n";
+ if (length($newContent)) {
+ my $wb = syswrite($FileHandle, $newContent);
+ if (! $wb or length($newContent) != $wb) {
+ # FIXME: try restoring backup copy
+ my $ncl = length($newContent);
+ die "$ProgramName: Failed to write the content to '$File' (wb=$wb, len=$ncl, err:$!)\n";
+ }
# write content in new file in single write op
sysopen ($FileHandle, $file_new, O_CREAT|O_TRUNC|O_WRONLY)
or die "$ProgramName: Failed to open file '$File' for writing ($!)\n";
# write content in new file in single write op
sysopen ($FileHandle, $file_new, O_CREAT|O_TRUNC|O_WRONLY)
or die "$ProgramName: Failed to open file '$File' for writing ($!)\n";
- my $wb = syswrite($FileHandle, $newContent);
- if (! $wb or length($newContent) != $wb) {
- unlink($file_new);
- die "$ProgramName: Failed to write the content to '$File' ($!)\n";
+ if (length($newContent)) {
+ my $wb = syswrite($FileHandle, $newContent);
+ if (! $wb or length($newContent) != $wb) {
+ unlink($file_new);
+ my ($ncl) = length($newContent);
+ die "$ProgramName: Failed to write the content to '$File' (wb=$wb, len=$ncl, err:$!)\n";
+ }