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