1 1.9 kamil /* $NetBSD: mail.c,v 1.9 2018/05/08 16:37:59 kamil Exp $ */ 2 1.2 tls 3 1.1 jtc /* 4 1.1 jtc * Mailbox checking code by Robert J. Gibson, adapted for PD ksh by 5 1.1 jtc * John R. MacMillan 6 1.1 jtc */ 7 1.4 agc #include <sys/cdefs.h> 8 1.4 agc 9 1.4 agc #ifndef lint 10 1.9 kamil __RCSID("$NetBSD: mail.c,v 1.9 2018/05/08 16:37:59 kamil Exp $"); 11 1.4 agc #endif 12 1.4 agc 13 1.1 jtc #include "config.h" 14 1.1 jtc 15 1.1 jtc #ifdef KSH 16 1.7 kamil #include <sys/stat.h> 17 1.6 kamil #include <sys/time.h> 18 1.6 kamil #include <time.h> 19 1.6 kamil 20 1.1 jtc #include "sh.h" 21 1.1 jtc 22 1.5 jschauma #define MBMESSAGE "You have mail in $_" 23 1.1 jtc 24 1.1 jtc typedef struct mbox { 25 1.1 jtc struct mbox *mb_next; /* next mbox in list */ 26 1.1 jtc char *mb_path; /* path to mail file */ 27 1.1 jtc char *mb_msg; /* to announce arrival of new mail */ 28 1.1 jtc time_t mb_mtime; /* mtime of mail file */ 29 1.1 jtc } mbox_t; 30 1.1 jtc 31 1.1 jtc /* 32 1.1 jtc * $MAILPATH is a linked list of mboxes. $MAIL is a treated as a 33 1.1 jtc * special case of $MAILPATH, where the list has only one node. The 34 1.1 jtc * same list is used for both since they are exclusive. 35 1.1 jtc */ 36 1.1 jtc 37 1.3 hubertf static mbox_t *mplist; 38 1.3 hubertf static mbox_t mbox; 39 1.1 jtc static time_t mlastchkd; /* when mail was last checked */ 40 1.3 hubertf static time_t mailcheck_interval; 41 1.1 jtc 42 1.1 jtc static void munset ARGS((mbox_t *mlist)); /* free mlist and mval */ 43 1.1 jtc static mbox_t * mballoc ARGS((char *p, char *m)); /* allocate a new mbox */ 44 1.1 jtc static void mprintit ARGS((mbox_t *mbp)); 45 1.1 jtc 46 1.1 jtc void 47 1.1 jtc mcheck() 48 1.1 jtc { 49 1.9 kamil mbox_t *mbp; 50 1.1 jtc time_t now; 51 1.1 jtc struct tbl *vp; 52 1.1 jtc struct stat stbuf; 53 1.1 jtc 54 1.1 jtc now = time((time_t *) 0); 55 1.1 jtc if (mlastchkd == 0) 56 1.1 jtc mlastchkd = now; 57 1.3 hubertf if (now - mlastchkd >= mailcheck_interval) { 58 1.1 jtc mlastchkd = now; 59 1.1 jtc 60 1.3 hubertf if (mplist) 61 1.1 jtc mbp = mplist; 62 1.1 jtc else if ((vp = global("MAIL")) && (vp->flag & ISSET)) 63 1.1 jtc mbp = &mbox; 64 1.1 jtc else 65 1.1 jtc mbp = NULL; 66 1.1 jtc 67 1.1 jtc while (mbp) { 68 1.1 jtc if (mbp->mb_path && stat(mbp->mb_path, &stbuf) == 0 69 1.1 jtc && S_ISREG(stbuf.st_mode)) 70 1.1 jtc { 71 1.1 jtc if (stbuf.st_size 72 1.1 jtc && mbp->mb_mtime != stbuf.st_mtime 73 1.1 jtc && stbuf.st_atime <= stbuf.st_mtime) 74 1.1 jtc mprintit(mbp); 75 1.1 jtc mbp->mb_mtime = stbuf.st_mtime; 76 1.1 jtc } else { 77 1.1 jtc /* 78 1.1 jtc * Some mail readers remove the mail 79 1.1 jtc * file if all mail is read. If file 80 1.1 jtc * does not exist, assume this is the 81 1.1 jtc * case and set mtime to zero. 82 1.1 jtc */ 83 1.1 jtc mbp->mb_mtime = 0; 84 1.1 jtc } 85 1.1 jtc mbp = mbp->mb_next; 86 1.1 jtc } 87 1.1 jtc } 88 1.1 jtc } 89 1.1 jtc 90 1.1 jtc void 91 1.3 hubertf mcset(interval) 92 1.3 hubertf long interval; 93 1.3 hubertf { 94 1.3 hubertf mailcheck_interval = interval; 95 1.3 hubertf } 96 1.3 hubertf 97 1.3 hubertf void 98 1.1 jtc mbset(p) 99 1.9 kamil char *p; 100 1.1 jtc { 101 1.1 jtc struct stat stbuf; 102 1.1 jtc 103 1.1 jtc if (mbox.mb_msg) 104 1.1 jtc afree((void *)mbox.mb_msg, APERM); 105 1.3 hubertf if (mbox.mb_path) 106 1.3 hubertf afree((void *)mbox.mb_path, APERM); 107 1.3 hubertf /* Save a copy to protect from export (which munges the string) */ 108 1.3 hubertf mbox.mb_path = str_save(p, APERM); 109 1.1 jtc mbox.mb_msg = NULL; 110 1.3 hubertf if (p && stat(p, &stbuf) == 0 && S_ISREG(stbuf.st_mode)) 111 1.1 jtc mbox.mb_mtime = stbuf.st_mtime; 112 1.1 jtc else 113 1.1 jtc mbox.mb_mtime = 0; 114 1.1 jtc } 115 1.1 jtc 116 1.1 jtc void 117 1.1 jtc mpset(mptoparse) 118 1.9 kamil char *mptoparse; 119 1.1 jtc { 120 1.9 kamil mbox_t *mbp; 121 1.9 kamil char *mpath, *mmsg, *mval; 122 1.1 jtc char *p; 123 1.1 jtc 124 1.1 jtc munset( mplist ); 125 1.1 jtc mplist = NULL; 126 1.1 jtc mval = str_save(mptoparse, APERM); 127 1.1 jtc while (mval) { 128 1.1 jtc mpath = mval; 129 1.1 jtc if ((mval = strchr(mval, PATHSEP)) != NULL) { 130 1.1 jtc *mval = '\0', mval++; 131 1.1 jtc } 132 1.1 jtc /* POSIX/bourne-shell say file%message */ 133 1.1 jtc for (p = mpath; (mmsg = strchr(p, '%')); ) { 134 1.1 jtc /* a literal percent? (POSIXism) */ 135 1.1 jtc if (mmsg[-1] == '\\') { 136 1.1 jtc /* use memmove() to avoid overlap problems */ 137 1.1 jtc memmove(mmsg - 1, mmsg, strlen(mmsg) + 1); 138 1.1 jtc p = mmsg + 1; 139 1.1 jtc continue; 140 1.1 jtc } 141 1.1 jtc break; 142 1.1 jtc } 143 1.1 jtc /* at&t ksh says file?message */ 144 1.1 jtc if (!mmsg && !Flag(FPOSIX)) 145 1.1 jtc mmsg = strchr(mpath, '?'); 146 1.1 jtc if (mmsg) { 147 1.1 jtc *mmsg = '\0'; 148 1.1 jtc mmsg++; 149 1.1 jtc } 150 1.1 jtc mbp = mballoc(mpath, mmsg); 151 1.1 jtc mbp->mb_next = mplist; 152 1.1 jtc mplist = mbp; 153 1.1 jtc } 154 1.1 jtc } 155 1.1 jtc 156 1.1 jtc static void 157 1.1 jtc munset(mlist) 158 1.9 kamil mbox_t *mlist; 159 1.1 jtc { 160 1.9 kamil mbox_t *mbp; 161 1.1 jtc 162 1.1 jtc while (mlist != NULL) { 163 1.1 jtc mbp = mlist; 164 1.1 jtc mlist = mbp->mb_next; 165 1.1 jtc if (!mlist) 166 1.1 jtc afree((void *)mbp->mb_path, APERM); 167 1.1 jtc afree((void *)mbp, APERM); 168 1.1 jtc } 169 1.1 jtc } 170 1.1 jtc 171 1.1 jtc static mbox_t * 172 1.1 jtc mballoc(p, m) 173 1.1 jtc char *p; 174 1.1 jtc char *m; 175 1.1 jtc { 176 1.1 jtc struct stat stbuf; 177 1.9 kamil mbox_t *mbp; 178 1.1 jtc 179 1.1 jtc mbp = (mbox_t *)alloc(sizeof(mbox_t), APERM); 180 1.1 jtc mbp->mb_next = NULL; 181 1.1 jtc mbp->mb_path = p; 182 1.1 jtc mbp->mb_msg = m; 183 1.1 jtc if (stat(mbp->mb_path, &stbuf) == 0 && S_ISREG(stbuf.st_mode)) 184 1.1 jtc mbp->mb_mtime = stbuf.st_mtime; 185 1.1 jtc else 186 1.1 jtc mbp->mb_mtime = 0; 187 1.1 jtc return(mbp); 188 1.1 jtc } 189 1.1 jtc 190 1.1 jtc static void 191 1.1 jtc mprintit( mbp ) 192 1.1 jtc mbox_t *mbp; 193 1.1 jtc { 194 1.1 jtc struct tbl *vp; 195 1.1 jtc 196 1.3 hubertf /* Ignore setstr errors here (arbitrary) */ 197 1.8 kamil setstr((vp = local("_", false)), mbp->mb_path, KSH_RETURN_ERROR); 198 1.1 jtc 199 1.1 jtc shellf("%s\n", substitute(mbp->mb_msg ? mbp->mb_msg : MBMESSAGE, 0)); 200 1.1 jtc 201 1.1 jtc unset(vp, 0); 202 1.1 jtc } 203 1.1 jtc #endif /* KSH */ 204