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