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