/*-1* Copyright (c) 1991, 19932* The Regents of the University of California. All rights reserved.3*4* This code is derived from software contributed to Berkeley by5* Kenneth Almquist.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. Neither the name of the University nor the names of its contributors16* may be used to endorse or promote products derived from this software17* without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132/*33* Routines to check for mail. (Perhaps make part of main.c?)34*/3536#include "shell.h"37#include "mail.h"38#include "var.h"39#include "output.h"40#include "memalloc.h"41#include "error.h"42#include <sys/types.h>43#include <sys/stat.h>44#include <stdlib.h>454647#define MAXMBOXES 10484950static int nmboxes; /* number of mailboxes */51static time_t mailtime[MAXMBOXES]; /* times of mailboxes */52535455/*56* Print appropriate message(s) if mail has arrived. If the argument is57* non-zero, then the value of MAIL has changed, so we just update the58* values.59*/6061void62chkmail(int silent)63{64int i;65char *mpath;66char *p;67char *msg;68struct stackmark smark;69struct stat statb;7071if (silent)72nmboxes = 10;73if (nmboxes == 0)74return;75setstackmark(&smark);76mpath = stsavestr(mpathset()? mpathval() : mailval());77for (i = 0 ; i < nmboxes ; i++) {78p = mpath;79if (*p == '\0')80break;81mpath = strchrnul(mpath, ':');82if (*mpath != '\0') {83*mpath++ = '\0';84if (p == mpath - 1)85continue;86}87msg = strchr(p, '%');88if (msg != NULL)89*msg++ = '\0';90#ifdef notdef /* this is what the System V shell claims to do (it lies) */91if (stat(p, &statb) < 0)92statb.st_mtime = 0;93if (statb.st_mtime > mailtime[i] && ! silent) {94out2str(msg? msg : "you have mail");95out2c('\n');96}97mailtime[i] = statb.st_mtime;98#else /* this is what it should do */99if (stat(p, &statb) < 0)100statb.st_size = 0;101if (statb.st_size > mailtime[i] && ! silent) {102out2str(msg? msg : "you have mail");103out2c('\n');104}105mailtime[i] = statb.st_size;106#endif107}108nmboxes = i;109popstackmark(&smark);110}111112113