2017-06-30 07:41:19 +03:00
|
|
|
/* $NetBSD: mail.c,v 1.8 2017/06/30 04:41:19 kamil Exp $ */
|
1997-01-12 22:11:37 +03:00
|
|
|
|
1996-09-22 03:35:13 +04:00
|
|
|
/*
|
|
|
|
* Mailbox checking code by Robert J. Gibson, adapted for PD ksh by
|
|
|
|
* John R. MacMillan
|
|
|
|
*/
|
2003-06-23 15:38:51 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
2017-06-30 07:41:19 +03:00
|
|
|
__RCSID("$NetBSD: mail.c,v 1.8 2017/06/30 04:41:19 kamil Exp $");
|
2003-06-23 15:38:51 +04:00
|
|
|
#endif
|
|
|
|
|
1996-09-22 03:35:13 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef KSH
|
2017-06-30 06:56:12 +03:00
|
|
|
#include <sys/stat.h>
|
2017-06-30 06:43:57 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
1996-09-22 03:35:13 +04:00
|
|
|
#include "sh.h"
|
|
|
|
|
2006-01-15 21:16:30 +03:00
|
|
|
#define MBMESSAGE "You have mail in $_"
|
1996-09-22 03:35:13 +04:00
|
|
|
|
|
|
|
typedef struct mbox {
|
|
|
|
struct mbox *mb_next; /* next mbox in list */
|
|
|
|
char *mb_path; /* path to mail file */
|
|
|
|
char *mb_msg; /* to announce arrival of new mail */
|
|
|
|
time_t mb_mtime; /* mtime of mail file */
|
|
|
|
} mbox_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $MAILPATH is a linked list of mboxes. $MAIL is a treated as a
|
|
|
|
* special case of $MAILPATH, where the list has only one node. The
|
|
|
|
* same list is used for both since they are exclusive.
|
|
|
|
*/
|
|
|
|
|
1999-10-20 19:09:58 +04:00
|
|
|
static mbox_t *mplist;
|
|
|
|
static mbox_t mbox;
|
1996-09-22 03:35:13 +04:00
|
|
|
static time_t mlastchkd; /* when mail was last checked */
|
1999-10-20 19:09:58 +04:00
|
|
|
static time_t mailcheck_interval;
|
1996-09-22 03:35:13 +04:00
|
|
|
|
|
|
|
static void munset ARGS((mbox_t *mlist)); /* free mlist and mval */
|
|
|
|
static mbox_t * mballoc ARGS((char *p, char *m)); /* allocate a new mbox */
|
|
|
|
static void mprintit ARGS((mbox_t *mbp));
|
|
|
|
|
|
|
|
void
|
|
|
|
mcheck()
|
|
|
|
{
|
|
|
|
register mbox_t *mbp;
|
|
|
|
time_t now;
|
|
|
|
struct tbl *vp;
|
|
|
|
struct stat stbuf;
|
|
|
|
|
|
|
|
now = time((time_t *) 0);
|
|
|
|
if (mlastchkd == 0)
|
|
|
|
mlastchkd = now;
|
1999-10-20 19:09:58 +04:00
|
|
|
if (now - mlastchkd >= mailcheck_interval) {
|
1996-09-22 03:35:13 +04:00
|
|
|
mlastchkd = now;
|
|
|
|
|
1999-10-20 19:09:58 +04:00
|
|
|
if (mplist)
|
1996-09-22 03:35:13 +04:00
|
|
|
mbp = mplist;
|
|
|
|
else if ((vp = global("MAIL")) && (vp->flag & ISSET))
|
|
|
|
mbp = &mbox;
|
|
|
|
else
|
|
|
|
mbp = NULL;
|
|
|
|
|
|
|
|
while (mbp) {
|
|
|
|
if (mbp->mb_path && stat(mbp->mb_path, &stbuf) == 0
|
|
|
|
&& S_ISREG(stbuf.st_mode))
|
|
|
|
{
|
|
|
|
if (stbuf.st_size
|
|
|
|
&& mbp->mb_mtime != stbuf.st_mtime
|
|
|
|
&& stbuf.st_atime <= stbuf.st_mtime)
|
|
|
|
mprintit(mbp);
|
|
|
|
mbp->mb_mtime = stbuf.st_mtime;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Some mail readers remove the mail
|
|
|
|
* file if all mail is read. If file
|
|
|
|
* does not exist, assume this is the
|
|
|
|
* case and set mtime to zero.
|
|
|
|
*/
|
|
|
|
mbp->mb_mtime = 0;
|
|
|
|
}
|
|
|
|
mbp = mbp->mb_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-20 19:09:58 +04:00
|
|
|
void
|
|
|
|
mcset(interval)
|
|
|
|
long interval;
|
|
|
|
{
|
|
|
|
mailcheck_interval = interval;
|
|
|
|
}
|
|
|
|
|
1996-09-22 03:35:13 +04:00
|
|
|
void
|
|
|
|
mbset(p)
|
|
|
|
register char *p;
|
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
|
|
|
|
if (mbox.mb_msg)
|
|
|
|
afree((void *)mbox.mb_msg, APERM);
|
1999-10-20 19:09:58 +04:00
|
|
|
if (mbox.mb_path)
|
|
|
|
afree((void *)mbox.mb_path, APERM);
|
|
|
|
/* Save a copy to protect from export (which munges the string) */
|
|
|
|
mbox.mb_path = str_save(p, APERM);
|
1996-09-22 03:35:13 +04:00
|
|
|
mbox.mb_msg = NULL;
|
1999-10-20 19:09:58 +04:00
|
|
|
if (p && stat(p, &stbuf) == 0 && S_ISREG(stbuf.st_mode))
|
1996-09-22 03:35:13 +04:00
|
|
|
mbox.mb_mtime = stbuf.st_mtime;
|
|
|
|
else
|
|
|
|
mbox.mb_mtime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mpset(mptoparse)
|
|
|
|
register char *mptoparse;
|
|
|
|
{
|
|
|
|
register mbox_t *mbp;
|
|
|
|
register char *mpath, *mmsg, *mval;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
munset( mplist );
|
|
|
|
mplist = NULL;
|
|
|
|
mval = str_save(mptoparse, APERM);
|
|
|
|
while (mval) {
|
|
|
|
mpath = mval;
|
|
|
|
if ((mval = strchr(mval, PATHSEP)) != NULL) {
|
|
|
|
*mval = '\0', mval++;
|
|
|
|
}
|
|
|
|
/* POSIX/bourne-shell say file%message */
|
|
|
|
for (p = mpath; (mmsg = strchr(p, '%')); ) {
|
|
|
|
/* a literal percent? (POSIXism) */
|
|
|
|
if (mmsg[-1] == '\\') {
|
|
|
|
/* use memmove() to avoid overlap problems */
|
|
|
|
memmove(mmsg - 1, mmsg, strlen(mmsg) + 1);
|
|
|
|
p = mmsg + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* at&t ksh says file?message */
|
|
|
|
if (!mmsg && !Flag(FPOSIX))
|
|
|
|
mmsg = strchr(mpath, '?');
|
|
|
|
if (mmsg) {
|
|
|
|
*mmsg = '\0';
|
|
|
|
mmsg++;
|
|
|
|
}
|
|
|
|
mbp = mballoc(mpath, mmsg);
|
|
|
|
mbp->mb_next = mplist;
|
|
|
|
mplist = mbp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
munset(mlist)
|
|
|
|
register mbox_t *mlist;
|
|
|
|
{
|
|
|
|
register mbox_t *mbp;
|
|
|
|
|
|
|
|
while (mlist != NULL) {
|
|
|
|
mbp = mlist;
|
|
|
|
mlist = mbp->mb_next;
|
|
|
|
if (!mlist)
|
|
|
|
afree((void *)mbp->mb_path, APERM);
|
|
|
|
afree((void *)mbp, APERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static mbox_t *
|
|
|
|
mballoc(p, m)
|
|
|
|
char *p;
|
|
|
|
char *m;
|
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
register mbox_t *mbp;
|
|
|
|
|
|
|
|
mbp = (mbox_t *)alloc(sizeof(mbox_t), APERM);
|
|
|
|
mbp->mb_next = NULL;
|
|
|
|
mbp->mb_path = p;
|
|
|
|
mbp->mb_msg = m;
|
|
|
|
if (stat(mbp->mb_path, &stbuf) == 0 && S_ISREG(stbuf.st_mode))
|
|
|
|
mbp->mb_mtime = stbuf.st_mtime;
|
|
|
|
else
|
|
|
|
mbp->mb_mtime = 0;
|
|
|
|
return(mbp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mprintit( mbp )
|
|
|
|
mbox_t *mbp;
|
|
|
|
{
|
|
|
|
struct tbl *vp;
|
|
|
|
|
1999-10-20 19:09:58 +04:00
|
|
|
/* Ignore setstr errors here (arbitrary) */
|
2017-06-30 07:41:19 +03:00
|
|
|
setstr((vp = local("_", false)), mbp->mb_path, KSH_RETURN_ERROR);
|
1996-09-22 03:35:13 +04:00
|
|
|
|
|
|
|
shellf("%s\n", substitute(mbp->mb_msg ? mbp->mb_msg : MBMESSAGE, 0));
|
|
|
|
|
|
|
|
unset(vp, 0);
|
|
|
|
}
|
|
|
|
#endif /* KSH */
|