1N/A;# $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
1N/A
1N/A;# The validate routine takes a single multiline string consisting of
1N/A;# lines containing a filename plus a file test to try on it. (The
1N/A;# file test may also be a 'cd', causing subsequent relative filenames
1N/A;# to be interpreted relative to that directory.) After the file test
1N/A;# you may put '|| die' to make it a fatal error if the file test fails.
1N/A;# The default is '|| warn'. The file test may optionally have a ! prepended
1N/A;# to test for the opposite condition. If you do a cd and then list some
1N/A;# relative filenames, you may want to indent them slightly for readability.
1N/A;# If you supply your own "die" or "warn" message, you can use $file to
1N/A;# interpolate the filename.
1N/A
1N/A;# Filetests may be bunched: -rwx tests for all of -r, -w and -x.
1N/A;# Only the first failed test of the bunch will produce a warning.
1N/A
1N/A;# The routine returns the number of warnings issued.
1N/A
1N/A;# Usage:
1N/A;# require "validate.pl";
1N/A;# $warnings += do validate('
1N/A;# /vmunix -e || die
1N/A;# /boot -e || die
1N/A;# /bin cd
1N/A;# csh -ex
1N/A;# csh !-ug
1N/A;# sh -ex
1N/A;# sh !-ug
1N/A;# /usr -d || warn "What happened to $file?\n"
1N/A;# ');
1N/A
1N/Asub validate {
1N/A local($file,$test,$warnings,$oldwarnings);
1N/A foreach $check (split(/\n/,$_[0])) {
1N/A next if $check =~ /^#/;
1N/A next if $check =~ /^$/;
1N/A ($file,$test) = split(' ',$check,2);
1N/A if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
1N/A $testlist = $2;
1N/A @testlist = split(//,$testlist);
1N/A }
1N/A else {
1N/A @testlist = ('Z');
1N/A }
1N/A $oldwarnings = $warnings;
1N/A foreach $one (@testlist) {
1N/A $this = $test;
1N/A $this =~ s/(-\w\b)/$1 \$file/g;
1N/A $this =~ s/-Z/-$one/;
1N/A $this .= ' || warn' unless $this =~ /\|\|/;
1N/A $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || do valmess('$2','$1')/;
1N/A $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
1N/A eval $this;
1N/A last if $warnings > $oldwarnings;
1N/A }
1N/A }
1N/A $warnings;
1N/A}
1N/A
1N/Asub valmess {
1N/A local($disposition,$this) = @_;
1N/A $file = $cwd . '/' . $file unless $file =~ m|^/|;
1N/A if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
1N/A $neg = $1;
1N/A $tmp = $2;
1N/A $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
1N/A $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
1N/A $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
1N/A $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
1N/A $tmp eq 'R' && ($mess = "$file is not readable by you.");
1N/A $tmp eq 'W' && ($mess = "$file is not writable by you.");
1N/A $tmp eq 'X' && ($mess = "$file is not executable by you.");
1N/A $tmp eq 'O' && ($mess = "$file is not owned by you.");
1N/A $tmp eq 'e' && ($mess = "$file does not exist.");
1N/A $tmp eq 'z' && ($mess = "$file does not have zero size.");
1N/A $tmp eq 's' && ($mess = "$file does not have non-zero size.");
1N/A $tmp eq 'f' && ($mess = "$file is not a plain file.");
1N/A $tmp eq 'd' && ($mess = "$file is not a directory.");
1N/A $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
1N/A $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
1N/A $tmp eq 'S' && ($mess = "$file is not a socket.");
1N/A $tmp eq 'b' && ($mess = "$file is not a block special file.");
1N/A $tmp eq 'c' && ($mess = "$file is not a character special file.");
1N/A $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
1N/A $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
1N/A $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
1N/A $tmp eq 'T' && ($mess = "$file is not a text file.");
1N/A $tmp eq 'B' && ($mess = "$file is not a binary file.");
1N/A if ($neg eq '!') {
1N/A $mess =~ s/ is not / should not be / ||
1N/A $mess =~ s/ does not / should not / ||
1N/A $mess =~ s/ not / /;
1N/A }
1N/A print STDERR $mess,"\n";
1N/A }
1N/A else {
1N/A $this =~ s/\$file/'$file'/g;
1N/A print STDERR "Can't do $this.\n";
1N/A }
1N/A if ($disposition eq 'die') { exit 1; }
1N/A ++$warnings;
1N/A}
1N/A
1N/A1;