Home | History | Annotate | Line # | Download | only in mail
quit.c revision 1.5
      1  1.5  christos /*	$NetBSD: quit.c,v 1.5 1996/06/08 19:48:37 christos Exp $	*/
      2  1.5  christos 
      3  1.1       cgd /*
      4  1.3   deraadt  * Copyright (c) 1980, 1993
      5  1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.1       cgd #ifndef lint
     37  1.5  christos #if 0
     38  1.5  christos static char sccsid[] = "@(#)quit.c	8.1 (Berkeley) 6/6/93";
     39  1.5  christos #else
     40  1.5  christos static char rcsid[] = "$NetBSD: quit.c,v 1.5 1996/06/08 19:48:37 christos Exp $";
     41  1.5  christos #endif
     42  1.1       cgd #endif /* not lint */
     43  1.1       cgd 
     44  1.1       cgd #include "rcv.h"
     45  1.3   deraadt #include <fcntl.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.1       cgd 	register struct message *mp;
     81  1.1       cgd 	register 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.1       cgd 		Fclose(rbuf);
    142  1.1       cgd 		if ((rbuf = Fopen(tempResid, "r")) == NULL)
    143  1.1       cgd 			goto newmail;
    144  1.1       cgd 		rm(tempResid);
    145  1.1       cgd 	}
    146  1.1       cgd 
    147  1.1       cgd 	/*
    148  1.1       cgd 	 * Adjust the message flags in each message.
    149  1.1       cgd 	 */
    150  1.1       cgd 
    151  1.1       cgd 	anystat = 0;
    152  1.1       cgd 	autohold = value("hold") != NOSTR;
    153  1.1       cgd 	holdbit = autohold ? MPRESERVE : MBOX;
    154  1.1       cgd 	nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
    155  1.1       cgd 	if (value("keepsave") != NOSTR)
    156  1.1       cgd 		nohold &= ~MSAVED;
    157  1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
    158  1.1       cgd 		if (mp->m_flag & MNEW) {
    159  1.1       cgd 			mp->m_flag &= ~MNEW;
    160  1.1       cgd 			mp->m_flag |= MSTATUS;
    161  1.1       cgd 		}
    162  1.1       cgd 		if (mp->m_flag & MSTATUS)
    163  1.1       cgd 			anystat++;
    164  1.1       cgd 		if ((mp->m_flag & MTOUCH) == 0)
    165  1.1       cgd 			mp->m_flag |= MPRESERVE;
    166  1.1       cgd 		if ((mp->m_flag & nohold) == 0)
    167  1.1       cgd 			mp->m_flag |= holdbit;
    168  1.1       cgd 	}
    169  1.1       cgd 	modify = 0;
    170  1.1       cgd 	if (Tflag != NOSTR) {
    171  1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    172  1.1       cgd 			Tflag = NOSTR;
    173  1.1       cgd 	}
    174  1.1       cgd 	for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
    175  1.1       cgd 		if (mp->m_flag & MBOX)
    176  1.1       cgd 			c++;
    177  1.1       cgd 		if (mp->m_flag & MPRESERVE)
    178  1.1       cgd 			p++;
    179  1.1       cgd 		if (mp->m_flag & MODIFY)
    180  1.1       cgd 			modify++;
    181  1.1       cgd 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    182  1.1       cgd 			char *id;
    183  1.1       cgd 
    184  1.1       cgd 			if ((id = hfield("article-id", mp)) != NOSTR)
    185  1.1       cgd 				fprintf(readstat, "%s\n", id);
    186  1.1       cgd 		}
    187  1.1       cgd 	}
    188  1.1       cgd 	if (Tflag != NOSTR)
    189  1.1       cgd 		Fclose(readstat);
    190  1.1       cgd 	if (p == msgCount && !modify && !anystat) {
    191  1.1       cgd 		printf("Held %d message%s in %s\n",
    192  1.1       cgd 			p, p == 1 ? "" : "s", mailname);
    193  1.1       cgd 		Fclose(fbuf);
    194  1.5  christos 		dot_unlock(mailname);
    195  1.1       cgd 		return;
    196  1.1       cgd 	}
    197  1.1       cgd 	if (c == 0) {
    198  1.1       cgd 		if (p != 0) {
    199  1.1       cgd 			writeback(rbuf);
    200  1.1       cgd 			Fclose(fbuf);
    201  1.5  christos 			dot_unlock(mailname);
    202  1.1       cgd 			return;
    203  1.1       cgd 		}
    204  1.1       cgd 		goto cream;
    205  1.1       cgd 	}
    206  1.1       cgd 
    207  1.1       cgd 	/*
    208  1.1       cgd 	 * Create another temporary file and copy user's mbox file
    209  1.1       cgd 	 * darin.  If there is no mbox, copy nothing.
    210  1.1       cgd 	 * If he has specified "append" don't copy his mailbox,
    211  1.1       cgd 	 * just copy saveable entries at the end.
    212  1.1       cgd 	 */
    213  1.1       cgd 
    214  1.1       cgd 	mbox = expand("&");
    215  1.1       cgd 	mcount = c;
    216  1.1       cgd 	if (value("append") == NOSTR) {
    217  1.1       cgd 		if ((obuf = Fopen(tempQuit, "w")) == NULL) {
    218  1.1       cgd 			perror(tempQuit);
    219  1.1       cgd 			Fclose(fbuf);
    220  1.5  christos 			dot_unlock(mailname);
    221  1.1       cgd 			return;
    222  1.1       cgd 		}
    223  1.1       cgd 		if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
    224  1.1       cgd 			perror(tempQuit);
    225  1.1       cgd 			rm(tempQuit);
    226  1.1       cgd 			Fclose(obuf);
    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 		rm(tempQuit);
    232  1.1       cgd 		if ((abuf = Fopen(mbox, "r")) != NULL) {
    233  1.1       cgd 			while ((c = getc(abuf)) != EOF)
    234  1.1       cgd 				(void) putc(c, obuf);
    235  1.1       cgd 			Fclose(abuf);
    236  1.1       cgd 		}
    237  1.1       cgd 		if (ferror(obuf)) {
    238  1.1       cgd 			perror(tempQuit);
    239  1.1       cgd 			Fclose(ibuf);
    240  1.1       cgd 			Fclose(obuf);
    241  1.1       cgd 			Fclose(fbuf);
    242  1.5  christos 			dot_unlock(mailname);
    243  1.1       cgd 			return;
    244  1.1       cgd 		}
    245  1.1       cgd 		Fclose(obuf);
    246  1.1       cgd 		close(creat(mbox, 0600));
    247  1.1       cgd 		if ((obuf = Fopen(mbox, "r+")) == NULL) {
    248  1.1       cgd 			perror(mbox);
    249  1.1       cgd 			Fclose(ibuf);
    250  1.1       cgd 			Fclose(fbuf);
    251  1.5  christos 			dot_unlock(mailname);
    252  1.1       cgd 			return;
    253  1.1       cgd 		}
    254  1.1       cgd 	}
    255  1.5  christos 	else {
    256  1.1       cgd 		if ((obuf = Fopen(mbox, "a")) == NULL) {
    257  1.1       cgd 			perror(mbox);
    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 		fchmod(fileno(obuf), 0600);
    263  1.1       cgd 	}
    264  1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    265  1.1       cgd 		if (mp->m_flag & MBOX)
    266  1.1       cgd 			if (send(mp, obuf, saveignore, NOSTR) < 0) {
    267  1.1       cgd 				perror(mbox);
    268  1.1       cgd 				Fclose(ibuf);
    269  1.1       cgd 				Fclose(obuf);
    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 
    275  1.1       cgd 	/*
    276  1.1       cgd 	 * Copy the user's old mbox contents back
    277  1.1       cgd 	 * to the end of the stuff we just saved.
    278  1.1       cgd 	 * If we are appending, this is unnecessary.
    279  1.1       cgd 	 */
    280  1.1       cgd 
    281  1.1       cgd 	if (value("append") == NOSTR) {
    282  1.1       cgd 		rewind(ibuf);
    283  1.1       cgd 		c = getc(ibuf);
    284  1.1       cgd 		while (c != EOF) {
    285  1.1       cgd 			(void) putc(c, obuf);
    286  1.1       cgd 			if (ferror(obuf))
    287  1.1       cgd 				break;
    288  1.1       cgd 			c = getc(ibuf);
    289  1.1       cgd 		}
    290  1.1       cgd 		Fclose(ibuf);
    291  1.1       cgd 		fflush(obuf);
    292  1.1       cgd 	}
    293  1.1       cgd 	trunc(obuf);
    294  1.1       cgd 	if (ferror(obuf)) {
    295  1.1       cgd 		perror(mbox);
    296  1.1       cgd 		Fclose(obuf);
    297  1.1       cgd 		Fclose(fbuf);
    298  1.5  christos 		dot_unlock(mailname);
    299  1.1       cgd 		return;
    300  1.1       cgd 	}
    301  1.1       cgd 	Fclose(obuf);
    302  1.1       cgd 	if (mcount == 1)
    303  1.1       cgd 		printf("Saved 1 message in mbox\n");
    304  1.1       cgd 	else
    305  1.1       cgd 		printf("Saved %d messages in mbox\n", mcount);
    306  1.1       cgd 
    307  1.1       cgd 	/*
    308  1.1       cgd 	 * Now we are ready to copy back preserved files to
    309  1.1       cgd 	 * the system mailbox, if any were requested.
    310  1.1       cgd 	 */
    311  1.1       cgd 
    312  1.1       cgd 	if (p != 0) {
    313  1.1       cgd 		writeback(rbuf);
    314  1.1       cgd 		Fclose(fbuf);
    315  1.5  christos 		dot_unlock(mailname);
    316  1.1       cgd 		return;
    317  1.1       cgd 	}
    318  1.1       cgd 
    319  1.1       cgd 	/*
    320  1.1       cgd 	 * Finally, remove his /usr/mail file.
    321  1.1       cgd 	 * If new mail has arrived, copy it back.
    322  1.1       cgd 	 */
    323  1.1       cgd 
    324  1.1       cgd cream:
    325  1.1       cgd 	if (rbuf != NULL) {
    326  1.1       cgd 		abuf = Fopen(mailname, "r+");
    327  1.1       cgd 		if (abuf == NULL)
    328  1.1       cgd 			goto newmail;
    329  1.1       cgd 		while ((c = getc(rbuf)) != EOF)
    330  1.1       cgd 			(void) putc(c, abuf);
    331  1.1       cgd 		Fclose(rbuf);
    332  1.1       cgd 		trunc(abuf);
    333  1.1       cgd 		Fclose(abuf);
    334  1.1       cgd 		alter(mailname);
    335  1.1       cgd 		Fclose(fbuf);
    336  1.5  christos 		dot_unlock(mailname);
    337  1.1       cgd 		return;
    338  1.1       cgd 	}
    339  1.1       cgd 	demail();
    340  1.1       cgd 	Fclose(fbuf);
    341  1.5  christos 	dot_unlock(mailname);
    342  1.1       cgd 	return;
    343  1.1       cgd 
    344  1.1       cgd newmail:
    345  1.1       cgd 	printf("Thou hast new mail.\n");
    346  1.5  christos 	if (fbuf != NULL) {
    347  1.1       cgd 		Fclose(fbuf);
    348  1.5  christos 		dot_unlock(mailname);
    349  1.5  christos 	}
    350  1.1       cgd }
    351  1.1       cgd 
    352  1.1       cgd /*
    353  1.1       cgd  * Preserve all the appropriate messages back in the system
    354  1.1       cgd  * mailbox, and print a nice message indicated how many were
    355  1.1       cgd  * saved.  On any error, just return -1.  Else return 0.
    356  1.1       cgd  * Incorporate the any new mail that we found.
    357  1.1       cgd  */
    358  1.3   deraadt int
    359  1.1       cgd writeback(res)
    360  1.1       cgd 	register FILE *res;
    361  1.1       cgd {
    362  1.1       cgd 	register struct message *mp;
    363  1.1       cgd 	register int p, c;
    364  1.1       cgd 	FILE *obuf;
    365  1.1       cgd 
    366  1.1       cgd 	p = 0;
    367  1.1       cgd 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    368  1.1       cgd 		perror(mailname);
    369  1.1       cgd 		return(-1);
    370  1.1       cgd 	}
    371  1.1       cgd #ifndef APPEND
    372  1.1       cgd 	if (res != NULL)
    373  1.1       cgd 		while ((c = getc(res)) != EOF)
    374  1.1       cgd 			(void) putc(c, obuf);
    375  1.1       cgd #endif
    376  1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    377  1.1       cgd 		if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
    378  1.1       cgd 			p++;
    379  1.1       cgd 			if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
    380  1.1       cgd 				perror(mailname);
    381  1.1       cgd 				Fclose(obuf);
    382  1.1       cgd 				return(-1);
    383  1.1       cgd 			}
    384  1.1       cgd 		}
    385  1.1       cgd #ifdef APPEND
    386  1.1       cgd 	if (res != NULL)
    387  1.1       cgd 		while ((c = getc(res)) != EOF)
    388  1.1       cgd 			(void) putc(c, obuf);
    389  1.1       cgd #endif
    390  1.1       cgd 	fflush(obuf);
    391  1.1       cgd 	trunc(obuf);
    392  1.1       cgd 	if (ferror(obuf)) {
    393  1.1       cgd 		perror(mailname);
    394  1.1       cgd 		Fclose(obuf);
    395  1.1       cgd 		return(-1);
    396  1.1       cgd 	}
    397  1.1       cgd 	if (res != NULL)
    398  1.1       cgd 		Fclose(res);
    399  1.1       cgd 	Fclose(obuf);
    400  1.1       cgd 	alter(mailname);
    401  1.1       cgd 	if (p == 1)
    402  1.1       cgd 		printf("Held 1 message in %s\n", mailname);
    403  1.1       cgd 	else
    404  1.1       cgd 		printf("Held %d messages in %s\n", p, mailname);
    405  1.1       cgd 	return(0);
    406  1.1       cgd }
    407  1.1       cgd 
    408  1.1       cgd /*
    409  1.1       cgd  * Terminate an editing session by attempting to write out the user's
    410  1.1       cgd  * file from the temporary.  Save any new stuff appended to the file.
    411  1.1       cgd  */
    412  1.3   deraadt void
    413  1.1       cgd edstop()
    414  1.1       cgd {
    415  1.3   deraadt 	extern char *tmpdir;
    416  1.1       cgd 	register int gotcha, c;
    417  1.1       cgd 	register struct message *mp;
    418  1.5  christos 	FILE *obuf, *ibuf, *readstat = NULL;
    419  1.1       cgd 	struct stat statb;
    420  1.4       jtc 	char *tempname;
    421  1.1       cgd 
    422  1.1       cgd 	if (readonly)
    423  1.1       cgd 		return;
    424  1.1       cgd 	holdsigs();
    425  1.1       cgd 	if (Tflag != NOSTR) {
    426  1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    427  1.1       cgd 			Tflag = NOSTR;
    428  1.1       cgd 	}
    429  1.1       cgd 	for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
    430  1.1       cgd 		if (mp->m_flag & MNEW) {
    431  1.1       cgd 			mp->m_flag &= ~MNEW;
    432  1.1       cgd 			mp->m_flag |= MSTATUS;
    433  1.1       cgd 		}
    434  1.1       cgd 		if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
    435  1.1       cgd 			gotcha++;
    436  1.1       cgd 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    437  1.1       cgd 			char *id;
    438  1.1       cgd 
    439  1.1       cgd 			if ((id = hfield("article-id", mp)) != NOSTR)
    440  1.1       cgd 				fprintf(readstat, "%s\n", id);
    441  1.1       cgd 		}
    442  1.1       cgd 	}
    443  1.1       cgd 	if (Tflag != NOSTR)
    444  1.1       cgd 		Fclose(readstat);
    445  1.1       cgd 	if (!gotcha || Tflag != NOSTR)
    446  1.1       cgd 		goto done;
    447  1.1       cgd 	ibuf = NULL;
    448  1.1       cgd 	if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
    449  1.4       jtc 		tempname = tempnam(tmpdir, "mbox");
    450  1.4       jtc 
    451  1.1       cgd 		if ((obuf = Fopen(tempname, "w")) == NULL) {
    452  1.1       cgd 			perror(tempname);
    453  1.1       cgd 			relsesigs();
    454  1.1       cgd 			reset(0);
    455  1.1       cgd 		}
    456  1.1       cgd 		if ((ibuf = Fopen(mailname, "r")) == NULL) {
    457  1.1       cgd 			perror(mailname);
    458  1.1       cgd 			Fclose(obuf);
    459  1.1       cgd 			rm(tempname);
    460  1.1       cgd 			relsesigs();
    461  1.1       cgd 			reset(0);
    462  1.1       cgd 		}
    463  1.3   deraadt 		fseek(ibuf, (long)mailsize, 0);
    464  1.1       cgd 		while ((c = getc(ibuf)) != EOF)
    465  1.1       cgd 			(void) putc(c, obuf);
    466  1.1       cgd 		Fclose(ibuf);
    467  1.1       cgd 		Fclose(obuf);
    468  1.1       cgd 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
    469  1.1       cgd 			perror(tempname);
    470  1.1       cgd 			rm(tempname);
    471  1.1       cgd 			relsesigs();
    472  1.1       cgd 			reset(0);
    473  1.1       cgd 		}
    474  1.1       cgd 		rm(tempname);
    475  1.4       jtc 		free(tempname);
    476  1.1       cgd 	}
    477  1.1       cgd 	printf("\"%s\" ", mailname);
    478  1.1       cgd 	fflush(stdout);
    479  1.1       cgd 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    480  1.1       cgd 		perror(mailname);
    481  1.1       cgd 		relsesigs();
    482  1.1       cgd 		reset(0);
    483  1.1       cgd 	}
    484  1.1       cgd 	trunc(obuf);
    485  1.1       cgd 	c = 0;
    486  1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
    487  1.1       cgd 		if ((mp->m_flag & MDELETED) != 0)
    488  1.1       cgd 			continue;
    489  1.1       cgd 		c++;
    490  1.1       cgd 		if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
    491  1.1       cgd 			perror(mailname);
    492  1.1       cgd 			relsesigs();
    493  1.1       cgd 			reset(0);
    494  1.1       cgd 		}
    495  1.1       cgd 	}
    496  1.1       cgd 	gotcha = (c == 0 && ibuf == NULL);
    497  1.1       cgd 	if (ibuf != NULL) {
    498  1.1       cgd 		while ((c = getc(ibuf)) != EOF)
    499  1.1       cgd 			(void) putc(c, obuf);
    500  1.1       cgd 		Fclose(ibuf);
    501  1.1       cgd 	}
    502  1.1       cgd 	fflush(obuf);
    503  1.1       cgd 	if (ferror(obuf)) {
    504  1.1       cgd 		perror(mailname);
    505  1.1       cgd 		relsesigs();
    506  1.1       cgd 		reset(0);
    507  1.1       cgd 	}
    508  1.1       cgd 	Fclose(obuf);
    509  1.1       cgd 	if (gotcha) {
    510  1.1       cgd 		rm(mailname);
    511  1.1       cgd 		printf("removed\n");
    512  1.1       cgd 	} else
    513  1.1       cgd 		printf("complete\n");
    514  1.1       cgd 	fflush(stdout);
    515  1.1       cgd 
    516  1.1       cgd done:
    517  1.1       cgd 	relsesigs();
    518  1.1       cgd }
    519