home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
1
/
1892
/
cyclelog
next >
Wrap
Text File
|
1990-12-28
|
4KB
|
169 lines
#!/usr/bin/perl -- # -*-Perl-*-
#
# cyclelog - roll log file within a give number. Thus logfile.n-1
# => logfile.n, logfile.n-2 => logfile.n-1 ... logfile =>
# logfile.1.
#
# -z compress
# -s n start compression of files from nth logfile
# -n n keep n file starting at 1 .. n
# -d dir keep the logfile in dir directory
# -v verbose mode
# -V print version and exit
# -m mode mode of the logfile
# -g grp group of the log file
# -o owner owner of the logfile
#
#*** config part for different systems
$copy = "/bin/cp";
$compress = "/usr/ucb/compress";
#*** end config
do 'getopts.pl';
&Getopts("o:m:g:zn:d:s:Vv");
die '$Header: cyclelog,v 1.2 90/09/20 10:28:46 admin Exp $'."\n" if $opt_V;
# map owner to its uid
if ($opt_o) {
@entry=getpwnam($opt_o);
if ($#entry == -1) {
die "There is no user called \"$opt_o\"\n";
}
print "Owner setting: \"$opt_o\" maps to uid $entry[2]\n" if $opt_v;
$opt_o = $entry[2];
}
# map group to its gid
if ($opt_g) {
@entry=getgrnam($opt_g);
if ($#entry == -1) {
die "There is no group called \"$opt_g\"\n";
}
print "Group setting: \"$opt_g\" maps to uid $entry[2]\n" if $opt_v;
$opt_g = $entry[2];
}
if ($opt_m) { # convert mode given in octal to decimal
$opt_m = oct($opt_m);
}
$max = (defined($opt_n)) ? $opt_n : 1; # how many logfiles, 1 ... n
$zstart = ($opt_s) ? $opt_s : 1; # if compressing then where to
# start compression from
# for each logfile specified - roll
while ($file = shift) {
print ">>> $file\n" if $opt_v;
if (-e $file) {
$dir = &setdir($file,$opt_d);
&roll($file,$dir,$max);
} else {
warn "\t$file does not exist\n";
}
}
# determine where the logfile are going to be statched
sub setdir {
local($file,$dir) = @_;
if (!$dir) {
if ($file =~ /(.*\/).*/) {
$dir = $1;
} else {
$dir = "./";
}
}
if ($dir !~ /\/$/) {
$dir .= "/";
}
if (! -w $dir) {
warn "\t\"$dir\" - does not exist or is not writable - set to /tmp.\n";
$dir = "/tmp/";
}
print "\tlogdir is \"$dir\"\n" if $opt_v;
$dir;
}
# roll the file
sub roll {
local($file,$dir,$max) = @_;
local($older,$error,$filepart,$dest) = ($max,0,'','');
# get just the filename - /a/b/c/d => d
if ($file =~ /.*\//) {
$filepart = $'; # ' ignore this comment
} else {
$filepart = $file;
}
$dest = "$dir$filepart";
while ($older> 1) {
$old = $older - 1;
if ($opt_z) {
if ($older > $zstart) {
$sz = ".Z";
$dz = ".Z";
} elsif ($older == $zstart && -f "$dest.$old") {
system("$compress $dest.$old");
if (($? >> 8) == 2) { # get exit value of compress
$sz = "";
} else {
$sz = ".Z";
}
$dz = ".Z";
} else {
$sz = "";
$dz = "";
}
}
if (-f "$dest.$old$sz") {
print "\tmv $dest.$old$sz => $dest.$older$dz\n" if $opt_v;
rename("$dest.$old$sz","$dest.$older$dz") ||
warn "rename of $dest.$old$sz => $dest.older$dz failed: $!\n";
}
$older--;
}
if (-f $file) {
if ($max > 0) {
print("\t$copy $file $dest.1\n") if $opt_v;
system("$copy $file $dest.1");
if (($? >> 8) == 0) {
@entry = stat($file);
if ($opt_o || $opt_g) {
$owner = ($opt_o) ? $opt_o : $entry[4];
$group = ($opt_g) ? $opt_g : $entry[5];
printf "\tchown and group to $owner and $group\n" if $opt_v;
chown($owner,$group,"$dest.1") || warn "\tchown failed: $!\n";
}
if ($opt_m) {
chmod($opt_m,"$dest.1");
printf("\tchmod %0o $dest.1\n",$opt_m) if $opt_v;
} else {
printf("\tchmod %0o $dest.1\n",$entry[2]) if $opt_v;
chmod($entry[2],"$dest.1") || warn "\tchmod failed: $!\n";
}
if ($opt_z && $zstart == 1) {
system("$compress $dest.1");
print("\t$compress $dest.1\n") if $opt_v;
if (($? >> 8) == 2) { # get exit value of compress
rename("$dest.1","$dest.1.Z");
print("\tDidn't compress, mv $dest.1 => $dest.1.Z\n") if $opt_v;
}
}
} else {
warn "\tcopy of $file to $dest.1 failed: $!\n";
$error=1;
}
}
if (! $error) {
system("$copy /dev/null $file");
print("\t$copy /dev/null $file\n") if $opt_v;
}
}
}