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 * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)mail.c	8.2 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
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() */
47149017Sstefanf#include "mail.h"
481556Srgrimes#include "var.h"
491556Srgrimes#include "output.h"
501556Srgrimes#include "memalloc.h"
511556Srgrimes#include "error.h"
521556Srgrimes#include <sys/types.h>
531556Srgrimes#include <sys/stat.h>
5478469Sdes#include <stdlib.h>
551556Srgrimes
561556Srgrimes
571556Srgrimes#define MAXMBOXES 10
581556Srgrimes
591556Srgrimes
60213760Sobrienstatic int nmboxes;			/* number of mailboxes */
61213760Sobrienstatic time_t mailtime[MAXMBOXES];	/* times of mailboxes */
621556Srgrimes
631556Srgrimes
641556Srgrimes
651556Srgrimes/*
661556Srgrimes * Print appropriate message(s) if mail has arrived.  If the argument is
671556Srgrimes * nozero, then the value of MAIL has changed, so we just update the
681556Srgrimes * values.
691556Srgrimes */
701556Srgrimes
711556Srgrimesvoid
7290111Simpchkmail(int silent)
7317987Speter{
7425222Ssteve	int i;
75200956Sjilles	const char *mpath;
761556Srgrimes	char *p;
7725222Ssteve	char *q;
781556Srgrimes	struct stackmark smark;
791556Srgrimes	struct stat statb;
801556Srgrimes
811556Srgrimes	if (silent)
821556Srgrimes		nmboxes = 10;
831556Srgrimes	if (nmboxes == 0)
841556Srgrimes		return;
851556Srgrimes	setstackmark(&smark);
861556Srgrimes	mpath = mpathset()? mpathval() : mailval();
871556Srgrimes	for (i = 0 ; i < nmboxes ; i++) {
881556Srgrimes		p = padvance(&mpath, nullstr);
891556Srgrimes		if (p == NULL)
901556Srgrimes			break;
911556Srgrimes		if (*p == '\0')
921556Srgrimes			continue;
931556Srgrimes		for (q = p ; *q ; q++);
941556Srgrimes		if (q[-1] != '/')
951556Srgrimes			abort();
961556Srgrimes		q[-1] = '\0';			/* delete trailing '/' */
971556Srgrimes#ifdef notdef /* this is what the System V shell claims to do (it lies) */
981556Srgrimes		if (stat(p, &statb) < 0)
991556Srgrimes			statb.st_mtime = 0;
1001556Srgrimes		if (statb.st_mtime > mailtime[i] && ! silent) {
1011556Srgrimes			out2str(pathopt? pathopt : "you have mail");
1021556Srgrimes			out2c('\n');
1031556Srgrimes		}
1041556Srgrimes		mailtime[i] = statb.st_mtime;
1051556Srgrimes#else /* this is what it should do */
1061556Srgrimes		if (stat(p, &statb) < 0)
1071556Srgrimes			statb.st_size = 0;
1081556Srgrimes		if (statb.st_size > mailtime[i] && ! silent) {
1091556Srgrimes			out2str(pathopt? pathopt : "you have mail");
1101556Srgrimes			out2c('\n');
1111556Srgrimes		}
1121556Srgrimes		mailtime[i] = statb.st_size;
1131556Srgrimes#endif
1141556Srgrimes	}
1151556Srgrimes	nmboxes = i;
1161556Srgrimes	popstackmark(&smark);
1171556Srgrimes}
118