Checking for new mail
T. William Wells
bill at twwells.com
Sun Aug 27 06:08:13 AEST 1989
In article <105 at csnz.co.nz> paul at csnz.co.nz (Paul Gillingwater) writes:
: What's the best way to check for the arrival of new mail?
: I'm writing in C using SCO Xenix 2.3.1, but no doubt other approaches
: would be helpful too. I want a menu to display an indicator saying that
: new mail has arrived when it comes into the user's mailbox. It should
: go away when they have read it. I'd like it to look every so often,
: maybe once per minute -- but it should check each time a new menu
: is selected.
I saw a lot of answers, none of which seemed to answer the question.
If I understand you, you want your C program to check to see if mail
has arrived in the user's mailbox.
On many (all?) Unix systems, there is a shell variable MAIL that
contains the name of the user's mailbox. If yours doesn't provide
one, you can generally set up such a variable in the /etc/profile or
whatever your shell uses for initialization at login.
Anyway, given the variable, you then use the stat() system call to
see what the file modification time is. If it is later than the file
modification time since you last checked, new mail has arrived.
(Barring other things that might modify this file, like reading the
mail.)
Here is a sample function to do this; this was just typed in by hand:
caveat usor!
#include <sys/types.h>
#include <sys/stat.h>
/* There are two functions done here, the init function and the check
function. Normal use is as follows:
has_mail = mail_check(1);
is called at the start of the program and after the user has read
his mail or told the program that he doesn't care to know about
mail that is already in the box. It returns true if the mailbox
exists.
has_mail = mail_check(0);
is called periodically, to see if mail has arrived since the last
initialization. It returns true when mail has arrived, and
continues to return true till the function is called with init=1
(Barring wierdness like touching the mail file).
Note that this function insists on using the mail file specified
at the first call; changing the MAIL variable afterwords will have
no effect. */
int
mail_check(init)
int init;
{
struct stat statbuf;
static char *mail_file;
static time_t last_time;
/* Get the name of the mail file. If there is none, assume
that the user doesn't want a mail check. */
if (!mail_file) {
mail_file = getenv("MAIL");
}
if (!mail_file) {
return (0);
}
/* Get the file modification time. If there isn't a file (or
the file is otherwise unaccessible), pretend that it was
modified at the earliest possible moment. */
if (stat(mail_file, &statbuf) < 0) {
statbuf.st_mtime = 0;
}
/* If initializing, just squirrel away the time; return true
if the file exists. */
if (init) {
last_time = statbuf.st_mtime;
return (last_time != 0);
}
/* Return true if the current modification time is greater
than the saved modification time. */
return (statbuf.st_mtime > last_time);
}
---
Bill { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com
More information about the Comp.unix.xenix
mailing list