xref: /freebsd/bin/sh/mail.c (revision 878bf23d3758244171f12e06d956c021b30a2aad)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes /*
344b88c807SRodney W. Grimes  * Routines to check for mail.  (Perhaps make part of main.c?)
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #include "shell.h"
38955e9f68SStefan Farfeleder #include "mail.h"
394b88c807SRodney W. Grimes #include "var.h"
404b88c807SRodney W. Grimes #include "output.h"
414b88c807SRodney W. Grimes #include "memalloc.h"
424b88c807SRodney W. Grimes #include "error.h"
434b88c807SRodney W. Grimes #include <sys/types.h>
444b88c807SRodney W. Grimes #include <sys/stat.h>
4526f6b0fbSDag-Erling Smørgrav #include <stdlib.h>
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #define MAXMBOXES 10
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes 
51aa7b6f82SDavid E. O'Brien static int nmboxes;			/* number of mailboxes */
52aa7b6f82SDavid E. O'Brien static time_t mailtime[MAXMBOXES];	/* times of mailboxes */
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes /*
574b88c807SRodney W. Grimes  * Print appropriate message(s) if mail has arrived.  If the argument is
58*878bf23dSWEININGCHIU  * non-zero, then the value of MAIL has changed, so we just update the
594b88c807SRodney W. Grimes  * values.
604b88c807SRodney W. Grimes  */
614b88c807SRodney W. Grimes 
624b88c807SRodney W. Grimes void
chkmail(int silent)635134c3f7SWarner Losh chkmail(int silent)
64aa9caaf6SPeter Wemm {
65afb033d5SSteve Price 	int i;
667d6f6a35SJilles Tjoelker 	char *mpath;
674b88c807SRodney W. Grimes 	char *p;
687d6f6a35SJilles Tjoelker 	char *msg;
694b88c807SRodney W. Grimes 	struct stackmark smark;
704b88c807SRodney W. Grimes 	struct stat statb;
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes 	if (silent)
734b88c807SRodney W. Grimes 		nmboxes = 10;
744b88c807SRodney W. Grimes 	if (nmboxes == 0)
754b88c807SRodney W. Grimes 		return;
764b88c807SRodney W. Grimes 	setstackmark(&smark);
777d6f6a35SJilles Tjoelker 	mpath = stsavestr(mpathset()? mpathval() : mailval());
784b88c807SRodney W. Grimes 	for (i = 0 ; i < nmboxes ; i++) {
797d6f6a35SJilles Tjoelker 		p = mpath;
804b88c807SRodney W. Grimes 		if (*p == '\0')
817d6f6a35SJilles Tjoelker 			break;
827d6f6a35SJilles Tjoelker 		mpath = strchrnul(mpath, ':');
837d6f6a35SJilles Tjoelker 		if (*mpath != '\0') {
847d6f6a35SJilles Tjoelker 			*mpath++ = '\0';
857d6f6a35SJilles Tjoelker 			if (p == mpath - 1)
864b88c807SRodney W. Grimes 				continue;
877d6f6a35SJilles Tjoelker 		}
887d6f6a35SJilles Tjoelker 		msg = strchr(p, '%');
897d6f6a35SJilles Tjoelker 		if (msg != NULL)
907d6f6a35SJilles Tjoelker 			*msg++ = '\0';
914b88c807SRodney W. Grimes #ifdef notdef /* this is what the System V shell claims to do (it lies) */
924b88c807SRodney W. Grimes 		if (stat(p, &statb) < 0)
934b88c807SRodney W. Grimes 			statb.st_mtime = 0;
944b88c807SRodney W. Grimes 		if (statb.st_mtime > mailtime[i] && ! silent) {
957d6f6a35SJilles Tjoelker 			out2str(msg? msg : "you have mail");
964b88c807SRodney W. Grimes 			out2c('\n');
974b88c807SRodney W. Grimes 		}
984b88c807SRodney W. Grimes 		mailtime[i] = statb.st_mtime;
994b88c807SRodney W. Grimes #else /* this is what it should do */
1004b88c807SRodney W. Grimes 		if (stat(p, &statb) < 0)
1014b88c807SRodney W. Grimes 			statb.st_size = 0;
1024b88c807SRodney W. Grimes 		if (statb.st_size > mailtime[i] && ! silent) {
1037d6f6a35SJilles Tjoelker 			out2str(msg? msg : "you have mail");
1044b88c807SRodney W. Grimes 			out2c('\n');
1054b88c807SRodney W. Grimes 		}
1064b88c807SRodney W. Grimes 		mailtime[i] = statb.st_size;
1074b88c807SRodney W. Grimes #endif
1084b88c807SRodney W. Grimes 	}
1094b88c807SRodney W. Grimes 	nmboxes = i;
1104b88c807SRodney W. Grimes 	popstackmark(&smark);
1114b88c807SRodney W. Grimes }
112