Home | History | Annotate | Line # | Download | only in sh
      1 /*	$NetBSD: mail.c,v 1.18 2017/06/04 20:28:13 kre 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)mail.c	8.2 (Berkeley) 5/4/95";
     39 #else
     40 __RCSID("$NetBSD: mail.c,v 1.18 2017/06/04 20:28:13 kre Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 /*
     45  * Routines to check for mail.  (Perhaps make part of main.c?)
     46  */
     47 #include <sys/types.h>
     48 #include <sys/stat.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 
     52 #include "shell.h"
     53 #include "exec.h"	/* defines padvance() */
     54 #include "var.h"
     55 #include "output.h"
     56 #include "memalloc.h"
     57 #include "error.h"
     58 #include "mail.h"
     59 
     60 
     61 #define MAXMBOXES 10
     62 
     63 
     64 STATIC int nmboxes;			/* number of mailboxes */
     65 STATIC off_t mailsize[MAXMBOXES];	/* sizes 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(int silent)
     77 {
     78 	int i;
     79 	const char *mpath;
     80 	char *p;
     81 	char *q;
     82 	struct stackmark smark;
     83 	struct stat statb;
     84 
     85 	if (silent)
     86 		nmboxes = 10;
     87 	if (nmboxes == 0)
     88 		return;
     89 	setstackmark(&smark);
     90 	mpath = mpathset() ? mpathval() : mailval();
     91 	for (i = 0 ; i < nmboxes ; i++) {
     92 		p = padvance(&mpath, nullstr, 1);
     93 		if (p == NULL)
     94 			break;
     95 		stunalloc(p);
     96 		if (*p == '\0')
     97 			continue;
     98 		for (q = p ; *q ; q++)
     99 			;
    100 		if (q[-1] != '/')
    101 			abort();
    102 		q[-1] = '\0';			/* delete trailing '/' */
    103 #ifdef notdef /* this is what the System V shell claims to do (it lies) */
    104 		if (stat(p, &statb) < 0)
    105 			statb.st_mtime = 0;
    106 		if (statb.st_mtime > mailtime[i] && ! silent) {
    107 			out2str(pathopt ? pathopt : "you have mail");
    108 			out2c('\n');
    109 		}
    110 		mailtime[i] = statb.st_mtime;
    111 #else /* this is what it should do */
    112 		if (stat(p, &statb) < 0)
    113 			statb.st_size = 0;
    114 		if (statb.st_size > mailsize[i] && ! silent) {
    115 			const char *pp;
    116 
    117 			if ((pp = pathopt) != NULL) {
    118 				int len = 0;
    119 
    120 				while (*pp && *pp != ':')
    121 					len++, pp++;
    122 				if (len == 0) {
    123 					CHECKSTRSPACE(16, p);
    124 					strcat(p, ": file changed");
    125 				} else {
    126 					while (stackblocksize() <= len)
    127 						growstackblock();
    128 					p = stackblock();
    129 					memcpy(p, pathopt, len);
    130 					p[len] = '\0';
    131 				}
    132 				pp = p;
    133 			} else
    134 				pp = "you have mail";
    135 
    136 			out2str(pp);
    137 			out2c('\n');
    138 		}
    139 		mailsize[i] = statb.st_size;
    140 #endif
    141 	}
    142 	nmboxes = i;
    143 	popstackmark(&smark);
    144 }
    145