mail.c revision 90111
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
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)mail.c	8.2 (Berkeley) 5/4/95";
4036150Scharnier#endif
4136150Scharnierstatic const char rcsid[] =
4250471Speter  "$FreeBSD: head/bin/sh/mail.c 90111 2002-02-02 06:50:57Z imp $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
451556Srgrimes/*
461556Srgrimes * Routines to check for mail.  (Perhaps make part of main.c?)
471556Srgrimes */
481556Srgrimes
491556Srgrimes#include "shell.h"
501556Srgrimes#include "exec.h"	/* defines padvance() */
511556Srgrimes#include "var.h"
521556Srgrimes#include "output.h"
531556Srgrimes#include "memalloc.h"
541556Srgrimes#include "error.h"
551556Srgrimes#include <sys/types.h>
561556Srgrimes#include <sys/stat.h>
5778469Sdes#include <stdlib.h>
581556Srgrimes
591556Srgrimes
601556Srgrimes#define MAXMBOXES 10
611556Srgrimes
621556Srgrimes
631556SrgrimesSTATIC int nmboxes;			/* number of mailboxes */
641556SrgrimesSTATIC time_t mailtime[MAXMBOXES];	/* times of mailboxes */
651556Srgrimes
661556Srgrimes
671556Srgrimes
681556Srgrimes/*
691556Srgrimes * Print appropriate message(s) if mail has arrived.  If the argument is
701556Srgrimes * nozero, then the value of MAIL has changed, so we just update the
711556Srgrimes * values.
721556Srgrimes */
731556Srgrimes
741556Srgrimesvoid
7590111Simpchkmail(int silent)
7617987Speter{
7725222Ssteve	int i;
781556Srgrimes	char *mpath;
791556Srgrimes	char *p;
8025222Ssteve	char *q;
811556Srgrimes	struct stackmark smark;
821556Srgrimes	struct stat statb;
831556Srgrimes
841556Srgrimes	if (silent)
851556Srgrimes		nmboxes = 10;
861556Srgrimes	if (nmboxes == 0)
871556Srgrimes		return;
881556Srgrimes	setstackmark(&smark);
891556Srgrimes	mpath = mpathset()? mpathval() : mailval();
901556Srgrimes	for (i = 0 ; i < nmboxes ; i++) {
911556Srgrimes		p = padvance(&mpath, nullstr);
921556Srgrimes		if (p == NULL)
931556Srgrimes			break;
941556Srgrimes		if (*p == '\0')
951556Srgrimes			continue;
961556Srgrimes		for (q = p ; *q ; q++);
971556Srgrimes		if (q[-1] != '/')
981556Srgrimes			abort();
991556Srgrimes		q[-1] = '\0';			/* delete trailing '/' */
1001556Srgrimes#ifdef notdef /* this is what the System V shell claims to do (it lies) */
1011556Srgrimes		if (stat(p, &statb) < 0)
1021556Srgrimes			statb.st_mtime = 0;
1031556Srgrimes		if (statb.st_mtime > mailtime[i] && ! silent) {
1041556Srgrimes			out2str(pathopt? pathopt : "you have mail");
1051556Srgrimes			out2c('\n');
1061556Srgrimes		}
1071556Srgrimes		mailtime[i] = statb.st_mtime;
1081556Srgrimes#else /* this is what it should do */
1091556Srgrimes		if (stat(p, &statb) < 0)
1101556Srgrimes			statb.st_size = 0;
1111556Srgrimes		if (statb.st_size > mailtime[i] && ! silent) {
1121556Srgrimes			out2str(pathopt? pathopt : "you have mail");
1131556Srgrimes			out2c('\n');
1141556Srgrimes		}
1151556Srgrimes		mailtime[i] = statb.st_size;
1161556Srgrimes#endif
1171556Srgrimes	}
1181556Srgrimes	nmboxes = i;
1191556Srgrimes	popstackmark(&smark);
1201556Srgrimes}
121