xref: /original-bsd/bin/sh/mail.c (revision b3c06cab)
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.2 (Berkeley) 05/04/95";
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 	int silent;
46 {
47 	register int i;
48 	char *mpath;
49 	char *p;
50 	register char *q;
51 	struct stackmark smark;
52 	struct stat statb;
53 
54 	if (silent)
55 		nmboxes = 10;
56 	if (nmboxes == 0)
57 		return;
58 	setstackmark(&smark);
59 	mpath = mpathset()? mpathval() : mailval();
60 	for (i = 0 ; i < nmboxes ; i++) {
61 		p = padvance(&mpath, nullstr);
62 		if (p == NULL)
63 			break;
64 		if (*p == '\0')
65 			continue;
66 		for (q = p ; *q ; q++);
67 		if (q[-1] != '/')
68 			abort();
69 		q[-1] = '\0';			/* delete trailing '/' */
70 #ifdef notdef /* this is what the System V shell claims to do (it lies) */
71 		if (stat(p, &statb) < 0)
72 			statb.st_mtime = 0;
73 		if (statb.st_mtime > mailtime[i] && ! silent) {
74 			out2str(pathopt? pathopt : "you have mail");
75 			out2c('\n');
76 		}
77 		mailtime[i] = statb.st_mtime;
78 #else /* this is what it should do */
79 		if (stat(p, &statb) < 0)
80 			statb.st_size = 0;
81 		if (statb.st_size > mailtime[i] && ! silent) {
82 			out2str(pathopt? pathopt : "you have mail");
83 			out2c('\n');
84 		}
85 		mailtime[i] = statb.st_size;
86 #endif
87 	}
88 	nmboxes = i;
89 	popstackmark(&smark);
90 }
91