Home | History | Annotate | Line # | Download | only in mail
quit.c revision 1.15
      1  1.15       wiz /*	$NetBSD: quit.c,v 1.15 2002/03/05 20:14:02 wiz 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.15       wiz __RCSID("$NetBSD: quit.c,v 1.15 2002/03/05 20:14:02 wiz 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.15       wiz extern char *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.13       wiz quitcmd(void *v)
     62   1.1       cgd {
     63   1.1       cgd 	/*
     64   1.1       cgd 	 * If we are sourcing, then return 1 so execute() can handle it.
     65   1.1       cgd 	 * Otherwise, return -1 to abort command loop.
     66   1.1       cgd 	 */
     67   1.1       cgd 	if (sourcing)
     68   1.1       cgd 		return 1;
     69   1.1       cgd 	return -1;
     70   1.1       cgd }
     71   1.1       cgd 
     72   1.1       cgd /*
     73   1.1       cgd  * Save all of the undetermined messages at the top of "mbox"
     74   1.1       cgd  * Save all untouched messages back in the system mailbox.
     75   1.1       cgd  * Remove the system mailbox, if none saved there.
     76   1.1       cgd  */
     77   1.3   deraadt void
     78  1.13       wiz quit(void)
     79   1.1       cgd {
     80   1.1       cgd 	int mcount, p, modify, autohold, anystat, holdbit, nohold;
     81   1.5  christos 	FILE *ibuf = NULL, *obuf, *fbuf, *rbuf, *readstat = NULL, *abuf;
     82   1.7     lukem 	struct message *mp;
     83   1.7     lukem 	int c;
     84  1.15       wiz 	int fd; /* temporary file descriptor for temp file */
     85   1.1       cgd 	struct stat minfo;
     86   1.1       cgd 	char *mbox;
     87  1.15       wiz 	char tempname[PATHSIZE];
     88   1.1       cgd 
     89   1.9       bad #ifdef __GNUC__
     90   1.9       bad 	obuf = NULL;		/* XXX gcc -Wuninitialized */
     91   1.9       bad #endif
     92   1.9       bad 
     93   1.1       cgd 	/*
     94   1.1       cgd 	 * If we are read only, we can't do anything,
     95   1.1       cgd 	 * so just return quickly.
     96   1.1       cgd 	 */
     97   1.1       cgd 	if (readonly)
     98   1.1       cgd 		return;
     99   1.1       cgd 	/*
    100   1.1       cgd 	 * If editing (not reading system mail box), then do the work
    101   1.1       cgd 	 * in edstop()
    102   1.1       cgd 	 */
    103   1.1       cgd 	if (edit) {
    104   1.1       cgd 		edstop();
    105   1.1       cgd 		return;
    106   1.1       cgd 	}
    107   1.1       cgd 
    108   1.1       cgd 	/*
    109   1.1       cgd 	 * See if there any messages to save in mbox.  If no, we
    110   1.1       cgd 	 * can save copying mbox to /tmp and back.
    111   1.1       cgd 	 *
    112   1.1       cgd 	 * Check also to see if any files need to be preserved.
    113   1.1       cgd 	 * Delete all untouched messages to keep them out of mbox.
    114   1.1       cgd 	 * If all the messages are to be preserved, just exit with
    115   1.1       cgd 	 * a message.
    116   1.1       cgd 	 */
    117   1.1       cgd 
    118   1.1       cgd 	fbuf = Fopen(mailname, "r");
    119   1.1       cgd 	if (fbuf == NULL)
    120   1.1       cgd 		goto newmail;
    121   1.5  christos 	if (flock(fileno(fbuf), LOCK_EX) == -1) {
    122   1.5  christos nolock:
    123   1.5  christos 		perror("Unable to lock mailbox");
    124   1.5  christos 		Fclose(fbuf);
    125   1.5  christos 		return;
    126   1.5  christos 	}
    127   1.5  christos 	if (dot_lock(mailname, 1, stdout, ".") == -1)
    128   1.5  christos 		goto nolock;
    129   1.1       cgd 	rbuf = NULL;
    130   1.1       cgd 	if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
    131   1.1       cgd 		printf("New mail has arrived.\n");
    132   1.1       cgd 		rbuf = Fopen(tempResid, "w");
    133   1.1       cgd 		if (rbuf == NULL || fbuf == NULL)
    134   1.1       cgd 			goto newmail;
    135   1.1       cgd #ifdef APPEND
    136   1.3   deraadt 		fseek(fbuf, (long)mailsize, 0);
    137   1.1       cgd 		while ((c = getc(fbuf)) != EOF)
    138   1.1       cgd 			(void) putc(c, rbuf);
    139   1.1       cgd #else
    140   1.1       cgd 		p = minfo.st_size - mailsize;
    141   1.1       cgd 		while (p-- > 0) {
    142   1.1       cgd 			c = getc(fbuf);
    143   1.1       cgd 			if (c == EOF)
    144   1.1       cgd 				goto newmail;
    145   1.1       cgd 			(void) putc(c, rbuf);
    146   1.1       cgd 		}
    147   1.1       cgd #endif
    148   1.8       bad 		(void) fflush(rbuf);
    149   1.8       bad 		if (ferror(rbuf)) {
    150   1.8       bad 			perror(tempResid);
    151   1.8       bad 			Fclose(rbuf);
    152   1.8       bad 			Fclose(fbuf);
    153   1.8       bad 			dot_unlock(mailname);
    154   1.8       bad 			return;
    155   1.8       bad 		}
    156   1.1       cgd 		Fclose(rbuf);
    157   1.1       cgd 		if ((rbuf = Fopen(tempResid, "r")) == NULL)
    158   1.1       cgd 			goto newmail;
    159   1.1       cgd 		rm(tempResid);
    160   1.1       cgd 	}
    161   1.1       cgd 
    162   1.1       cgd 	/*
    163   1.1       cgd 	 * Adjust the message flags in each message.
    164   1.1       cgd 	 */
    165   1.1       cgd 
    166   1.1       cgd 	anystat = 0;
    167  1.14       wiz 	autohold = value("hold") != NULL;
    168   1.1       cgd 	holdbit = autohold ? MPRESERVE : MBOX;
    169   1.1       cgd 	nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
    170  1.14       wiz 	if (value("keepsave") != NULL)
    171   1.1       cgd 		nohold &= ~MSAVED;
    172   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
    173   1.1       cgd 		if (mp->m_flag & MNEW) {
    174   1.1       cgd 			mp->m_flag &= ~MNEW;
    175   1.1       cgd 			mp->m_flag |= MSTATUS;
    176   1.1       cgd 		}
    177   1.1       cgd 		if (mp->m_flag & MSTATUS)
    178   1.1       cgd 			anystat++;
    179   1.1       cgd 		if ((mp->m_flag & MTOUCH) == 0)
    180   1.1       cgd 			mp->m_flag |= MPRESERVE;
    181   1.1       cgd 		if ((mp->m_flag & nohold) == 0)
    182   1.1       cgd 			mp->m_flag |= holdbit;
    183   1.1       cgd 	}
    184   1.1       cgd 	modify = 0;
    185  1.14       wiz 	if (Tflag != NULL) {
    186   1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    187  1.14       wiz 			Tflag = NULL;
    188   1.1       cgd 	}
    189   1.1       cgd 	for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
    190   1.1       cgd 		if (mp->m_flag & MBOX)
    191   1.1       cgd 			c++;
    192   1.1       cgd 		if (mp->m_flag & MPRESERVE)
    193   1.1       cgd 			p++;
    194   1.1       cgd 		if (mp->m_flag & MODIFY)
    195   1.1       cgd 			modify++;
    196  1.14       wiz 		if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    197   1.1       cgd 			char *id;
    198   1.1       cgd 
    199  1.14       wiz 			if ((id = hfield("article-id", mp)) != NULL)
    200   1.1       cgd 				fprintf(readstat, "%s\n", id);
    201   1.1       cgd 		}
    202   1.1       cgd 	}
    203  1.14       wiz 	if (Tflag != NULL)
    204   1.1       cgd 		Fclose(readstat);
    205   1.1       cgd 	if (p == msgCount && !modify && !anystat) {
    206   1.1       cgd 		printf("Held %d message%s in %s\n",
    207   1.1       cgd 			p, p == 1 ? "" : "s", mailname);
    208   1.1       cgd 		Fclose(fbuf);
    209   1.5  christos 		dot_unlock(mailname);
    210   1.1       cgd 		return;
    211   1.1       cgd 	}
    212   1.1       cgd 	if (c == 0) {
    213   1.1       cgd 		if (p != 0) {
    214   1.1       cgd 			writeback(rbuf);
    215   1.1       cgd 			Fclose(fbuf);
    216   1.5  christos 			dot_unlock(mailname);
    217   1.1       cgd 			return;
    218   1.1       cgd 		}
    219   1.1       cgd 		goto cream;
    220   1.1       cgd 	}
    221   1.1       cgd 
    222   1.1       cgd 	/*
    223   1.1       cgd 	 * Create another temporary file and copy user's mbox file
    224   1.1       cgd 	 * darin.  If there is no mbox, copy nothing.
    225   1.1       cgd 	 * If he has specified "append" don't copy his mailbox,
    226   1.1       cgd 	 * just copy saveable entries at the end.
    227   1.1       cgd 	 */
    228   1.1       cgd 
    229   1.1       cgd 	mbox = expand("&");
    230   1.1       cgd 	mcount = c;
    231  1.14       wiz 	if (value("append") == NULL) {
    232  1.15       wiz 		(void)snprintf(tempname, sizeof(tempname),
    233  1.15       wiz 		    "%s/mail.RmXXXXXXXXXX", tmpdir);
    234  1.15       wiz 		if ((fd = mkstemp(tempname)) == -1 ||
    235  1.15       wiz 		    (obuf = Fdopen(fd, "w")) == NULL) {
    236  1.15       wiz 			warn("%s", tempname);
    237   1.1       cgd 			Fclose(fbuf);
    238   1.5  christos 			dot_unlock(mailname);
    239   1.1       cgd 			return;
    240   1.1       cgd 		}
    241  1.15       wiz 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
    242  1.15       wiz 			warn("%s", tempname);
    243  1.15       wiz 			rm(tempname);
    244   1.1       cgd 			Fclose(obuf);
    245   1.1       cgd 			Fclose(fbuf);
    246   1.5  christos 			dot_unlock(mailname);
    247   1.1       cgd 			return;
    248   1.1       cgd 		}
    249  1.15       wiz 		rm(tempname);
    250   1.1       cgd 		if ((abuf = Fopen(mbox, "r")) != NULL) {
    251   1.1       cgd 			while ((c = getc(abuf)) != EOF)
    252   1.1       cgd 				(void) putc(c, obuf);
    253   1.1       cgd 			Fclose(abuf);
    254   1.1       cgd 		}
    255   1.1       cgd 		if (ferror(obuf)) {
    256  1.15       wiz 			warn("%s", tempname);
    257   1.1       cgd 			Fclose(ibuf);
    258   1.1       cgd 			Fclose(obuf);
    259   1.1       cgd 			Fclose(fbuf);
    260   1.5  christos 			dot_unlock(mailname);
    261   1.1       cgd 			return;
    262   1.1       cgd 		}
    263   1.1       cgd 		Fclose(obuf);
    264   1.1       cgd 		close(creat(mbox, 0600));
    265   1.1       cgd 		if ((obuf = Fopen(mbox, "r+")) == NULL) {
    266   1.1       cgd 			perror(mbox);
    267   1.1       cgd 			Fclose(ibuf);
    268   1.1       cgd 			Fclose(fbuf);
    269   1.5  christos 			dot_unlock(mailname);
    270   1.1       cgd 			return;
    271   1.1       cgd 		}
    272   1.1       cgd 	}
    273   1.5  christos 	else {
    274   1.1       cgd 		if ((obuf = Fopen(mbox, "a")) == NULL) {
    275   1.1       cgd 			perror(mbox);
    276   1.1       cgd 			Fclose(fbuf);
    277   1.5  christos 			dot_unlock(mailname);
    278   1.1       cgd 			return;
    279   1.1       cgd 		}
    280   1.1       cgd 		fchmod(fileno(obuf), 0600);
    281   1.1       cgd 	}
    282   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    283   1.1       cgd 		if (mp->m_flag & MBOX)
    284  1.14       wiz 			if (sendmessage(mp, obuf, saveignore, NULL) < 0) {
    285   1.1       cgd 				perror(mbox);
    286   1.1       cgd 				Fclose(ibuf);
    287   1.1       cgd 				Fclose(obuf);
    288   1.1       cgd 				Fclose(fbuf);
    289   1.5  christos 				dot_unlock(mailname);
    290   1.1       cgd 				return;
    291   1.1       cgd 			}
    292   1.1       cgd 
    293   1.1       cgd 	/*
    294   1.1       cgd 	 * Copy the user's old mbox contents back
    295   1.1       cgd 	 * to the end of the stuff we just saved.
    296   1.1       cgd 	 * If we are appending, this is unnecessary.
    297   1.1       cgd 	 */
    298   1.1       cgd 
    299  1.14       wiz 	if (value("append") == NULL) {
    300   1.1       cgd 		rewind(ibuf);
    301   1.1       cgd 		c = getc(ibuf);
    302   1.1       cgd 		while (c != EOF) {
    303   1.1       cgd 			(void) putc(c, obuf);
    304   1.1       cgd 			if (ferror(obuf))
    305   1.1       cgd 				break;
    306   1.1       cgd 			c = getc(ibuf);
    307   1.1       cgd 		}
    308   1.1       cgd 		Fclose(ibuf);
    309   1.1       cgd 	}
    310   1.6       tls 	fflush(obuf);
    311   1.8       bad 	if (!ferror(obuf))
    312   1.8       bad 		trunc(obuf);	/* XXX or should we truncate? */
    313   1.1       cgd 	if (ferror(obuf)) {
    314   1.1       cgd 		perror(mbox);
    315   1.1       cgd 		Fclose(obuf);
    316   1.1       cgd 		Fclose(fbuf);
    317   1.5  christos 		dot_unlock(mailname);
    318   1.1       cgd 		return;
    319   1.1       cgd 	}
    320   1.1       cgd 	Fclose(obuf);
    321   1.1       cgd 	if (mcount == 1)
    322   1.1       cgd 		printf("Saved 1 message in mbox\n");
    323   1.1       cgd 	else
    324   1.1       cgd 		printf("Saved %d messages in mbox\n", mcount);
    325   1.1       cgd 
    326   1.1       cgd 	/*
    327   1.1       cgd 	 * Now we are ready to copy back preserved files to
    328   1.1       cgd 	 * the system mailbox, if any were requested.
    329   1.1       cgd 	 */
    330   1.1       cgd 
    331   1.1       cgd 	if (p != 0) {
    332   1.1       cgd 		writeback(rbuf);
    333   1.1       cgd 		Fclose(fbuf);
    334   1.5  christos 		dot_unlock(mailname);
    335   1.1       cgd 		return;
    336   1.1       cgd 	}
    337   1.1       cgd 
    338   1.1       cgd 	/*
    339   1.1       cgd 	 * Finally, remove his /usr/mail file.
    340   1.1       cgd 	 * If new mail has arrived, copy it back.
    341   1.1       cgd 	 */
    342   1.1       cgd 
    343   1.1       cgd cream:
    344   1.1       cgd 	if (rbuf != NULL) {
    345   1.1       cgd 		abuf = Fopen(mailname, "r+");
    346   1.1       cgd 		if (abuf == NULL)
    347   1.1       cgd 			goto newmail;
    348   1.1       cgd 		while ((c = getc(rbuf)) != EOF)
    349   1.1       cgd 			(void) putc(c, abuf);
    350   1.8       bad 		(void) fflush(abuf);
    351  1.10       bad 		if (ferror(abuf)) {
    352   1.8       bad 			perror(mailname);
    353   1.8       bad 			Fclose(abuf);
    354   1.8       bad 			Fclose(fbuf);
    355   1.8       bad 			dot_unlock(mailname);
    356   1.8       bad 			return;
    357   1.8       bad 		}
    358   1.1       cgd 		Fclose(rbuf);
    359   1.1       cgd 		trunc(abuf);
    360   1.1       cgd 		Fclose(abuf);
    361   1.1       cgd 		alter(mailname);
    362   1.1       cgd 		Fclose(fbuf);
    363   1.5  christos 		dot_unlock(mailname);
    364   1.1       cgd 		return;
    365   1.1       cgd 	}
    366   1.1       cgd 	demail();
    367   1.1       cgd 	Fclose(fbuf);
    368   1.5  christos 	dot_unlock(mailname);
    369   1.1       cgd 	return;
    370   1.1       cgd 
    371   1.1       cgd newmail:
    372   1.1       cgd 	printf("Thou hast new mail.\n");
    373   1.5  christos 	if (fbuf != NULL) {
    374   1.1       cgd 		Fclose(fbuf);
    375   1.5  christos 		dot_unlock(mailname);
    376   1.5  christos 	}
    377   1.1       cgd }
    378   1.1       cgd 
    379   1.1       cgd /*
    380   1.1       cgd  * Preserve all the appropriate messages back in the system
    381   1.1       cgd  * mailbox, and print a nice message indicated how many were
    382   1.1       cgd  * saved.  On any error, just return -1.  Else return 0.
    383   1.1       cgd  * Incorporate the any new mail that we found.
    384   1.1       cgd  */
    385   1.3   deraadt int
    386  1.13       wiz writeback(FILE *res)
    387   1.1       cgd {
    388   1.7     lukem 	struct message *mp;
    389   1.7     lukem 	int p, c;
    390   1.1       cgd 	FILE *obuf;
    391   1.1       cgd 
    392   1.1       cgd 	p = 0;
    393   1.1       cgd 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    394   1.1       cgd 		perror(mailname);
    395   1.1       cgd 		return(-1);
    396   1.1       cgd 	}
    397   1.1       cgd #ifndef APPEND
    398   1.8       bad 	if (res != NULL) {
    399   1.1       cgd 		while ((c = getc(res)) != EOF)
    400   1.1       cgd 			(void) putc(c, obuf);
    401   1.8       bad 		(void) fflush(obuf);
    402   1.8       bad 		if (ferror(obuf)) {
    403   1.8       bad 			perror(mailname);
    404   1.8       bad 			Fclose(obuf);
    405   1.8       bad 			return(-1);
    406   1.8       bad 		}
    407   1.8       bad 	}
    408   1.1       cgd #endif
    409   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    410   1.1       cgd 		if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
    411   1.1       cgd 			p++;
    412  1.14       wiz 			if (sendmessage(mp, obuf, (struct ignoretab *)0, NULL) < 0) {
    413   1.1       cgd 				perror(mailname);
    414   1.1       cgd 				Fclose(obuf);
    415   1.1       cgd 				return(-1);
    416   1.1       cgd 			}
    417   1.1       cgd 		}
    418   1.1       cgd #ifdef APPEND
    419   1.1       cgd 	if (res != NULL)
    420   1.1       cgd 		while ((c = getc(res)) != EOF)
    421   1.1       cgd 			(void) putc(c, obuf);
    422   1.1       cgd #endif
    423   1.1       cgd 	fflush(obuf);
    424   1.8       bad 	if (!ferror(obuf))
    425  1.10       bad 		trunc(obuf);	/* XXX or should we truncate? */
    426   1.1       cgd 	if (ferror(obuf)) {
    427   1.1       cgd 		perror(mailname);
    428   1.1       cgd 		Fclose(obuf);
    429   1.1       cgd 		return(-1);
    430   1.1       cgd 	}
    431   1.1       cgd 	if (res != NULL)
    432   1.1       cgd 		Fclose(res);
    433   1.1       cgd 	Fclose(obuf);
    434   1.1       cgd 	alter(mailname);
    435   1.1       cgd 	if (p == 1)
    436   1.1       cgd 		printf("Held 1 message in %s\n", mailname);
    437   1.1       cgd 	else
    438   1.1       cgd 		printf("Held %d messages in %s\n", p, mailname);
    439   1.1       cgd 	return(0);
    440   1.1       cgd }
    441   1.1       cgd 
    442   1.1       cgd /*
    443   1.1       cgd  * Terminate an editing session by attempting to write out the user's
    444   1.1       cgd  * file from the temporary.  Save any new stuff appended to the file.
    445   1.1       cgd  */
    446   1.3   deraadt void
    447  1.13       wiz edstop(void)
    448   1.1       cgd {
    449   1.7     lukem 	int gotcha, c;
    450   1.7     lukem 	struct message *mp;
    451   1.5  christos 	FILE *obuf, *ibuf, *readstat = NULL;
    452   1.1       cgd 	struct stat statb;
    453   1.4       jtc 	char *tempname;
    454   1.1       cgd 
    455   1.1       cgd 	if (readonly)
    456   1.1       cgd 		return;
    457   1.1       cgd 	holdsigs();
    458  1.14       wiz 	if (Tflag != NULL) {
    459   1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    460  1.14       wiz 			Tflag = NULL;
    461   1.1       cgd 	}
    462   1.1       cgd 	for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
    463   1.1       cgd 		if (mp->m_flag & MNEW) {
    464   1.1       cgd 			mp->m_flag &= ~MNEW;
    465   1.1       cgd 			mp->m_flag |= MSTATUS;
    466   1.1       cgd 		}
    467   1.1       cgd 		if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
    468   1.1       cgd 			gotcha++;
    469  1.14       wiz 		if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    470   1.1       cgd 			char *id;
    471   1.1       cgd 
    472  1.14       wiz 			if ((id = hfield("article-id", mp)) != NULL)
    473   1.1       cgd 				fprintf(readstat, "%s\n", id);
    474   1.1       cgd 		}
    475   1.1       cgd 	}
    476  1.14       wiz 	if (Tflag != NULL)
    477   1.1       cgd 		Fclose(readstat);
    478  1.14       wiz 	if (!gotcha || Tflag != NULL)
    479   1.1       cgd 		goto done;
    480   1.1       cgd 	ibuf = NULL;
    481   1.1       cgd 	if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
    482   1.4       jtc 		tempname = tempnam(tmpdir, "mbox");
    483   1.4       jtc 
    484   1.1       cgd 		if ((obuf = Fopen(tempname, "w")) == NULL) {
    485   1.1       cgd 			perror(tempname);
    486   1.1       cgd 			relsesigs();
    487   1.1       cgd 			reset(0);
    488   1.1       cgd 		}
    489   1.1       cgd 		if ((ibuf = Fopen(mailname, "r")) == NULL) {
    490   1.1       cgd 			perror(mailname);
    491   1.1       cgd 			Fclose(obuf);
    492   1.1       cgd 			rm(tempname);
    493   1.1       cgd 			relsesigs();
    494   1.1       cgd 			reset(0);
    495   1.1       cgd 		}
    496   1.3   deraadt 		fseek(ibuf, (long)mailsize, 0);
    497   1.1       cgd 		while ((c = getc(ibuf)) != EOF)
    498   1.1       cgd 			(void) putc(c, obuf);
    499   1.8       bad 		(void) fflush(obuf);
    500   1.8       bad 		if (ferror(obuf)) {
    501   1.8       bad 			perror(tempname);
    502   1.8       bad 			Fclose(obuf);
    503   1.8       bad 			Fclose(ibuf);
    504   1.8       bad 			rm(tempname);
    505   1.8       bad 			relsesigs();
    506   1.8       bad 			reset(0);
    507   1.8       bad 		}
    508   1.1       cgd 		Fclose(ibuf);
    509   1.1       cgd 		Fclose(obuf);
    510   1.1       cgd 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
    511   1.1       cgd 			perror(tempname);
    512   1.1       cgd 			rm(tempname);
    513   1.1       cgd 			relsesigs();
    514   1.1       cgd 			reset(0);
    515   1.1       cgd 		}
    516   1.1       cgd 		rm(tempname);
    517   1.4       jtc 		free(tempname);
    518   1.1       cgd 	}
    519   1.1       cgd 	printf("\"%s\" ", mailname);
    520   1.1       cgd 	fflush(stdout);
    521   1.1       cgd 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    522   1.1       cgd 		perror(mailname);
    523   1.1       cgd 		relsesigs();
    524   1.1       cgd 		reset(0);
    525   1.1       cgd 	}
    526   1.1       cgd 	trunc(obuf);
    527   1.1       cgd 	c = 0;
    528   1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
    529   1.1       cgd 		if ((mp->m_flag & MDELETED) != 0)
    530   1.1       cgd 			continue;
    531   1.1       cgd 		c++;
    532  1.14       wiz 		if (sendmessage(mp, obuf, (struct ignoretab *) NULL, NULL) < 0) {
    533   1.1       cgd 			perror(mailname);
    534   1.1       cgd 			relsesigs();
    535   1.1       cgd 			reset(0);
    536   1.1       cgd 		}
    537   1.1       cgd 	}
    538   1.1       cgd 	gotcha = (c == 0 && ibuf == NULL);
    539   1.1       cgd 	if (ibuf != NULL) {
    540   1.1       cgd 		while ((c = getc(ibuf)) != EOF)
    541   1.1       cgd 			(void) putc(c, obuf);
    542   1.1       cgd 		Fclose(ibuf);
    543   1.1       cgd 	}
    544   1.1       cgd 	fflush(obuf);
    545   1.1       cgd 	if (ferror(obuf)) {
    546   1.1       cgd 		perror(mailname);
    547   1.1       cgd 		relsesigs();
    548   1.1       cgd 		reset(0);
    549   1.1       cgd 	}
    550   1.1       cgd 	Fclose(obuf);
    551   1.1       cgd 	if (gotcha) {
    552   1.1       cgd 		rm(mailname);
    553   1.1       cgd 		printf("removed\n");
    554   1.1       cgd 	} else
    555   1.1       cgd 		printf("complete\n");
    556   1.1       cgd 	fflush(stdout);
    557   1.1       cgd 
    558   1.1       cgd done:
    559   1.1       cgd 	relsesigs();
    560   1.1       cgd }
    561