xref: /minix3/bin/sh/mail.c (revision d90bee97498b3043241050f61aed100786c59df4)
1*d90bee97SLionel Sambuc /*	$NetBSD: mail.c,v 1.16 2003/08/07 09:05:33 agc Exp $	*/
2*d90bee97SLionel Sambuc 
3*d90bee97SLionel Sambuc /*-
4*d90bee97SLionel Sambuc  * Copyright (c) 1991, 1993
5*d90bee97SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*d90bee97SLionel Sambuc  *
7*d90bee97SLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*d90bee97SLionel Sambuc  * Kenneth Almquist.
9*d90bee97SLionel Sambuc  *
10*d90bee97SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*d90bee97SLionel Sambuc  * modification, are permitted provided that the following conditions
12*d90bee97SLionel Sambuc  * are met:
13*d90bee97SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*d90bee97SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*d90bee97SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*d90bee97SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*d90bee97SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*d90bee97SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*d90bee97SLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*d90bee97SLionel Sambuc  *    without specific prior written permission.
21*d90bee97SLionel Sambuc  *
22*d90bee97SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*d90bee97SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*d90bee97SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*d90bee97SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*d90bee97SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*d90bee97SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*d90bee97SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*d90bee97SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*d90bee97SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*d90bee97SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*d90bee97SLionel Sambuc  * SUCH DAMAGE.
33*d90bee97SLionel Sambuc  */
34*d90bee97SLionel Sambuc 
35*d90bee97SLionel Sambuc #include <sys/cdefs.h>
36*d90bee97SLionel Sambuc #ifndef lint
37*d90bee97SLionel Sambuc #if 0
38*d90bee97SLionel Sambuc static char sccsid[] = "@(#)mail.c	8.2 (Berkeley) 5/4/95";
39*d90bee97SLionel Sambuc #else
40*d90bee97SLionel Sambuc __RCSID("$NetBSD: mail.c,v 1.16 2003/08/07 09:05:33 agc Exp $");
41*d90bee97SLionel Sambuc #endif
42*d90bee97SLionel Sambuc #endif /* not lint */
43*d90bee97SLionel Sambuc 
44*d90bee97SLionel Sambuc /*
45*d90bee97SLionel Sambuc  * Routines to check for mail.  (Perhaps make part of main.c?)
46*d90bee97SLionel Sambuc  */
47*d90bee97SLionel Sambuc #include <sys/types.h>
48*d90bee97SLionel Sambuc #include <sys/stat.h>
49*d90bee97SLionel Sambuc #include <stdlib.h>
50*d90bee97SLionel Sambuc 
51*d90bee97SLionel Sambuc #include "shell.h"
52*d90bee97SLionel Sambuc #include "exec.h"	/* defines padvance() */
53*d90bee97SLionel Sambuc #include "var.h"
54*d90bee97SLionel Sambuc #include "output.h"
55*d90bee97SLionel Sambuc #include "memalloc.h"
56*d90bee97SLionel Sambuc #include "error.h"
57*d90bee97SLionel Sambuc #include "mail.h"
58*d90bee97SLionel Sambuc 
59*d90bee97SLionel Sambuc 
60*d90bee97SLionel Sambuc #define MAXMBOXES 10
61*d90bee97SLionel Sambuc 
62*d90bee97SLionel Sambuc 
63*d90bee97SLionel Sambuc STATIC int nmboxes;			/* number of mailboxes */
64*d90bee97SLionel Sambuc STATIC time_t mailtime[MAXMBOXES];	/* times of mailboxes */
65*d90bee97SLionel Sambuc 
66*d90bee97SLionel Sambuc 
67*d90bee97SLionel Sambuc 
68*d90bee97SLionel Sambuc /*
69*d90bee97SLionel Sambuc  * Print appropriate message(s) if mail has arrived.  If the argument is
70*d90bee97SLionel Sambuc  * nozero, then the value of MAIL has changed, so we just update the
71*d90bee97SLionel Sambuc  * values.
72*d90bee97SLionel Sambuc  */
73*d90bee97SLionel Sambuc 
74*d90bee97SLionel Sambuc void
chkmail(int silent)75*d90bee97SLionel Sambuc chkmail(int silent)
76*d90bee97SLionel Sambuc {
77*d90bee97SLionel Sambuc 	int i;
78*d90bee97SLionel Sambuc 	const char *mpath;
79*d90bee97SLionel Sambuc 	char *p;
80*d90bee97SLionel Sambuc 	char *q;
81*d90bee97SLionel Sambuc 	struct stackmark smark;
82*d90bee97SLionel Sambuc 	struct stat statb;
83*d90bee97SLionel Sambuc 
84*d90bee97SLionel Sambuc 	if (silent)
85*d90bee97SLionel Sambuc 		nmboxes = 10;
86*d90bee97SLionel Sambuc 	if (nmboxes == 0)
87*d90bee97SLionel Sambuc 		return;
88*d90bee97SLionel Sambuc 	setstackmark(&smark);
89*d90bee97SLionel Sambuc 	mpath = mpathset() ? mpathval() : mailval();
90*d90bee97SLionel Sambuc 	for (i = 0 ; i < nmboxes ; i++) {
91*d90bee97SLionel Sambuc 		p = padvance(&mpath, nullstr);
92*d90bee97SLionel Sambuc 		if (p == NULL)
93*d90bee97SLionel Sambuc 			break;
94*d90bee97SLionel Sambuc 		if (*p == '\0')
95*d90bee97SLionel Sambuc 			continue;
96*d90bee97SLionel Sambuc 		for (q = p ; *q ; q++);
97*d90bee97SLionel Sambuc 		if (q[-1] != '/')
98*d90bee97SLionel Sambuc 			abort();
99*d90bee97SLionel Sambuc 		q[-1] = '\0';			/* delete trailing '/' */
100*d90bee97SLionel Sambuc #ifdef notdef /* this is what the System V shell claims to do (it lies) */
101*d90bee97SLionel Sambuc 		if (stat(p, &statb) < 0)
102*d90bee97SLionel Sambuc 			statb.st_mtime = 0;
103*d90bee97SLionel Sambuc 		if (statb.st_mtime > mailtime[i] && ! silent) {
104*d90bee97SLionel Sambuc 			out2str(pathopt ? pathopt : "you have mail");
105*d90bee97SLionel Sambuc 			out2c('\n');
106*d90bee97SLionel Sambuc 		}
107*d90bee97SLionel Sambuc 		mailtime[i] = statb.st_mtime;
108*d90bee97SLionel Sambuc #else /* this is what it should do */
109*d90bee97SLionel Sambuc 		if (stat(p, &statb) < 0)
110*d90bee97SLionel Sambuc 			statb.st_size = 0;
111*d90bee97SLionel Sambuc 		if (statb.st_size > mailtime[i] && ! silent) {
112*d90bee97SLionel Sambuc 			out2str(pathopt ? pathopt : "you have mail");
113*d90bee97SLionel Sambuc 			out2c('\n');
114*d90bee97SLionel Sambuc 		}
115*d90bee97SLionel Sambuc 		mailtime[i] = statb.st_size;
116*d90bee97SLionel Sambuc #endif
117*d90bee97SLionel Sambuc 	}
118*d90bee97SLionel Sambuc 	nmboxes = i;
119*d90bee97SLionel Sambuc 	popstackmark(&smark);
120*d90bee97SLionel Sambuc }
121