home *** CD-ROM | disk | FTP | other *** search
- /* mailwatcher.c -- author: sam@lfcs.edinburgh.ac.uk */
-
- #ifndef lint
- static char *scid = "s. manoharan edinburgh u";
- #endif
-
- #define VERSION 1
- #define EDITION 0
- #define PATCH 0
-
- #include <stdio.h>
-
- #define UNSIZE 8
- #define SLEEP 60
- #define SPOOL "/usr/spool/mail/"
- #define ALERT ""
-
-
- typedef enum { false = 0, true = 1 } Boolean;
-
-
- void main(argc,argv)
- int argc; char *argv[];
- {
- int opch; extern int Optind; extern char *Optarg;
- char *mbox; unsigned seconds = SLEEP; FILE *fp;
- int oldsize = 0, newsize;
- char *getenv(), *malloc(), *strcpy(), *strcat();
- void Usage();
-
- while ( ( opch = Getopt(argc,argv,"Vn:") ) != -1 )
- switch ( opch ) {
- case 'V' :
- (void)fprintf(stderr,"%s version %d.%d.%d ",argv[0],
- VERSION, EDITION, PATCH);
- (void)fprintf(stderr,"(c) s. manoharan edinburgh univ\n");
- exit(0);
- break;
- case 'n' :
- if ( ( seconds = (unsigned)atoi(Optarg) ) == 0 )
- seconds = SLEEP;
- break;
- default :
- Usage(argv[0]);
- exit(0);
- } /* ensw */
-
- if ( ( mbox = malloc(sizeof(SPOOL)+UNSIZE) ) != (char *)0 ) {
- (void)strcpy(mbox,SPOOL);
- (void)strcat(mbox,getenv("USER"));
- }
- else {
- (void)fprintf(stderr,"%s: cannot get space\n",argv[0]);
- exit(0);
- }
-
- for ( ; ; ) {
-
- /* check size of SPOOL/USER */
- if ( (fp = fopen(mbox,"r")) != (FILE *)0 ) {
- newsize = 0;
- while ( getc(fp) != EOF )
- ++newsize;
- if ( newsize > oldsize )
- (void)printf("%s\nYou have new mail\n",ALERT);
- oldsize = newsize;
- }
- else {
- (void)fprintf(stderr,"%s: cannot open %s\n",argv[0],mbox);
- exit(0);
- }
- (void)fclose(fp);
- sleep(seconds); /* sleep for a while */
- } /* enfo */
-
- } /* enma */
-
- void Usage(progname)
- char *progname;
- {
-
- (void)fprintf(stderr,"usage: %s [-V] [-n seconds]\n",progname);
- (void)fprintf(stderr,"\t-V\t\t: print version and exit\n");
- (void)fprintf(stderr,"\t-n t\t\t: lie dormant for `t' seconds\n");
- } /* enUsage */
-
- /* ------------------------------------------------------ */
-
-
-
- char *Optarg; int Optind;
-
- int Getopt(argc,argv,options)
- int argc; char **argv; char *options;
- {
- char *str, *ptr; char opch; char *Strchr();
- static int flag = 0; static int Argc; static char **Argv;
-
- if ( flag == 0 ) {
- Argc = argc; Argv = argv; flag = 1; Optind = 1;
- }
-
- if ( Argc <= 1 ) return -1;
-
- if ( --Argc >= 1 ) {
- str = *++Argv;
- if (*str != '-') return -1; /* argument is not an option */
- else { /* argument is an option */
- if ( ( ptr = Strchr(options, opch = *++str) ) != (char *) 0 ) {
- ++Optind;
- Optarg = ++str; /* point to rest of argument if any */
- if ( ( *++ptr == ':' ) && ( *Optarg == '\0' ) ) {
- if (--Argc <= 0) return '?';
- Optarg = *++Argv; ++Optind;
- }
- return opch;
- }
- else if ( opch == '-' ) { /* end of options */
- ++Optind;
- return -1;
- }
- else return '?';
- }
- }
- return 0; /* this will never be reached */
-
- } /* EnGetopt */
-
- char *Strchr(s,c)
- char *s; char c;
- {
- while ( *s != '\0' ) {
- if ( *s == c ) return s;
- else ++s;
- }
- return ( (char *) 0 );
- } /* EnStrchr */
-
-