[net.sources] checkmail

mesches@sunybcs.UUCP (Scott Mesches) (08/04/86)

CHECKMAIL(1)     Local UNIX Programmer's Manual      CHECKMAIL(1)



NAME
     checkmail - check the status of your and others mail

SYNOPSIS
     checkmail [ -q ] [ user ... ]

DESCRIPTION
     checkmail tells whether you have, mail (old mail), new mail,
     no mail, or no mailbox on the machine it is executed on.

     By default, checkmail only reports the status of your mail-
     box, it returns a status of one if you have new mail and
     zero otherwise.  If you specify a list of users, it tells
     you the state of their mailbox(s).

     If you have a .hushlogin file, it defeats mail checking on
     login.  This program can then be used to re-instate this. It
     can also check your mail on login, and if you have new mail
     start up a mail job in the background with the following in
     your .login file.

          checkmail
          if ($status != 0) then
               mail &
          endif

     with the -q option, checkmail is quiet, has no output but
     still sets the status flag.

BUGS
     biff if the user is logged in and they have biff set to y,
     the status returned will be that of old mail.

     from If the user executes the command "from" it will also
     appear as old mail.

SEE ALSO
     chtim(1), stat(2), biff(1), from(1)

AUTHOR
     Scott Mesches

# This is a shell archive.  Remove anything before this line, then
# unpack it by saving it in a file and typing "sh file".  (Files
# unpacked will be owned by you and have default permissions.)
#
# This archive contains:
# checkmail.1 checkmail.c

echo x - checkmail.1
sed -e 's/^X//' > "checkmail.1" << '//E*O*F checkmail.1//'
X.TH CHECKMAIL 1 "15 July 1986" "" "Local UNIX Programmer's Manual"
X.SH NAME
Xcheckmail \- check the status of your and others mail
X.SH SYNOPSIS
X.B checkmail 
X[
X-q
X] [
Xuser ...  
X]
X.SH DESCRIPTION
Xcheckmail tells whether you have, mail (old mail), new mail, no mail,
Xor no mailbox on the machine it is executed on.
X.PP
XBy default,
X.I checkmail
Xonly reports the status of your mailbox, it returns a status of one
Xif you have 
X.I "new mail"
Xand zero otherwise.
XIf you specify a list of users, it tells you
Xthe state of their mailbox(s).
X.PP
XIf you have a .hushlogin file, it defeats mail checking on login.
XThis program can then be used to re-instate this. It can also check 
Xyour mail on login, and if you have new mail start up a mail job 
Xin the background with the following in your
X.I .login 
Xfile.
X
X.nf
X	checkmail
X	if ($status != 0) then
X		mail &
X	endif
X.fi
X.PP
Xwith the -q option, checkmail is quiet, has no output but still 
Xsets the status flag.
X.SH BUGS
X.I biff
Xif the user is logged in and they have biff set to y, the status 
Xreturned will be that of old mail.
X.PP
X.I from
XIf the user executes the command "from" it will also appear as old mail.
X.SH "SEE ALSO"
Xchtim(1), stat(2), biff(1), from(1)
X.SH AUTHOR
XScott Mesches
//E*O*F checkmail.1//

echo x - checkmail.c
sed -e 's/^X//' > "checkmail.c" << '//E*O*F checkmail.c//'
X#ifndef lint
Xstatic char *sccsid = "@(#)checkmail.c	1.4 (Scott Mesches) 7/18/86";
X#endif
X
X/*
X * checkmail  -- Mail checker for me and other folks.
X * 		Returns a status of one for new mail, zero otherwise.
X * 		-q option supresses output and only affects status.
X * Usage: checkmail [ -q ] [ name... ]
X */
X
X#include <stdio.h>
X#include <pwd.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X
X#define	MAILDIR	"/usr/spool/mail"
X#define	TRUE	1
X#define	FALSE	0
X
Xmain (argc, argv)
Xint     argc;
Xchar   *argv[];
X{
X	int     i, qflag = FALSE;
X	struct passwd  *getpwuid (), *pwd;
X
X	if (argc == 1) {
X		if ((pwd = getpwuid (getuid ())) == NULL) {
X			perror (*argv);
X			exit (1);
X		}
X		printf ("You have ");
X		exit (status (pwd -> pw_name,qflag));
X	}
X	else {
X		if (*argv[1] == '-') {
X			if (!strcmp("-q",argv[1])) {
X				qflag = TRUE;
X				if ((pwd = getpwuid (getuid ())) == NULL) {
X					perror (*argv);
X					exit (1);
X				}
X				(void)status (pwd -> pw_name,qflag);
X			}
X			else
X				fprintf(stderr,"Usage: %s [ -q ] [ user ... ]\n", argv[0]);
X		}
X		else
X			for (i = 1; i < argc; i++) {
X				printf ("%s has ", argv[i]);
X				(void)status (argv[i],qflag);
X		}
X	}
X}
X
Xstatus (name,flag)
Xchar	*name;
Xint		flag;
X{
X    char    path[1024];
X    struct stat st;
X
X    (void) sprintf (path, "%s/%s", MAILDIR, name);
X    if (stat (path, &st)) {
X		if (flag == FALSE)
X			printf ("no mailbox.\n");
X		return (0);
X    }
X    if (st.st_size == 0) {
X		if (flag == FALSE)
X			printf ("no mail.\n");
X	}
X    else
X		if (st.st_mtime > st.st_atime) {
X			if (flag == FALSE)
X				printf ("new mail.\n");
X			return (1);
X		}
X		else
X			if (flag == FALSE)
X				printf ("mail.\n");
X	return(0);
X}
//E*O*F checkmail.c//

