Config-Cfe
view release on metacpan or search on metacpan
=cut
sub read_file {
my ($file, $exist_flag) = @_;
# &lock($file);
if ($exist_flag && !-e $file) {
&new_file($file);
$par->{'mode'} = 0666 & umask;
$par->{'uid'} = $>;
$par->{'gid'} = $);
&verbose('read_file', sprintf("mode=0%o, uid=%d, gid=%d",
$par->{'mode'}, $par->{'uid'}, $par->{'gid'}));
return;
}
&get_filemode($file);
$par->{'file_name'} = $file;
$par->{'edit'} = $par->{'cur_line'} = 0;
my $lines = $par->{'lines'} = [];
unless (-e $file) {
&verbose("read_file", "$file does not exist");
return;
The old file will be renamed to B<.>I<filename>B<.cfe>.
Will not rename the final file (I<filename.new>) to I<filename>
if debug is active.
Accepted keys for B<par>:
key name default
--------------------------------------
mode filemode 0666 & umask.
uid numeric userid 0
gid numeric groupid 0
=cut
sub write_file {
my (%tpar) = @_;
my ($tmp, $fsize, $file);
return unless $par->{'edit'};
$tpar{'mode'} = $par->{'mode'} unless $tpar{'mode'};
$tpar{'uid'} = $par->{'uid'} unless $tpar{'uid'};
$tpar{'gid'} = $par->{'gid'} unless $tpar{'gid'};
croak "No filename!" unless $file = $par->{'file_name'};
($tmp = $file) =~ s/$/.new/;
open(FILEO, ">$tmp") || croak "can't create $tmp:$!";
chmod $tpar{'mode'}, $tmp;
chown $tpar{'uid'}, $tpar{'gid'}, $tmp;
for my $line (@{$par->{'lines'}}) {
print FILEO "$line\n";
$fsize += length($line)+1;
}
close(FILEO);
&abort_file("incorrect filesize, abort") if -s $tmp != $fsize;
unless ($debug) {
rename($file, ".$file.cfe");
rename($tmp, $file) || &abort_file("rename $tmp to $file:$!");
}
sub reset_par {
$par->{'max_lines'} = 100000;
$par->{'file_name'} = '';
$par->{'num_lines'} = 0;
$par->{'lines'} = [];
$par->{'cur_line'} = 0;
$par->{'edit'} = 0;
$par->{'mode'} = 0666 & umask;
$par->{'uid'} = 0;
$par->{'gid'} = 0;
&set_comment;
}
sub get_filemode {
my ($f) = @_;
($par->{'mode'}, $par->{'uid'}, $par->{'gid'}) = (stat($f))[2,4,5];
$par->{'mode'} &= 0777;
&verbose('get_filemode',
sprintf("mode=0%o, uid=%d, gid=%d", $par->{'mode'},
$par->{'uid'}, $par->{'gid'}));
}
sub BEGIN {
my (%type, $uname, $r, $ostype, $version, $mach_type);
$par = {};
&reset_par;
chop($uname = `uname -srm`);
($ostype, $version, $mach_type) = split(/\s+/, $uname);
($r = $version) =~ tr/0-9//cd;
read_file filename
write_file [par]
abort_file
# par-> 'uid':def 0, 'gid':def 0, 'mode':def 0644
locate regexp
incr N
reset
append str [str2 ...]
insert str [str2 ...]
replace_all str1 replacewith1 [str2 replacewith2 ...]
eval exp
( run in 1.373 second using v1.01-cache-2.11-cpan-5735350b133 )