#!/usr/bin/perl -w # 05/01/1999 acs # 04/19/2002 acs added -e arg to output epoch seconds use strict; use Getopt::Std; my $default_seconds = 60; my $default_mode = 9; # mode 8 - atime; mode 9 - mtime; imode 10 ctime; #offsets into stat() my $verbose = 0; sub test_file { my $f_cc = 0; my $f_name = $_[0]; my $f_mode = $_[1]; my $f_seconds = $_[2]; my $f_verbose = $_[3]; my $f_epoch = $_[4]; if (-e $f_name) { my $now = time(); my @f_ray = stat($f_name); if ($f_epoch) { printf("%d\n",$f_ray[$f_mode]); } else { if (($f_ray[$f_mode] + $f_seconds) > $now) { $f_cc = 1; } } } else { if ($f_verbose) { printf STDERR ("File \"%s\" does not exist.\n",$f_name); } $f_cc = 250; } return($f_cc); } # testfile sub Usage { printf STDERR ("\n"); printf STDERR ("Usage: %s [ -m | -a | -c ] [-s seconds] [-v | -e] ",$0); printf STDERR ("filename\n"); printf STDERR ("\n"); printf STDERR ("-m : Has filename been modified (mtime) in seconds? "); printf STDERR ("(default)\n"); printf STDERR ("-a : Has filename been accessed (atime) in seconds?\n"); printf STDERR ("-c : Has filename been chmod'ed (ctime) in seconds?\n"); printf STDERR ("-s seconds (default %d)\n",$default_seconds); printf STDERR ("-v : verbose; normally %s does its work silently\n",$0); printf STDERR (" returning 0 as an exit status if filename has not\n"); printf STDERR (" been altered in seconds; if -v is enabled the exit\n"); printf STDERR (" status is printed on stdout.\n"); printf STDERR ("-e : print the indicated epoch seconds on stdout.\n"); printf STDERR ("filename - the file/directory/device node to examine.\n\n"); printf STDERR ("Exit Status: 0 - file has NOT been modified/accessed/"); printf STDERR ("changed\n"); printf STDERR ("in seconds; 1 - the file has been modified/accessed/"); printf STDERR ("changed\n"); printf STDERR ("in seconds; >1 error\n"); return(255); } # Usage my $cc = 0; our($opt_m,$opt_a,$opt_c,$opt_s,$opt_v,$opt_e); my $arg_knt = 0; my $arg_knt_ev = 0; my $mode = $default_mode; my $seconds = $default_seconds; my $epoch_mode = 0; getopts('macves:'); if (defined($opt_m)) { $arg_knt++; } if (defined($opt_a)) { $arg_knt++; $mode = 8; } if (defined($opt_c)) { $arg_knt++; $mode = 10; } if (defined($opt_v)) { $verbose = 1; $arg_knt_ev++; } if (defined($opt_e)) { $epoch_mode = 1; $arg_knt_ev++; } if (defined($opt_s)) { $seconds = $opt_s; if ($seconds <= 0) { $seconds = $default_seconds; } } if ($arg_knt > 1) { printf STDERR ("%s: Only 1 -m, -a, -c arg allowed.\n",$0); $cc = Usage(); } if ($arg_knt_ev > 1 && $cc == 0) { printf STDERR ("%s: Only 1 -v or -e arg allowed.\n",$0); $cc = Usage(); } if ($cc == 0) { if ($#ARGV >= 0) { $cc = test_file($ARGV[0],$mode,$seconds,$verbose,$epoch_mode); } else { $cc = Usage(); } } if ($verbose) { printf("%d\n",$cc); } exit($cc);