#!/usr/local/bin/perl # chkmail -- Check for new mail # Brian Katzung 5 January 1995 @Spool = ('/usr/mail', '/usr/spool/mail'); $True = 1; $False = 0; ###################################################################### # Check for mail in a mail box file sub Chkmail { local($Mailbox) = $_[0]; local($Label) = $_[1]; local(*MAILBOX); local($InHeaders); local($Content); local($HasMail); local($Unread); local($NewMail); open(MAILBOX, "< $Mailbox\0") || return -1; $InHeaders = $False; $Content = 0; $HasMail = 0; $Unread = 0; $NewMail = 0; while () { if ($InHeaders == $False) { if (!/^From /) { # Skip over the current message body $Previous = $_; while () { if (/^From / && $Previous =~ /^$/) { last; } $Previous = $_; } } if (/^From /) { # Start of new message $InHeaders = $True; ++$HasMail; ++$NewMail; } } else { # Examine headers if (/^$/) { $InHeaders = $False; seek(MAILBOX, $Content, 1) if $Content > 0; $Content = 0; } elsif (/^Status:/i) { --$NewMail; ++$Unread unless /R/; } elsif (/^Content-Length:\s+(\d+)/i) { $Content = $1; } } } close(MAILBOX); if ($HasMail > 0) { $Unread += $NewMail; print "$Label mail ($HasMail total, $NewMail new, $Unread unread)\n"; } return $HasMail; } ###################################################################### # Main program if ($ARGV[0] =~ /^\//) { # Check mail for specified mail box file $Mailbox = shift(@ARGV); $Status = &Chkmail($Mailbox, "$Mailbox has"); } else { if (scalar(@ARGV) == 0) { # Check mail for current user $User = $ENV{'USER'} || $ENV{'LOGNAME'} || (getpwuid($<))[0]; $Label = 'You have'; } else { # Check mail for specified user $User = shift(@ARGV); $Label = "$User has"; } $Status = -1; for $Spool (@Spool) { if (-s "$Spool/$User") { $Status = &Chkmail("$Spool/$User", $Label); last; } } } if ($Status < 0) { exit 2; } if ($Status == 0) { exit 1; } exit 0;