Home | History | Annotate | Line # | Download | only in mail
quit.c revision 1.12
      1  1.12  christos /*	$NetBSD: quit.c,v 1.12 2001/02/05 02:07:53 christos Exp $	*/
      2   1.5  christos 
      3   1.1       cgd /*
      4   1.3   deraadt  * Copyright (c) 1980, 1993
      5   1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.7     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.5  christos #if 0
     39   1.6       tls static char sccsid[] = "@(#)quit.c	8.2 (Berkeley) 4/28/95";
     40   1.5  christos #else
     41  1.12  christos __RCSID("$NetBSD: quit.c,v 1.12 2001/02/05 02:07:53 christos Exp $");
     42   1.5  christos #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include "rcv.h"
     46   1.3   deraadt #include "extern.h"
     47   1.1       cgd 
     48   1.1       cgd /*
     49   1.1       cgd  * Rcv -- receive mail rationally.
     50   1.1       cgd  *
     51   1.1       cgd  * Termination processing.
     52   1.1       cgd  */
     53   1.1       cgd 
     54  1.12  christos extern char *tmpdir;
     55  1.12  christos extern char *tempQuit, *tempResid;
     56  1.12  christos 
     57   1.1       cgd /*
     58   1.1       cgd  * The "quit" command.
     59   1.1       cgd  */
     60   1.3   deraadt int
     61   1.5  christos quitcmd(v)
     62   1.5  christos 	void *v;
     63   1.1       cgd {
     64   1.1       cgd 	/*
     65   1.1       cgd 	 * If we are sourcing, then return 1 so execute() can handle it.
     66   1.1       cgd 	 * Otherwise, return -1 to abort command loop.
     67   1.1       cgd 	 */
     68   1.1       cgd 	if (sourcing)
     69   1.1       cgd 		return 1;
     70   1.1       cgd 	return -1;
     71   1.1       cgd }
     72   1.1       cgd 
     73   1.1       cgd /*
     74   1.1       cgd  * Save all of the undetermined messages at the top of "mbox"
     75   1.1       cgd  * Save all untouched messages back in the system mailbox.
     76   1.1       cgd  * Remove the system mailbox, if none saved there.
     77   1.1       cgd  */
     78   1.3   deraadt void
     79   1.1       cgd quit()
     80   1.1       cgd {
     81   1.1       cgd 	int mcount, p, modify, autohold, anystat, holdbit, nohold;
     82   1.5  christos 	FILE *ibuf = NULL, *obuf, *fbuf, *rbuf, *readstat = NULL, *abuf;
     83   1.7     lukem 	struct message *mp;
     84   1.7     lukem 	int c;
     85   1.1       cgd 	struct stat minfo;
     86   1.1       cgd 	char *mbox;
     87   1.1       cgd 
     88   1.9       bad #ifdef __GNUC__
     89   1.9       bad 	obuf = NULL;		/* XXX gcc -Wuninitialized */
     90   1.9       bad #endif
     91   1.9       bad 
     92   1.1       cgd 	/*
     93   1.1       cgd 	 * If we are read only, we can't do anything,
     94   1.1       cgd 	 * so just return quickly.
     95   1.1       cgd 	 */
     96   1.1       cgd 	if (readonly)
     97   1.1       cgd 		return;
     98   1.1       cgd 	/*
     99   1.1       cgd 	 * If editing (not reading system mail box), then do the work
    100   1.1       cgd 	 * in edstop()
    101   1.1       cgd 	 */
    102   1.1       cgd 	if (edit) {
    103   1.1       cgd 		edstop();
    104   1.1       cgd 		return;
    105   1.1       cgd 	}
    106   1.1       cgd 
    107   1.1       cgd 	/*
    108   1.1       cgd 	 * See if there any messages to save in mbox.  If no, we
    109   1.1       cgd 	 * can save copying mbox to /tmp and back.
    110   1.1       cgd 	 *
    111   1.1       cgd 	 * Check also to see if any files need to be preserved.
    112   1.1       cgd 	 * Delete all untouched messages to keep them out of mbox.
    113   1.1       cgd 	 * If all the messages are to be preserved, just exit with
    114   1.1       cgd 	 * a message.
    115   1.1       cgd 	 */
    116   1.1       cgd 
    117   1.1       cgd 	fbuf = Fopen(mailname, "r");
    118   1.1       cgd 	if (fbuf == NULL)
    119   1.1       cgd 		goto newmail;
    120   1.5  christos 	if (flock(fileno(fbuf), LOCK_EX) == -1) {
    121   1.5  christos nolock:
    122   1.5  christos 		perror("Unable to lock mailbox");
    123   1.5  christos 		Fclose(fbuf);
    124   1.5  christos 		return;
    125   1.5  christos 	}
    126   1.5  christos 	if (dot_lock(mailname, 1, stdout, ".") == -1)
    127   1.5  christos 		goto nolock;
    128   1.1       cgd 	rbuf = NULL;
    129   1.1       cgd 	if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
    130   1.1       cgd 		printf("New mail has arrived.\n");
    131   1.1       cgd 		rbuf = Fopen(tempResid, "w");
    132   1.1       cgd 		if (rbuf == NULL || fbuf == NULL)
    133   1.1       cgd 			goto newmail;
    134   1.1       cgd #ifdef APPEND
    135   1.3   deraadt 		fseek(fbuf, (long)mailsize, 0);
    136   1.1       cgd 		while ((c = getc(fbuf)) != EOF)
    137   1.1       cgd 			(void) putc(c, rbuf);
    138   1.1       cgd #else
    139   1.1       cgd 		p = minfo.st_size - mailsize;
    140   1.1       cgd 		while (p-- > 0) {
    141   1.1       cgd 			c = getc(fbuf);
    142   1.1       cgd 			if (c == EOF)
    143   1.1       cgd 				goto newmail;
    144   1.1       cgd 			(void) putc(c, rbuf);
    145   1.1       cgd 		}
    146   1.1       cgd #endif
    147   1.8       bad 		(void) fflush(rbuf);
    148   1.8       bad 		if (ferror(rbuf)) {
    149   1.8       bad 			perror(tempResid);
    150   1.8       bad 			Fclose(rbuf);
    151   1.8       bad 			Fclose(fbuf);
    152   1.8       bad 			dot_unlock(mailname);
    153   1.8       bad 			return;
    154   1.8       bad 		}
    155   1.1       cgd 		Fclose(rbuf);
    156   1.1       cgd 		if ((rbuf = Fopen(tempResid, "r")) == NULL)
    157   1.1       cgd 			goto newmail;
    158   1.1       cgd 		rm(tempResid);
    159   1.1       cgd 	}
    160   1.1       cgd 
    161   1.1       cgd 	/*
    162   1.1       cgd 	 * Adjust the message flags in each message.
    163   1.1       cgd 	 */
    164   1.1       cgd 
    165   1.1       cgd 	anystat = 0;
    166   1.1       cgd 	autohold = value("hold") != NOSTR;
    167   1.1       cgd 	holdbit = autohold ? MPRESERVE : MBOX;
    168   1.1       cgd 	nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
    169   1.1       cgd 	if (value("keepsave") != NOSTR)
    170   1.1       cgd 		nohold &= ~MSAVED;
    171   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
    172   1.1       cgd 		if (mp->m_flag & MNEW) {
    173   1.1       cgd 			mp->m_flag &= ~MNEW;
    174   1.1       cgd 			mp->m_flag |= MSTATUS;
    175   1.1       cgd 		}
    176   1.1       cgd 		if (mp->m_flag & MSTATUS)
    177   1.1       cgd 			anystat++;
    178   1.1       cgd 		if ((mp->m_flag & MTOUCH) == 0)
    179   1.1       cgd 			mp->m_flag |= MPRESERVE;
    180   1.1       cgd 		if ((mp->m_flag & nohold) == 0)
    181   1.1       cgd 			mp->m_flag |= holdbit;
    182   1.1       cgd 	}
    183   1.1       cgd 	modify = 0;
    184   1.1       cgd 	if (Tflag != NOSTR) {
    185   1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    186   1.1       cgd 			Tflag = NOSTR;
    187   1.1       cgd 	}
    188   1.1       cgd 	for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
    189   1.1       cgd 		if (mp->m_flag & MBOX)
    190   1.1       cgd 			c++;
    191   1.1       cgd 		if (mp->m_flag & MPRESERVE)
    192   1.1       cgd 			p++;
    193   1.1       cgd 		if (mp->m_flag & MODIFY)
    194   1.1       cgd 			modify++;
    195   1.1       cgd 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    196   1.1       cgd 			char *id;
    197   1.1       cgd 
    198   1.1       cgd 			if ((id = hfield("article-id", mp)) != NOSTR)
    199   1.1       cgd 				fprintf(readstat, "%s\n", id);
    200   1.1       cgd 		}
    201   1.1       cgd 	}
    202   1.1       cgd 	if (Tflag != NOSTR)
    203   1.1       cgd 		Fclose(readstat);
    204   1.1       cgd 	if (p == msgCount && !modify && !anystat) {
    205   1.1       cgd 		printf("Held %d message%s in %s\n",
    206   1.1       cgd 			p, p == 1 ? "" : "s", mailname);
    207   1.1       cgd 		Fclose(fbuf);
    208   1.5  christos 		dot_unlock(mailname);
    209   1.1       cgd 		return;
    210   1.1       cgd 	}
    211   1.1       cgd 	if (c == 0) {
    212   1.1       cgd 		if (p != 0) {
    213   1.1       cgd 			writeback(rbuf);
    214   1.1       cgd 			Fclose(fbuf);
    215   1.5  christos 			dot_unlock(mailname);
    216   1.1       cgd 			return;
    217   1.1       cgd 		}
    218   1.1       cgd 		goto cream;
    219   1.1       cgd 	}
    220   1.1       cgd 
    221   1.1       cgd 	/*
    222   1.1       cgd 	 * Create another temporary file and copy user's mbox file
    223   1.1       cgd 	 * darin.  If there is no mbox, copy nothing.
    224   1.1       cgd 	 * If he has specified "append" don't copy his mailbox,
    225   1.1       cgd 	 * just copy saveable entries at the end.
    226   1.1       cgd 	 */
    227   1.1       cgd 
    228   1.1       cgd 	mbox = expand("&");
    229   1.1       cgd 	mcount = c;
    230   1.1       cgd 	if (value("append") == NOSTR) {
    231   1.1       cgd 		if ((obuf = Fopen(tempQuit, "w")) == NULL) {
    232   1.1       cgd 			perror(tempQuit);
    233   1.1       cgd 			Fclose(fbuf);
    234   1.5  christos 			dot_unlock(mailname);
    235   1.1       cgd 			return;
    236   1.1       cgd 		}
    237   1.1       cgd 		if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
    238   1.1       cgd 			perror(tempQuit);
    239   1.1       cgd 			rm(tempQuit);
    240   1.1       cgd 			Fclose(obuf);
    241   1.1       cgd 			Fclose(fbuf);
    242   1.5  christos 			dot_unlock(mailname);
    243   1.1       cgd 			return;
    244   1.1       cgd 		}
    245   1.1       cgd 		rm(tempQuit);
    246   1.1       cgd 		if ((abuf = Fopen(mbox, "r")) != NULL) {
    247   1.1       cgd 			while ((c = getc(abuf)) != EOF)
    248   1.1       cgd 				(void) putc(c, obuf);
    249   1.1       cgd 			Fclose(abuf);
    250   1.1       cgd 		}
    251   1.1       cgd 		if (ferror(obuf)) {
    252   1.1       cgd 			perror(tempQuit);
    253   1.1       cgd 			Fclose(ibuf);
    254   1.1       cgd 			Fclose(obuf);
    255   1.1       cgd 			Fclose(fbuf);
    256   1.5  christos 			dot_unlock(mailname);
    257   1.1       cgd 			return;
    258   1.1       cgd 		}
    259   1.1       cgd 		Fclose(obuf);
    260   1.1       cgd 		close(creat(mbox, 0600));
    261   1.1       cgd 		if ((obuf = Fopen(mbox, "r+")) == NULL) {
    262   1.1       cgd 			perror(mbox);
    263   1.1       cgd 			Fclose(ibuf);
    264   1.1       cgd 			Fclose(fbuf);
    265   1.5  christos 			dot_unlock(mailname);
    266   1.1       cgd 			return;
    267   1.1       cgd 		}
    268   1.1       cgd 	}
    269   1.5  christos 	else {
    270   1.1       cgd 		if ((obuf = Fopen(mbox, "a")) == NULL) {
    271   1.1       cgd 			perror(mbox);
    272   1.1       cgd 			Fclose(fbuf);
    273   1.5  christos 			dot_unlock(mailname);
    274   1.1       cgd 			return;
    275   1.1       cgd 		}
    276   1.1       cgd 		fchmod(fileno(obuf), 0600);
    277   1.1       cgd 	}
    278   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    279   1.1       cgd 		if (mp->m_flag & MBOX)
    280  1.11      tron 			if (sendmessage(mp, obuf, saveignore, NOSTR) < 0) {
    281   1.1       cgd 				perror(mbox);
    282   1.1       cgd 				Fclose(ibuf);
    283   1.1       cgd 				Fclose(obuf);
    284   1.1       cgd 				Fclose(fbuf);
    285   1.5  christos 				dot_unlock(mailname);
    286   1.1       cgd 				return;
    287   1.1       cgd 			}
    288   1.1       cgd 
    289   1.1       cgd 	/*
    290   1.1       cgd 	 * Copy the user's old mbox contents back
    291   1.1       cgd 	 * to the end of the stuff we just saved.
    292   1.1       cgd 	 * If we are appending, this is unnecessary.
    293   1.1       cgd 	 */
    294   1.1       cgd 
    295   1.1       cgd 	if (value("append") == NOSTR) {
    296   1.1       cgd 		rewind(ibuf);
    297   1.1       cgd 		c = getc(ibuf);
    298   1.1       cgd 		while (c != EOF) {
    299   1.1       cgd 			(void) putc(c, obuf);
    300   1.1       cgd 			if (ferror(obuf))
    301   1.1       cgd 				break;
    302   1.1       cgd 			c = getc(ibuf);
    303   1.1       cgd 		}
    304   1.1       cgd 		Fclose(ibuf);
    305   1.1       cgd 	}
    306   1.6       tls 	fflush(obuf);
    307   1.8       bad 	if (!ferror(obuf))
    308   1.8       bad 		trunc(obuf);	/* XXX or should we truncate? */
    309   1.1       cgd 	if (ferror(obuf)) {
    310   1.1       cgd 		perror(mbox);
    311   1.1       cgd 		Fclose(obuf);
    312   1.1       cgd 		Fclose(fbuf);
    313   1.5  christos 		dot_unlock(mailname);
    314   1.1       cgd 		return;
    315   1.1       cgd 	}
    316   1.1       cgd 	Fclose(obuf);
    317   1.1       cgd 	if (mcount == 1)
    318   1.1       cgd 		printf("Saved 1 message in mbox\n");
    319   1.1       cgd 	else
    320   1.1       cgd 		printf("Saved %d messages in mbox\n", mcount);
    321   1.1       cgd 
    322   1.1       cgd 	/*
    323   1.1       cgd 	 * Now we are ready to copy back preserved files to
    324   1.1       cgd 	 * the system mailbox, if any were requested.
    325   1.1       cgd 	 */
    326   1.1       cgd 
    327   1.1       cgd 	if (p != 0) {
    328   1.1       cgd 		writeback(rbuf);
    329   1.1       cgd 		Fclose(fbuf);
    330   1.5  christos 		dot_unlock(mailname);
    331   1.1       cgd 		return;
    332   1.1       cgd 	}
    333   1.1       cgd 
    334   1.1       cgd 	/*
    335   1.1       cgd 	 * Finally, remove his /usr/mail file.
    336   1.1       cgd 	 * If new mail has arrived, copy it back.
    337   1.1       cgd 	 */
    338   1.1       cgd 
    339   1.1       cgd cream:
    340   1.1       cgd 	if (rbuf != NULL) {
    341   1.1       cgd 		abuf = Fopen(mailname, "r+");
    342   1.1       cgd 		if (abuf == NULL)
    343   1.1       cgd 			goto newmail;
    344   1.1       cgd 		while ((c = getc(rbuf)) != EOF)
    345   1.1       cgd 			(void) putc(c, abuf);
    346   1.8       bad 		(void) fflush(abuf);
    347  1.10       bad 		if (ferror(abuf)) {
    348   1.8       bad 			perror(mailname);
    349   1.8       bad 			Fclose(abuf);
    350   1.8       bad 			Fclose(fbuf);
    351   1.8       bad 			dot_unlock(mailname);
    352   1.8       bad 			return;
    353   1.8       bad 		}
    354   1.1       cgd 		Fclose(rbuf);
    355   1.1       cgd 		trunc(abuf);
    356   1.1       cgd 		Fclose(abuf);
    357   1.1       cgd 		alter(mailname);
    358   1.1       cgd 		Fclose(fbuf);
    359   1.5  christos 		dot_unlock(mailname);
    360   1.1       cgd 		return;
    361   1.1       cgd 	}
    362   1.1       cgd 	demail();
    363   1.1       cgd 	Fclose(fbuf);
    364   1.5  christos 	dot_unlock(mailname);
    365   1.1       cgd 	return;
    366   1.1       cgd 
    367   1.1       cgd newmail:
    368   1.1       cgd 	printf("Thou hast new mail.\n");
    369   1.5  christos 	if (fbuf != NULL) {
    370   1.1       cgd 		Fclose(fbuf);
    371   1.5  christos 		dot_unlock(mailname);
    372   1.5  christos 	}
    373   1.1       cgd }
    374   1.1       cgd 
    375   1.1       cgd /*
    376   1.1       cgd  * Preserve all the appropriate messages back in the system
    377   1.1       cgd  * mailbox, and print a nice message indicated how many were
    378   1.1       cgd  * saved.  On any error, just return -1.  Else return 0.
    379   1.1       cgd  * Incorporate the any new mail that we found.
    380   1.1       cgd  */
    381   1.3   deraadt int
    382   1.1       cgd writeback(res)
    383   1.7     lukem 	FILE *res;
    384   1.1       cgd {
    385   1.7     lukem 	struct message *mp;
    386   1.7     lukem 	int p, c;
    387   1.1       cgd 	FILE *obuf;
    388   1.1       cgd 
    389   1.1       cgd 	p = 0;
    390   1.1       cgd 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    391   1.1       cgd 		perror(mailname);
    392   1.1       cgd 		return(-1);
    393   1.1       cgd 	}
    394   1.1       cgd #ifndef APPEND
    395   1.8       bad 	if (res != NULL) {
    396   1.1       cgd 		while ((c = getc(res)) != EOF)
    397   1.1       cgd 			(void) putc(c, obuf);
    398   1.8       bad 		(void) fflush(obuf);
    399   1.8       bad 		if (ferror(obuf)) {
    400   1.8       bad 			perror(mailname);
    401   1.8       bad 			Fclose(obuf);
    402   1.8       bad 			return(-1);
    403   1.8       bad 		}
    404   1.8       bad 	}
    405   1.1       cgd #endif
    406   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    407   1.1       cgd 		if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
    408   1.1       cgd 			p++;
    409  1.11      tron 			if (sendmessage(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
    410   1.1       cgd 				perror(mailname);
    411   1.1       cgd 				Fclose(obuf);
    412   1.1       cgd 				return(-1);
    413   1.1       cgd 			}
    414   1.1       cgd 		}
    415   1.1       cgd #ifdef APPEND
    416   1.1       cgd 	if (res != NULL)
    417   1.1       cgd 		while ((c = getc(res)) != EOF)
    418   1.1       cgd 			(void) putc(c, obuf);
    419   1.1       cgd #endif
    420   1.1       cgd 	fflush(obuf);
    421   1.8       bad 	if (!ferror(obuf))
    422  1.10       bad 		trunc(obuf);	/* XXX or should we truncate? */
    423   1.1       cgd 	if (ferror(obuf)) {
    424   1.1       cgd 		perror(mailname);
    425   1.1       cgd 		Fclose(obuf);
    426   1.1       cgd 		return(-1);
    427   1.1       cgd 	}
    428   1.1       cgd 	if (res != NULL)
    429   1.1       cgd 		Fclose(res);
    430   1.1       cgd 	Fclose(obuf);
    431   1.1       cgd 	alter(mailname);
    432   1.1       cgd 	if (p == 1)
    433   1.1       cgd 		printf("Held 1 message in %s\n", mailname);
    434   1.1       cgd 	else
    435   1.1       cgd 		printf("Held %d messages in %s\n", p, mailname);
    436   1.1       cgd 	return(0);
    437   1.1       cgd }
    438   1.1       cgd 
    439   1.1       cgd /*
    440   1.1       cgd  * Terminate an editing session by attempting to write out the user's
    441   1.1       cgd  * file from the temporary.  Save any new stuff appended to the file.
    442   1.1       cgd  */
    443   1.3   deraadt void
    444   1.1       cgd edstop()
    445   1.1       cgd {
    446   1.7     lukem 	int gotcha, c;
    447   1.7     lukem 	struct message *mp;
    448   1.5  christos 	FILE *obuf, *ibuf, *readstat = NULL;
    449   1.1       cgd 	struct stat statb;
    450   1.4       jtc 	char *tempname;
    451   1.1       cgd 
    452   1.1       cgd 	if (readonly)
    453   1.1       cgd 		return;
    454   1.1       cgd 	holdsigs();
    455   1.1       cgd 	if (Tflag != NOSTR) {
    456   1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    457   1.1       cgd 			Tflag = NOSTR;
    458   1.1       cgd 	}
    459   1.1       cgd 	for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
    460   1.1       cgd 		if (mp->m_flag & MNEW) {
    461   1.1       cgd 			mp->m_flag &= ~MNEW;
    462   1.1       cgd 			mp->m_flag |= MSTATUS;
    463   1.1       cgd 		}
    464   1.1       cgd 		if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
    465   1.1       cgd 			gotcha++;
    466   1.1       cgd 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    467   1.1       cgd 			char *id;
    468   1.1       cgd 
    469   1.1       cgd 			if ((id = hfield("article-id", mp)) != NOSTR)
    470   1.1       cgd 				fprintf(readstat, "%s\n", id);
    471   1.1       cgd 		}
    472   1.1       cgd 	}
    473   1.1       cgd 	if (Tflag != NOSTR)
    474   1.1       cgd 		Fclose(readstat);
    475   1.1       cgd 	if (!gotcha || Tflag != NOSTR)
    476   1.1       cgd 		goto done;
    477   1.1       cgd 	ibuf = NULL;
    478   1.1       cgd 	if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
    479   1.4       jtc 		tempname = tempnam(tmpdir, "mbox");
    480   1.4       jtc 
    481   1.1       cgd 		if ((obuf = Fopen(tempname, "w")) == NULL) {
    482   1.1       cgd 			perror(tempname);
    483   1.1       cgd 			relsesigs();
    484   1.1       cgd 			reset(0);
    485   1.1       cgd 		}
    486   1.1       cgd 		if ((ibuf = Fopen(mailname, "r")) == NULL) {
    487   1.1       cgd 			perror(mailname);
    488   1.1       cgd 			Fclose(obuf);
    489   1.1       cgd 			rm(tempname);
    490   1.1       cgd 			relsesigs();
    491   1.1       cgd 			reset(0);
    492   1.1       cgd 		}
    493   1.3   deraadt 		fseek(ibuf, (long)mailsize, 0);
    494   1.1       cgd 		while ((c = getc(ibuf)) != EOF)
    495   1.1       cgd 			(void) putc(c, obuf);
    496   1.8       bad 		(void) fflush(obuf);
    497   1.8       bad 		if (ferror(obuf)) {
    498   1.8       bad 			perror(tempname);
    499   1.8       bad 			Fclose(obuf);
    500   1.8       bad 			Fclose(ibuf);
    501   1.8       bad 			rm(tempname);
    502   1.8       bad 			relsesigs();
    503   1.8       bad 			reset(0);
    504   1.8       bad 		}
    505   1.1       cgd 		Fclose(ibuf);
    506   1.1       cgd 		Fclose(obuf);
    507   1.1       cgd 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
    508   1.1       cgd 			perror(tempname);
    509   1.1       cgd 			rm(tempname);
    510   1.1       cgd 			relsesigs();
    511   1.1       cgd 			reset(0);
    512   1.1       cgd 		}
    513   1.1       cgd 		rm(tempname);
    514   1.4       jtc 		free(tempname);
    515   1.1       cgd 	}
    516   1.1       cgd 	printf("\"%s\" ", mailname);
    517   1.1       cgd 	fflush(stdout);
    518   1.1       cgd 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    519   1.1       cgd 		perror(mailname);
    520   1.1       cgd 		relsesigs();
    521   1.1       cgd 		reset(0);
    522   1.1       cgd 	}
    523   1.1       cgd 	trunc(obuf);
    524   1.1       cgd 	c = 0;
    525   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
    526   1.1       cgd 		if ((mp->m_flag & MDELETED) != 0)
    527   1.1       cgd 			continue;
    528   1.1       cgd 		c++;
    529  1.11      tron 		if (sendmessage(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
    530   1.1       cgd 			perror(mailname);
    531   1.1       cgd 			relsesigs();
    532   1.1       cgd 			reset(0);
    533   1.1       cgd 		}
    534   1.1       cgd 	}
    535   1.1       cgd 	gotcha = (c == 0 && ibuf == NULL);
    536   1.1       cgd 	if (ibuf != NULL) {
    537   1.1       cgd 		while ((c = getc(ibuf)) != EOF)
    538   1.1       cgd 			(void) putc(c, obuf);
    539   1.1       cgd 		Fclose(ibuf);
    540   1.1       cgd 	}
    541   1.1       cgd 	fflush(obuf);
    542   1.1       cgd 	if (ferror(obuf)) {
    543   1.1       cgd 		perror(mailname);
    544   1.1       cgd 		relsesigs();
    545   1.1       cgd 		reset(0);
    546   1.1       cgd 	}
    547   1.1       cgd 	Fclose(obuf);
    548   1.1       cgd 	if (gotcha) {
    549   1.1       cgd 		rm(mailname);
    550   1.1       cgd 		printf("removed\n");
    551   1.1       cgd 	} else
    552   1.1       cgd 		printf("complete\n");
    553   1.1       cgd 	fflush(stdout);
    554   1.1       cgd 
    555   1.1       cgd done:
    556   1.1       cgd 	relsesigs();
    557   1.1       cgd }
    558