Home | History | Annotate | Line # | Download | only in mail
quit.c revision 1.26
      1  1.24  christos /*	$NetBSD: quit.c,v 1.26 2006/11/28 18:45:32 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.19       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.7     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.5  christos #if 0
     35   1.6       tls static char sccsid[] = "@(#)quit.c	8.2 (Berkeley) 4/28/95";
     36   1.5  christos #else
     37  1.24  christos __RCSID("$NetBSD: quit.c,v 1.26 2006/11/28 18:45:32 christos Exp $");
     38   1.5  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include "rcv.h"
     42   1.3   deraadt #include "extern.h"
     43  1.26  christos #include "thread.h"
     44   1.1       cgd 
     45   1.1       cgd /*
     46   1.1       cgd  * Rcv -- receive mail rationally.
     47   1.1       cgd  *
     48   1.1       cgd  * Termination processing.
     49   1.1       cgd  */
     50   1.1       cgd 
     51   1.1       cgd /*
     52   1.1       cgd  * The "quit" command.
     53   1.1       cgd  */
     54  1.21  christos /*ARGSUSED*/
     55  1.26  christos PUBLIC int
     56  1.24  christos quitcmd(void *v __unused)
     57   1.1       cgd {
     58   1.1       cgd 	/*
     59   1.1       cgd 	 * If we are sourcing, then return 1 so execute() can handle it.
     60   1.1       cgd 	 * Otherwise, return -1 to abort command loop.
     61   1.1       cgd 	 */
     62   1.1       cgd 	if (sourcing)
     63   1.1       cgd 		return 1;
     64   1.1       cgd 	return -1;
     65   1.1       cgd }
     66   1.1       cgd 
     67   1.1       cgd /*
     68  1.26  christos  * Preserve all the appropriate messages back in the system
     69  1.26  christos  * mailbox, and print a nice message indicated how many were
     70  1.26  christos  * saved.  On any error, just return -1.  Else return 0.
     71  1.26  christos  * Incorporate the any new mail that we found.
     72  1.26  christos  */
     73  1.26  christos static int
     74  1.26  christos writeback(FILE *res)
     75  1.26  christos {
     76  1.26  christos 	struct message *mp;
     77  1.26  christos 	int p, c;
     78  1.26  christos 	FILE *obuf;
     79  1.26  christos 
     80  1.26  christos 	p = 0;
     81  1.26  christos 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
     82  1.26  christos 		warn("%s", mailname);
     83  1.26  christos 		return -1;
     84  1.26  christos 	}
     85  1.26  christos #ifndef APPEND
     86  1.26  christos 	if (res != NULL) {
     87  1.26  christos 		while ((c = getc(res)) != EOF)
     88  1.26  christos 			(void)putc(c, obuf);
     89  1.26  christos 		(void)fflush(obuf);
     90  1.26  christos 		if (ferror(obuf)) {
     91  1.26  christos 			warn("%s", mailname);
     92  1.26  christos 			(void)Fclose(obuf);
     93  1.26  christos 			return -1;
     94  1.26  christos 		}
     95  1.26  christos 	}
     96  1.26  christos #endif
     97  1.26  christos 	for (mp = get_message(1); mp; mp = next_message(mp))
     98  1.26  christos 		if ((mp->m_flag & MPRESERVE) || (mp->m_flag & MTOUCH)==0) {
     99  1.26  christos 			p++;
    100  1.26  christos 			if (sendmessage(mp, obuf, NULL, NULL, NULL) < 0) {
    101  1.26  christos 				warn("%s", mailname);
    102  1.26  christos 				(void)Fclose(obuf);
    103  1.26  christos 				return -1;
    104  1.26  christos 			}
    105  1.26  christos 		}
    106  1.26  christos #ifdef APPEND
    107  1.26  christos 	if (res != NULL)
    108  1.26  christos 		while ((c = getc(res)) != EOF)
    109  1.26  christos 			(void)putc(c, obuf);
    110  1.26  christos #endif
    111  1.26  christos 	(void)fflush(obuf);
    112  1.26  christos 	if (!ferror(obuf))
    113  1.26  christos 		trunc(obuf);	/* XXX or should we truncate? */
    114  1.26  christos 	if (ferror(obuf)) {
    115  1.26  christos 		warn("%s", mailname);
    116  1.26  christos 		(void)Fclose(obuf);
    117  1.26  christos 		return -1;
    118  1.26  christos 	}
    119  1.26  christos 	if (res != NULL)
    120  1.26  christos 		(void)Fclose(res);
    121  1.26  christos 	(void)Fclose(obuf);
    122  1.26  christos 	alter(mailname);
    123  1.26  christos 	if (p == 1)
    124  1.26  christos 		(void)printf("Held 1 message in %s\n", mailname);
    125  1.26  christos 	else
    126  1.26  christos 		(void)printf("Held %d messages in %s\n", p, mailname);
    127  1.26  christos 	return 0;
    128  1.26  christos }
    129  1.26  christos 
    130  1.26  christos /*
    131  1.26  christos  * Terminate an editing session by attempting to write out the user's
    132  1.26  christos  * file from the temporary.  Save any new stuff appended to the file.
    133  1.26  christos  */
    134  1.26  christos static void
    135  1.26  christos edstop(void)
    136  1.26  christos {
    137  1.26  christos 	int gotcha, c;
    138  1.26  christos 	struct message *mp;
    139  1.26  christos 	FILE *obuf, *ibuf, *readstat = NULL;
    140  1.26  christos 	struct stat statb;
    141  1.26  christos 	char tempname[PATHSIZE];
    142  1.26  christos 	int fd;
    143  1.26  christos 
    144  1.26  christos 	if (readonly)
    145  1.26  christos 		return;
    146  1.26  christos 	holdsigs();
    147  1.26  christos 	if (Tflag != NULL) {
    148  1.26  christos 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    149  1.26  christos 			Tflag = NULL;
    150  1.26  christos 	}
    151  1.26  christos 	for (mp = get_message(1), gotcha = 0; mp; mp = next_message(mp)) {
    152  1.26  christos 		if (mp->m_flag & MNEW) {
    153  1.26  christos 			mp->m_flag &= ~MNEW;
    154  1.26  christos 			mp->m_flag |= MSTATUS;
    155  1.26  christos 		}
    156  1.26  christos 		if (mp->m_flag & (MMODIFY|MDELETED|MSTATUS))
    157  1.26  christos 			gotcha++;
    158  1.26  christos 		if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    159  1.26  christos 			char *id;
    160  1.26  christos 
    161  1.26  christos 			if ((id = hfield("article-id", mp)) != NULL)
    162  1.26  christos 				(void)fprintf(readstat, "%s\n", id);
    163  1.26  christos 		}
    164  1.26  christos 	}
    165  1.26  christos 	if (Tflag != NULL)
    166  1.26  christos 		(void)Fclose(readstat);
    167  1.26  christos 	if (!gotcha || Tflag != NULL)
    168  1.26  christos 		goto done;
    169  1.26  christos 	ibuf = NULL;
    170  1.26  christos 	if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
    171  1.26  christos 		(void)snprintf(tempname, sizeof(tempname),
    172  1.26  christos 		    "%s/mbox.XXXXXXXXXX", tmpdir);
    173  1.26  christos 		if ((fd = mkstemp(tempname)) == -1 ||
    174  1.26  christos 		    (obuf = Fdopen(fd, "w")) == NULL) {
    175  1.26  christos 			warn("%s", tempname);
    176  1.26  christos 			if (fd != -1)
    177  1.26  christos 				(void)close(fd);
    178  1.26  christos 			relsesigs();
    179  1.26  christos 			reset(0);
    180  1.26  christos 		}
    181  1.26  christos 		if ((ibuf = Fopen(mailname, "r")) == NULL) {
    182  1.26  christos 			warn("%s", mailname);
    183  1.26  christos 			(void)Fclose(obuf);
    184  1.26  christos 			(void)rm(tempname);
    185  1.26  christos 			relsesigs();
    186  1.26  christos 			reset(0);
    187  1.26  christos 		}
    188  1.26  christos 		(void)fseek(ibuf, (long)mailsize, 0);
    189  1.26  christos 		while ((c = getc(ibuf)) != EOF)
    190  1.26  christos 			(void)putc(c, obuf);
    191  1.26  christos 		(void)fflush(obuf);
    192  1.26  christos 		if (ferror(obuf)) {
    193  1.26  christos 			warn("%s", tempname);
    194  1.26  christos 			(void)Fclose(obuf);
    195  1.26  christos 			(void)Fclose(ibuf);
    196  1.26  christos 			(void)rm(tempname);
    197  1.26  christos 			relsesigs();
    198  1.26  christos 			reset(0);
    199  1.26  christos 		}
    200  1.26  christos 		(void)Fclose(ibuf);
    201  1.26  christos 		(void)Fclose(obuf);
    202  1.26  christos 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
    203  1.26  christos 			warn("%s", tempname);
    204  1.26  christos 			(void)rm(tempname);
    205  1.26  christos 			relsesigs();
    206  1.26  christos 			reset(0);
    207  1.26  christos 		}
    208  1.26  christos 		(void)rm(tempname);
    209  1.26  christos 	}
    210  1.26  christos 	(void)printf("\"%s\" ", mailname);
    211  1.26  christos 	(void)fflush(stdout);
    212  1.26  christos 	if ((obuf = Fopen(mailname, "r+")) == NULL) {
    213  1.26  christos 		warn("%s", mailname);
    214  1.26  christos 		relsesigs();
    215  1.26  christos 		reset(0);
    216  1.26  christos 	}
    217  1.26  christos 	trunc(obuf);
    218  1.26  christos 	c = 0;
    219  1.26  christos 	for (mp = get_message(1); mp; mp = next_message(mp)) {
    220  1.26  christos 		if ((mp->m_flag & MDELETED) != 0)
    221  1.26  christos 			continue;
    222  1.26  christos 		c++;
    223  1.26  christos 		if (sendmessage(mp, obuf, NULL, NULL, NULL) < 0) {
    224  1.26  christos 			warn("%s", mailname);
    225  1.26  christos 			relsesigs();
    226  1.26  christos 			reset(0);
    227  1.26  christos 		}
    228  1.26  christos 	}
    229  1.26  christos 	gotcha = (c == 0 && ibuf == NULL);
    230  1.26  christos 	if (ibuf != NULL) {
    231  1.26  christos 		while ((c = getc(ibuf)) != EOF)
    232  1.26  christos 			(void)putc(c, obuf);
    233  1.26  christos 		(void)Fclose(ibuf);
    234  1.26  christos 	}
    235  1.26  christos 	(void)fflush(obuf);
    236  1.26  christos 	if (ferror(obuf)) {
    237  1.26  christos 		warn("%s", mailname);
    238  1.26  christos 		relsesigs();
    239  1.26  christos 		reset(0);
    240  1.26  christos 	}
    241  1.26  christos 	(void)Fclose(obuf);
    242  1.26  christos 	if (gotcha) {
    243  1.26  christos 		(void)rm(mailname);
    244  1.26  christos 		(void)printf("removed\n");
    245  1.26  christos 	} else
    246  1.26  christos 		(void)printf("complete\n");
    247  1.26  christos 	(void)fflush(stdout);
    248  1.26  christos 
    249  1.26  christos done:
    250  1.26  christos 	relsesigs();
    251  1.26  christos }
    252  1.26  christos 
    253  1.26  christos /*
    254   1.1       cgd  * Save all of the undetermined messages at the top of "mbox"
    255   1.1       cgd  * Save all untouched messages back in the system mailbox.
    256   1.1       cgd  * Remove the system mailbox, if none saved there.
    257   1.1       cgd  */
    258  1.26  christos PUBLIC void
    259  1.13       wiz quit(void)
    260   1.1       cgd {
    261   1.1       cgd 	int mcount, p, modify, autohold, anystat, holdbit, nohold;
    262   1.5  christos 	FILE *ibuf = NULL, *obuf, *fbuf, *rbuf, *readstat = NULL, *abuf;
    263   1.7     lukem 	struct message *mp;
    264  1.16       wiz 	int c, fd;
    265   1.1       cgd 	struct stat minfo;
    266  1.20  christos 	const char *mbox;
    267  1.15       wiz 	char tempname[PATHSIZE];
    268   1.1       cgd 
    269   1.9       bad #ifdef __GNUC__
    270   1.9       bad 	obuf = NULL;		/* XXX gcc -Wuninitialized */
    271   1.9       bad #endif
    272   1.9       bad 
    273   1.1       cgd 	/*
    274   1.1       cgd 	 * If we are read only, we can't do anything,
    275   1.1       cgd 	 * so just return quickly.
    276   1.1       cgd 	 */
    277   1.1       cgd 	if (readonly)
    278   1.1       cgd 		return;
    279  1.26  christos 
    280  1.26  christos #ifdef THREAD_SUPPORT
    281  1.26  christos 	(void)showtagscmd(NULL);	/* make sure we see tagged messages */
    282  1.26  christos 	(void)unthreadcmd(NULL);
    283  1.26  christos #endif
    284   1.1       cgd 	/*
    285   1.1       cgd 	 * If editing (not reading system mail box), then do the work
    286   1.1       cgd 	 * in edstop()
    287   1.1       cgd 	 */
    288   1.1       cgd 	if (edit) {
    289   1.1       cgd 		edstop();
    290   1.1       cgd 		return;
    291   1.1       cgd 	}
    292   1.1       cgd 
    293   1.1       cgd 	/*
    294   1.1       cgd 	 * See if there any messages to save in mbox.  If no, we
    295   1.1       cgd 	 * can save copying mbox to /tmp and back.
    296   1.1       cgd 	 *
    297   1.1       cgd 	 * Check also to see if any files need to be preserved.
    298   1.1       cgd 	 * Delete all untouched messages to keep them out of mbox.
    299   1.1       cgd 	 * If all the messages are to be preserved, just exit with
    300   1.1       cgd 	 * a message.
    301   1.1       cgd 	 */
    302   1.1       cgd 
    303   1.1       cgd 	fbuf = Fopen(mailname, "r");
    304   1.1       cgd 	if (fbuf == NULL)
    305   1.1       cgd 		goto newmail;
    306   1.5  christos 	if (flock(fileno(fbuf), LOCK_EX) == -1) {
    307   1.5  christos nolock:
    308  1.16       wiz 		warn("Unable to lock mailbox");
    309  1.21  christos 		(void)Fclose(fbuf);
    310   1.5  christos 		return;
    311   1.5  christos 	}
    312   1.5  christos 	if (dot_lock(mailname, 1, stdout, ".") == -1)
    313   1.5  christos 		goto nolock;
    314   1.1       cgd 	rbuf = NULL;
    315   1.1       cgd 	if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
    316  1.21  christos 		(void)printf("New mail has arrived.\n");
    317  1.17       wiz 		(void)snprintf(tempname, sizeof(tempname),
    318  1.17       wiz 		    "%s/mail.RqXXXXXXXXXX", tmpdir);
    319  1.17       wiz 		if ((fd = mkstemp(tempname)) == -1 ||
    320  1.17       wiz 		    (rbuf = Fdopen(fd, "w")) == NULL) {
    321  1.17       wiz 		    	if (fd != -1)
    322  1.21  christos 				(void)close(fd);
    323   1.1       cgd 			goto newmail;
    324  1.17       wiz 		}
    325   1.1       cgd #ifdef APPEND
    326  1.21  christos 		(void)fseek(fbuf, (long)mailsize, 0);
    327   1.1       cgd 		while ((c = getc(fbuf)) != EOF)
    328  1.18       wiz 			(void)putc(c, rbuf);
    329   1.1       cgd #else
    330   1.1       cgd 		p = minfo.st_size - mailsize;
    331   1.1       cgd 		while (p-- > 0) {
    332   1.1       cgd 			c = getc(fbuf);
    333   1.1       cgd 			if (c == EOF)
    334   1.1       cgd 				goto newmail;
    335  1.18       wiz 			(void)putc(c, rbuf);
    336   1.1       cgd 		}
    337   1.1       cgd #endif
    338  1.18       wiz 		(void)fflush(rbuf);
    339   1.8       bad 		if (ferror(rbuf)) {
    340  1.17       wiz 			warn("%s", tempname);
    341  1.21  christos 			(void)Fclose(rbuf);
    342  1.21  christos 			(void)Fclose(fbuf);
    343   1.8       bad 			dot_unlock(mailname);
    344   1.8       bad 			return;
    345   1.8       bad 		}
    346  1.21  christos 		(void)Fclose(rbuf);
    347  1.17       wiz 		if ((rbuf = Fopen(tempname, "r")) == NULL)
    348   1.1       cgd 			goto newmail;
    349  1.21  christos 		(void)rm(tempname);
    350   1.1       cgd 	}
    351   1.1       cgd 
    352   1.1       cgd 	/*
    353   1.1       cgd 	 * Adjust the message flags in each message.
    354   1.1       cgd 	 */
    355   1.1       cgd 
    356   1.1       cgd 	anystat = 0;
    357  1.26  christos 	autohold = value(ENAME_HOLD) != NULL;
    358   1.1       cgd 	holdbit = autohold ? MPRESERVE : MBOX;
    359   1.1       cgd 	nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
    360  1.26  christos 	if (value(ENAME_KEEPSAVE) != NULL)
    361   1.1       cgd 		nohold &= ~MSAVED;
    362  1.26  christos 	for (mp = get_message(1); mp; mp = next_message(mp)) {
    363   1.1       cgd 		if (mp->m_flag & MNEW) {
    364   1.1       cgd 			mp->m_flag &= ~MNEW;
    365   1.1       cgd 			mp->m_flag |= MSTATUS;
    366   1.1       cgd 		}
    367   1.1       cgd 		if (mp->m_flag & MSTATUS)
    368   1.1       cgd 			anystat++;
    369   1.1       cgd 		if ((mp->m_flag & MTOUCH) == 0)
    370   1.1       cgd 			mp->m_flag |= MPRESERVE;
    371   1.1       cgd 		if ((mp->m_flag & nohold) == 0)
    372   1.1       cgd 			mp->m_flag |= holdbit;
    373   1.1       cgd 	}
    374   1.1       cgd 	modify = 0;
    375  1.14       wiz 	if (Tflag != NULL) {
    376   1.1       cgd 		if ((readstat = Fopen(Tflag, "w")) == NULL)
    377  1.14       wiz 			Tflag = NULL;
    378   1.1       cgd 	}
    379  1.26  christos 	for (c = 0, p = 0, mp = get_message(1); mp; mp = next_message(mp)) {
    380   1.1       cgd 		if (mp->m_flag & MBOX)
    381   1.1       cgd 			c++;
    382   1.1       cgd 		if (mp->m_flag & MPRESERVE)
    383   1.1       cgd 			p++;
    384  1.26  christos 		if (mp->m_flag & MMODIFY)
    385   1.1       cgd 			modify++;
    386  1.14       wiz 		if (Tflag != NULL && (mp->m_flag & (MREAD|MDELETED)) != 0) {
    387   1.1       cgd 			char *id;
    388   1.1       cgd 
    389  1.14       wiz 			if ((id = hfield("article-id", mp)) != NULL)
    390  1.21  christos 				(void)fprintf(readstat, "%s\n", id);
    391   1.1       cgd 		}
    392   1.1       cgd 	}
    393  1.14       wiz 	if (Tflag != NULL)
    394  1.21  christos 		(void)Fclose(readstat);
    395  1.26  christos 	if (p == get_msgCount() && !modify && !anystat) {
    396  1.21  christos 		(void)printf("Held %d message%s in %s\n",
    397   1.1       cgd 			p, p == 1 ? "" : "s", mailname);
    398  1.21  christos 		(void)Fclose(fbuf);
    399   1.5  christos 		dot_unlock(mailname);
    400   1.1       cgd 		return;
    401   1.1       cgd 	}
    402   1.1       cgd 	if (c == 0) {
    403   1.1       cgd 		if (p != 0) {
    404  1.21  christos 			(void)writeback(rbuf);
    405  1.21  christos 			(void)Fclose(fbuf);
    406   1.5  christos 			dot_unlock(mailname);
    407   1.1       cgd 			return;
    408   1.1       cgd 		}
    409   1.1       cgd 		goto cream;
    410   1.1       cgd 	}
    411   1.1       cgd 
    412   1.1       cgd 	/*
    413   1.1       cgd 	 * Create another temporary file and copy user's mbox file
    414   1.1       cgd 	 * darin.  If there is no mbox, copy nothing.
    415   1.1       cgd 	 * If he has specified "append" don't copy his mailbox,
    416   1.1       cgd 	 * just copy saveable entries at the end.
    417   1.1       cgd 	 */
    418   1.1       cgd 
    419   1.1       cgd 	mbox = expand("&");
    420   1.1       cgd 	mcount = c;
    421  1.26  christos 	if (value(ENAME_APPEND) == NULL) {
    422  1.15       wiz 		(void)snprintf(tempname, sizeof(tempname),
    423  1.15       wiz 		    "%s/mail.RmXXXXXXXXXX", tmpdir);
    424  1.15       wiz 		if ((fd = mkstemp(tempname)) == -1 ||
    425  1.15       wiz 		    (obuf = Fdopen(fd, "w")) == NULL) {
    426  1.15       wiz 			warn("%s", tempname);
    427  1.16       wiz 			if (fd != -1)
    428  1.21  christos 				(void)close(fd);
    429  1.21  christos 			(void)Fclose(fbuf);
    430   1.5  christos 			dot_unlock(mailname);
    431   1.1       cgd 			return;
    432   1.1       cgd 		}
    433  1.15       wiz 		if ((ibuf = Fopen(tempname, "r")) == NULL) {
    434  1.15       wiz 			warn("%s", tempname);
    435  1.21  christos 			(void)rm(tempname);
    436  1.21  christos 			(void)Fclose(obuf);
    437  1.21  christos 			(void)Fclose(fbuf);
    438   1.5  christos 			dot_unlock(mailname);
    439   1.1       cgd 			return;
    440   1.1       cgd 		}
    441  1.21  christos 		(void)rm(tempname);
    442   1.1       cgd 		if ((abuf = Fopen(mbox, "r")) != NULL) {
    443   1.1       cgd 			while ((c = getc(abuf)) != EOF)
    444  1.18       wiz 				(void)putc(c, obuf);
    445  1.21  christos 			(void)Fclose(abuf);
    446   1.1       cgd 		}
    447   1.1       cgd 		if (ferror(obuf)) {
    448  1.15       wiz 			warn("%s", tempname);
    449  1.21  christos 			(void)Fclose(ibuf);
    450  1.21  christos 			(void)Fclose(obuf);
    451  1.21  christos 			(void)Fclose(fbuf);
    452   1.5  christos 			dot_unlock(mailname);
    453   1.1       cgd 			return;
    454   1.1       cgd 		}
    455  1.21  christos 		(void)Fclose(obuf);
    456  1.23  christos 		if ((fd = creat(mbox, 0600)) != -1)
    457  1.22  christos 			(void)close(fd);
    458   1.1       cgd 		if ((obuf = Fopen(mbox, "r+")) == NULL) {
    459  1.16       wiz 			warn("%s", mbox);
    460  1.21  christos 			(void)Fclose(ibuf);
    461  1.21  christos 			(void)Fclose(fbuf);
    462   1.5  christos 			dot_unlock(mailname);
    463   1.1       cgd 			return;
    464   1.1       cgd 		}
    465   1.1       cgd 	}
    466   1.5  christos 	else {
    467   1.1       cgd 		if ((obuf = Fopen(mbox, "a")) == NULL) {
    468  1.16       wiz 			warn("%s", mbox);
    469  1.21  christos 			(void)Fclose(fbuf);
    470   1.5  christos 			dot_unlock(mailname);
    471   1.1       cgd 			return;
    472   1.1       cgd 		}
    473  1.21  christos 		(void)fchmod(fileno(obuf), 0600);
    474   1.1       cgd 	}
    475  1.26  christos 	for (mp = get_message(1); mp; mp = next_message(mp))
    476   1.1       cgd 		if (mp->m_flag & MBOX)
    477  1.24  christos 			if (sendmessage(mp, obuf, saveignore, NULL, NULL) < 0) {
    478  1.16       wiz 				warn("%s", mbox);
    479  1.21  christos 				(void)Fclose(ibuf);
    480  1.21  christos 				(void)Fclose(obuf);
    481  1.21  christos 				(void)Fclose(fbuf);
    482   1.5  christos 				dot_unlock(mailname);
    483   1.1       cgd 				return;
    484   1.1       cgd 			}
    485   1.1       cgd 
    486   1.1       cgd 	/*
    487   1.1       cgd 	 * Copy the user's old mbox contents back
    488   1.1       cgd 	 * to the end of the stuff we just saved.
    489   1.1       cgd 	 * If we are appending, this is unnecessary.
    490   1.1       cgd 	 */
    491   1.1       cgd 
    492  1.26  christos 	if (value(ENAME_APPEND) == NULL) {
    493   1.1       cgd 		rewind(ibuf);
    494   1.1       cgd 		c = getc(ibuf);
    495   1.1       cgd 		while (c != EOF) {
    496  1.18       wiz 			(void)putc(c, obuf);
    497   1.1       cgd 			if (ferror(obuf))
    498   1.1       cgd 				break;
    499   1.1       cgd 			c = getc(ibuf);
    500   1.1       cgd 		}
    501  1.21  christos 		(void)Fclose(ibuf);
    502   1.1       cgd 	}
    503  1.21  christos 	(void)fflush(obuf);
    504   1.8       bad 	if (!ferror(obuf))
    505   1.8       bad 		trunc(obuf);	/* XXX or should we truncate? */
    506   1.1       cgd 	if (ferror(obuf)) {
    507  1.16       wiz 		warn("%s", mbox);
    508  1.21  christos 		(void)Fclose(obuf);
    509  1.21  christos 		(void)Fclose(fbuf);
    510   1.5  christos 		dot_unlock(mailname);
    511   1.1       cgd 		return;
    512   1.1       cgd 	}
    513  1.21  christos 	(void)Fclose(obuf);
    514   1.1       cgd 	if (mcount == 1)
    515  1.21  christos 		(void)printf("Saved 1 message in mbox\n");
    516   1.1       cgd 	else
    517  1.21  christos 		(void)printf("Saved %d messages in mbox\n", mcount);
    518   1.1       cgd 
    519   1.1       cgd 	/*
    520   1.1       cgd 	 * Now we are ready to copy back preserved files to
    521   1.1       cgd 	 * the system mailbox, if any were requested.
    522   1.1       cgd 	 */
    523   1.1       cgd 
    524   1.1       cgd 	if (p != 0) {
    525  1.21  christos 		(void)writeback(rbuf);
    526  1.21  christos 		(void)Fclose(fbuf);
    527   1.5  christos 		dot_unlock(mailname);
    528   1.1       cgd 		return;
    529   1.1       cgd 	}
    530   1.1       cgd 
    531   1.1       cgd 	/*
    532  1.16       wiz 	 * Finally, remove his /var/mail file.
    533   1.1       cgd 	 * If new mail has arrived, copy it back.
    534   1.1       cgd 	 */
    535   1.1       cgd 
    536   1.1       cgd cream:
    537   1.1       cgd 	if (rbuf != NULL) {
    538   1.1       cgd 		abuf = Fopen(mailname, "r+");
    539   1.1       cgd 		if (abuf == NULL)
    540   1.1       cgd 			goto newmail;
    541   1.1       cgd 		while ((c = getc(rbuf)) != EOF)
    542  1.18       wiz 			(void)putc(c, abuf);
    543  1.18       wiz 		(void)fflush(abuf);
    544  1.10       bad 		if (ferror(abuf)) {
    545  1.16       wiz 			warn("%s", mailname);
    546  1.21  christos 			(void)Fclose(abuf);
    547  1.21  christos 			(void)Fclose(fbuf);
    548   1.8       bad 			dot_unlock(mailname);
    549   1.8       bad 			return;
    550   1.8       bad 		}
    551  1.21  christos 		(void)Fclose(rbuf);
    552   1.1       cgd 		trunc(abuf);
    553  1.21  christos 		(void)Fclose(abuf);
    554   1.1       cgd 		alter(mailname);
    555  1.21  christos 		(void)Fclose(fbuf);
    556   1.5  christos 		dot_unlock(mailname);
    557   1.1       cgd 		return;
    558   1.1       cgd 	}
    559   1.1       cgd 	demail();
    560  1.21  christos 	(void)Fclose(fbuf);
    561   1.5  christos 	dot_unlock(mailname);
    562   1.1       cgd 	return;
    563   1.1       cgd 
    564   1.1       cgd newmail:
    565  1.21  christos 	(void)printf("Thou hast new mail.\n");
    566   1.5  christos 	if (fbuf != NULL) {
    567  1.21  christos 		(void)Fclose(fbuf);
    568   1.5  christos 		dot_unlock(mailname);
    569   1.5  christos 	}
    570   1.1       cgd }
    571