[comp.sources.misc] v07i090: A Simple Mailwatcher

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (07/22/89)

Posting-number: Volume 7, Issue 90
Submitted-by: sam@lfcs.ed.ac.uk ("S. Manoharan")
Archive-name: mailwatcher

[[A csh script?  One would think that the author's system would have "biff".
  ++bsa]]

This is a mailwatcher that keeps checking the mbox for the
arrival of new mails. There is a shell script ( slower )
and a C source. 

S. Manoharan ( sam@lfcs.edinburgh.ac.uk )

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  mailwatcher.csh mailwatcher.c makefile
# Wrapped by sam@cheops on Wed Jul 12 14:06:09 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f mailwatcher.csh -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"mailwatcher.csh\"
else
echo shar: Extracting \"mailwatcher.csh\" \(405 characters\)
sed "s/^X//" >mailwatcher.csh <<'END_OF_mailwatcher.csh'
X#
X# mailwatcher (c) s. manoharan edinburgh u
X#             sam@lfcs.edinburgh.ac.uk
X# 
X
Xonintr - ;
Xset username = `whoami`;
Xset oldmailsize = 0;
Xset alert = "";
X
Xwhile ( 1 )
X   set newmailsize = `wc -c < /usr/spool/mail/$username`;
X
X   if ( $newmailsize > $oldmailsize ) then
X      echo $alert;
X      echo "You have new mail";
X      from;
X   endif 
X   set oldmailsize = $newmailsize;
X   sleep 60;
Xend
X
END_OF_mailwatcher.csh
echo shar: 3 control characters may be missing from \"mailwatcher.csh\"
if test 405 -ne `wc -c <mailwatcher.csh`; then
    echo shar: \"mailwatcher.csh\" unpacked with wrong size!
fi
chmod +x mailwatcher.csh
# end of overwriting check
fi
if test -f mailwatcher.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"mailwatcher.c\"
else
echo shar: Extracting \"mailwatcher.c\" \(3210 characters\)
sed "s/^X//" >mailwatcher.c <<'END_OF_mailwatcher.c'
X/* mailwatcher.c -- author: sam@lfcs.edinburgh.ac.uk */
X
X#ifndef lint
Xstatic char *scid = "s. manoharan edinburgh u";
X#endif
X
X#define VERSION		1
X#define EDITION		0
X#define PATCH		0
X
X#include <stdio.h>
X
X#define UNSIZE	8
X#define SLEEP	60
X#define SPOOL	"/usr/spool/mail/"
X#define ALERT	""
X
X
Xtypedef enum { false = 0, true = 1 } Boolean;
X
X
Xvoid main(argc,argv)
Xint argc; char *argv[];
X{
X   int opch; extern int Optind; extern char *Optarg;
X   char *mbox; unsigned seconds = SLEEP; FILE *fp;
X   int oldsize = 0, newsize;
X   char *getenv(), *malloc(), *strcpy(), *strcat();
X   void Usage();
X
X   while ( ( opch = Getopt(argc,argv,"Vn:") ) != -1 )
X      switch ( opch ) {
X      case 'V'   :
X	 (void)fprintf(stderr,"%s version %d.%d.%d  ",argv[0],
X	    VERSION, EDITION, PATCH);
X	 (void)fprintf(stderr,"(c) s. manoharan  edinburgh univ\n");
X	 exit(0);
X	 break;
X      case 'n'	:
X	 if ( ( seconds = (unsigned)atoi(Optarg) ) == 0 )
X	    seconds = SLEEP;
X	 break;
X      default    :
X	 Usage(argv[0]);
X	 exit(0);
X      } /* ensw */
X
X   if ( ( mbox = malloc(sizeof(SPOOL)+UNSIZE) ) != (char *)0 ) {
X      (void)strcpy(mbox,SPOOL);
X      (void)strcat(mbox,getenv("USER"));
X   }
X   else {
X      (void)fprintf(stderr,"%s: cannot get space\n",argv[0]);
X      exit(0);
X   }
X
X   for ( ; ; ) {
X
X      /* check size of SPOOL/USER */
X      if ( (fp = fopen(mbox,"r")) != (FILE *)0 ) {
X	 newsize = 0;
X	 while ( getc(fp) != EOF )
X	    ++newsize;
X	 if ( newsize > oldsize )
X	    (void)printf("%s\nYou have new mail\n",ALERT);
X	 oldsize = newsize;
X      }
X      else {
X	 (void)fprintf(stderr,"%s: cannot open %s\n",argv[0],mbox);
X	 exit(0);
X      }
X      (void)fclose(fp);
X      sleep(seconds); /* sleep for a while */
X   } /* enfo */
X
X} /* enma */
X
Xvoid Usage(progname)
Xchar *progname;
X{
X
X   (void)fprintf(stderr,"usage: %s [-V] [-n seconds]\n",progname);
X   (void)fprintf(stderr,"\t-V\t\t: print version and exit\n");
X   (void)fprintf(stderr,"\t-n t\t\t: lie dormant for `t' seconds\n");
X} /* enUsage */
X
X/* ------------------------------------------------------ */
X
X
X
Xchar *Optarg; int Optind;
X
Xint Getopt(argc,argv,options)
Xint argc; char **argv; char *options;
X{
X   char *str, *ptr; char opch; char *Strchr();
X   static int flag = 0; static int Argc; static char **Argv;
X
X   if (  flag == 0 ) {
X      Argc = argc; Argv = argv; flag = 1; Optind = 1;
X   }
X
X   if ( Argc <= 1 ) return -1;
X
X   if ( --Argc >= 1 ) {
X      str = *++Argv;
X      if (*str != '-') return -1; /* argument is not an option   */
X      else {  /* argument is an option */
X	 if ( ( ptr = Strchr(options, opch = *++str) ) != (char *) 0 ) {
X	    ++Optind;
X            Optarg = ++str; /* point to rest of argument if any  */
X            if ( ( *++ptr == ':' ) && ( *Optarg == '\0' ) ) {
X               if (--Argc <= 0) return '?';
X               Optarg = *++Argv; ++Optind;
X            }
X	    return opch;
X         }
X	 else if ( opch == '-' ) { /* end of options */
X	    ++Optind;
X	    return -1;
X	 }
X         else return '?';
X      }
X   }
X   return 0; /* this will never be reached */
X
X} /* EnGetopt */
X
Xchar *Strchr(s,c)
Xchar *s; char c;
X{
X   while ( *s != '\0' ) {
X      if ( *s == c ) return s;
X      else ++s;
X   }
X   return ( (char *) 0 );
X} /* EnStrchr */
X
END_OF_mailwatcher.c
echo shar: 3 control characters may be missing from \"mailwatcher.c\"
if test 3210 -ne `wc -c <mailwatcher.c`; then
    echo shar: \"mailwatcher.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f makefile -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"makefile\"
else
echo shar: Extracting \"makefile\" \(70 characters\)
sed "s/^X//" >makefile <<'END_OF_makefile'
XCFLAGS= -O
Xinstall: mw
X
Xmw: mailwatcher.o
X	cc -s -o mw mailwatcher.o
X
END_OF_makefile
if test 70 -ne `wc -c <makefile`; then
    echo shar: \"makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0
Janet: sam@uk.ac.ed.lfcs                      S. Manoharan
Uucp : ..!mcvax!ukc!lfcs!sam                  Dept of Computer Science
Arpa : sam%lfcs.ed.ac.uk@nsfnet-relay.ac.uk   University of Edinburgh
Voice: 031-667 5076 (home)                    Edinburgh EH9 3JZ    UK.