exit 0

tim@hoptoad.uucp (Tim Maroney) (08/15/86)

It's been a while since I mentioned this trick I worked up a cuple of years
ago, so I thought I'd distribute it again:

The problem is that most of us have mailboxes which have back mail in them,
so the login message "You have mail." tells us nothing.  Scanning the
mailbox takes too long on login.  What we really want to know is whether
mail arrived while we were logged out.

A few commands in the .login and .logout scripts suffices to do this easily,
quickly, and efficiently.  (This version is for csh; I'm sure a similar
approach would work for sh, but I don't feel like researching that silly old
thing...)  The approach is that at logout you touch an empty file in your
home directory, and at login compare the date of the file to the date of
your mailbox.  If the date on the mailbox is newer, then new mail has
arrived, and the login script so informs you.

Here is the line in the .logout file, where the name of the empty file is
".m" in the home directory:

touch ~/.m

And here are the commands in the .login file:

set m=`ls -t $mail .m`
if ($m[1] == /usr/spool/mail/tim) then
	echo Read your mail, why don\'t you\?
endif

To start up the system, you will have to create the .m file in your home
directory; the easiest way is probably "cat > .m" followed by an end of
file.
-- 
Tim Maroney, Electronic Village Idiot
{ihnp4,sun,well,ptsfa,lll-crg,frog}!hoptoad!tim (uucp)
hoptoad!tim@lll-crg (arpa)

Give me food, or give me slack (or kill me).

shop@uwmcsd1.UUCP (Thomas Krueger) (08/15/86)

> To start up the system, you will have to create the .m file in your home
> directory; the easiest way is probably "cat > .m" followed by an end of
> file.
> -- 
> Tim Maroney, Electronic Village Idiot
> {ihnp4,sun,well,ptsfa,lll-crg,frog}!hoptoad!tim (uucp)
> hoptoad!tim@lll-crg (arpa)
> 
> Give me food, or give me slack (or kill me).

Another coupla ways is to cp /dev/null .m , or on Berkeley systems, touch
will create the zero length file to begin with.

						- Tom
-- 
Thomas Krueger				...ihnp4!uwmcsd1!shop	or
University of Wisconsin Milwaukee	uwmcsd1!shop@rsch.wisc.EDU
Computing Services Electronics Shop
3200 N. Cramer St.			(414) 963-5172
Milwaukee Wi 53211

msb@dciem.UUCP (Mark Brader) (08/19/86)

> > To start up the system, you will have to create the .m file ...
> Another coupla ways is to cp /dev/null .m , or ...

Not safely portable.  On some versions of UNIX (V7 at least), cp copies
the mode as well as the contents, without regard to your umask, so you
end up with .m world-writable.  Of course, for sh users, >.m is simplest.

Mark Brader

cjp@vax135.UUCP (Charles Poirier) (08/19/86)

> ... you will have to create the .m file in your home
> directory; the easiest way is probably "cat > .m" followed by an end of
> file.
> Tim Maroney, Electronic Village Idiot

If one isn't using that silly csh, one can simply do "> .m" to create .m.
(Aren't I picky today!  Btw I just spent 10 min. not finding .logout for ksh.)

nonh@utzoo.UUCP (Chris Robertson) (08/24/86)

In article <1256@uwmcsd1.UUCP> shop@uwmcsd1.UUCP (Thomas Krueger) writes:
>> Tim Maroney writes:
>> ... the easiest way [to get an empty file] is probably "cat > .m"
>> followed by an end of file.
>
>Another coupla ways is to cp /dev/null .m , or on Berkeley systems, touch
>will create the zero length file to begin with.
>
Try using ": > file" -- the file is guaranteed empty, and it works for
any shell I know of.
-- 
Christine Robertson {linus, decvax, ihnp4}!utzoo!{nonh, toram!chris}

Cat, n. - lapwarmer with built-in buzzer.

rbj@icst-cmr (Root Boy Jim) (09/02/86)

	Bill Silvert suggests using the ".mailrc" file to keep track of
	the last time you checked for mail programmatically.

Neat suggestion.

	Well...this is not a real good technique since evil things can
	happen when the user decides to edit their `.mailrc' file.

Oops, didn't think of that.

	Instead, I recommend people using "newmail" or "wnewmail" from
	the Elm Mail System package, available through your local
	mod.sources site :-)

	Ah! You say, How often do people touch their .mailrc file?
	You'd be suprised...IF I were using a mailer that used a
	.mailrc file I'd be changing it weekly - since I have a LOT of
	aliases and am constantly changing them as people move, as new
	routes turn out to be better, and as I find new people worth
	keeping track of...

True, but if you're playing with your .mailrc you must have just read
mail, or are just about to read it. In any case, it doesn't matter.

						-- Dave Taylor
	
				unbiased author of the Elm Mail System 
	

	(Root Boy) Jim Cottrell		<rbj@icst-cmr.arpa>
He is the MELBA-BEING...the ANGEL CAKE...XEROX him...XEROX him --