xref: /original-bsd/bin/sh/mail.c (revision 68a7e0e7)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)mail.c	8.1 (Berkeley) 05/31/93";
13 #endif /* not lint */
14 
15 /*
16  * Routines to check for mail.  (Perhaps make part of main.c?)
17  */
18 
19 #include "shell.h"
20 #include "exec.h"	/* defines padvance() */
21 #include "var.h"
22 #include "output.h"
23 #include "memalloc.h"
24 #include "error.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 
28 
29 #define MAXMBOXES 10
30 
31 
32 STATIC int nmboxes;			/* number of mailboxes */
33 STATIC time_t mailtime[MAXMBOXES];	/* times of mailboxes */
34 
35 
36 
37 /*
38  * Print appropriate message(s) if mail has arrived.  If the argument is
39  * nozero, then the value of MAIL has changed, so we just update the
40  * values.
41  */
42 
43 void
44 chkmail(silent) {
45 	register int i;
46 	char *mpath;
47 	char *p;
48 	register char *q;
49 	struct stackmark smark;
50 	struct stat statb;
51 
52 	if (silent)
53 		nmboxes = 10;
54 	if (nmboxes == 0)
55 		return;
56 	setstackmark(&smark);
57 	mpath = mpathset()? mpathval() : mailval();
58 	for (i = 0 ; i < nmboxes ; i++) {
59 		p = padvance(&mpath, nullstr);
60 		if (p == NULL)
61 			break;
62 		if (*p == '\0')
63 			continue;
64 		for (q = p ; *q ; q++);
65 		if (q[-1] != '/')
66 			abort();
67 		q[-1] = '\0';			/* delete trailing '/' */
68 #ifdef notdef /* this is what the System V shell claims to do (it lies) */
69 		if (stat(p, &statb) < 0)
70 			statb.st_mtime = 0;
71 		if (statb.st_mtime > mailtime[i] && ! silent) {
72 			out2str(pathopt? pathopt : "you have mail");
73 			out2c('\n');
74 		}
75 		mailtime[i] = statb.st_mtime;
76 #else /* this is what it should do */
77 		if (stat(p, &statb) < 0)
78 			statb.st_size = 0;
79 		if (statb.st_size > mailtime[i] && ! silent) {
80 			out2str(pathopt? pathopt : "you have mail");
81 			out2c('\n');
82 		}
83 		mailtime[i] = statb.st_size;
84 #endif
85 	}
86 	nmboxes = i;
87 	popstackmark(&smark);
88 }
89