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