mail.c revision 1556
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
381556Srgrimesstatic char sccsid[] = "@(#)mail.c	8.1 (Berkeley) 5/31/93";
391556Srgrimes#endif /* not lint */
401556Srgrimes
411556Srgrimes/*
421556Srgrimes * Routines to check for mail.  (Perhaps make part of main.c?)
431556Srgrimes */
441556Srgrimes
451556Srgrimes#include "shell.h"
461556Srgrimes#include "exec.h"	/* defines padvance() */
471556Srgrimes#include "var.h"
481556Srgrimes#include "output.h"
491556Srgrimes#include "memalloc.h"
501556Srgrimes#include "error.h"
511556Srgrimes#include <sys/types.h>
521556Srgrimes#include <sys/stat.h>
531556Srgrimes
541556Srgrimes
551556Srgrimes#define MAXMBOXES 10
561556Srgrimes
571556Srgrimes
581556SrgrimesSTATIC int nmboxes;			/* number of mailboxes */
591556SrgrimesSTATIC time_t mailtime[MAXMBOXES];	/* times of mailboxes */
601556Srgrimes
611556Srgrimes
621556Srgrimes
631556Srgrimes/*
641556Srgrimes * Print appropriate message(s) if mail has arrived.  If the argument is
651556Srgrimes * nozero, then the value of MAIL has changed, so we just update the
661556Srgrimes * values.
671556Srgrimes */
681556Srgrimes
691556Srgrimesvoid
701556Srgrimeschkmail(silent) {
711556Srgrimes	register int i;
721556Srgrimes	char *mpath;
731556Srgrimes	char *p;
741556Srgrimes	register char *q;
751556Srgrimes	struct stackmark smark;
761556Srgrimes	struct stat statb;
771556Srgrimes
781556Srgrimes	if (silent)
791556Srgrimes		nmboxes = 10;
801556Srgrimes	if (nmboxes == 0)
811556Srgrimes		return;
821556Srgrimes	setstackmark(&smark);
831556Srgrimes	mpath = mpathset()? mpathval() : mailval();
841556Srgrimes	for (i = 0 ; i < nmboxes ; i++) {
851556Srgrimes		p = padvance(&mpath, nullstr);
861556Srgrimes		if (p == NULL)
871556Srgrimes			break;
881556Srgrimes		if (*p == '\0')
891556Srgrimes			continue;
901556Srgrimes		for (q = p ; *q ; q++);
911556Srgrimes		if (q[-1] != '/')
921556Srgrimes			abort();
931556Srgrimes		q[-1] = '\0';			/* delete trailing '/' */
941556Srgrimes#ifdef notdef /* this is what the System V shell claims to do (it lies) */
951556Srgrimes		if (stat(p, &statb) < 0)
961556Srgrimes			statb.st_mtime = 0;
971556Srgrimes		if (statb.st_mtime > mailtime[i] && ! silent) {
981556Srgrimes			out2str(pathopt? pathopt : "you have mail");
991556Srgrimes			out2c('\n');
1001556Srgrimes		}
1011556Srgrimes		mailtime[i] = statb.st_mtime;
1021556Srgrimes#else /* this is what it should do */
1031556Srgrimes		if (stat(p, &statb) < 0)
1041556Srgrimes			statb.st_size = 0;
1051556Srgrimes		if (statb.st_size > mailtime[i] && ! silent) {
1061556Srgrimes			out2str(pathopt? pathopt : "you have mail");
1071556Srgrimes			out2c('\n');
1081556Srgrimes		}
1091556Srgrimes		mailtime[i] = statb.st_size;
1101556Srgrimes#endif
1111556Srgrimes	}
1121556Srgrimes	nmboxes = i;
1131556Srgrimes	popstackmark(&smark);
1141556Srgrimes}
115