xref: /titanic_41/usr/src/cmd/mailx/quit.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
28*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
29*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
32*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
33*7c478bd9Sstevel@tonic-gate  * contributors.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "rcv.h"
39*7c478bd9Sstevel@tonic-gate #include <locale.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
43*7c478bd9Sstevel@tonic-gate  *	mail program
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * Rcv -- receive mail rationally.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * Termination processing.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static void		writeback(int noremove);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #define PRIV(x)		setgid(myegid), (x), setgid(myrgid);
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * Save all of the undetermined messages at the top of "mbox"
56*7c478bd9Sstevel@tonic-gate  * Save all untouched messages back in the system mailbox.
57*7c478bd9Sstevel@tonic-gate  * Remove the system mailbox, if none saved there.
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate void
quit(int noremove)61*7c478bd9Sstevel@tonic-gate quit(
62*7c478bd9Sstevel@tonic-gate     int noremove	/* don't remove system mailbox, trunc it instead */
63*7c478bd9Sstevel@tonic-gate )
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	int mcount, p, modify, autohold, anystat, holdbit, nohold, fd;
66*7c478bd9Sstevel@tonic-gate 	FILE *ibuf, *obuf, *fbuf, *readstat;
67*7c478bd9Sstevel@tonic-gate 	register struct message *mp;
68*7c478bd9Sstevel@tonic-gate 	register int c;
69*7c478bd9Sstevel@tonic-gate 	char *id;
70*7c478bd9Sstevel@tonic-gate 	int appending;
71*7c478bd9Sstevel@tonic-gate 	char *mbox = Getf("MBOX");
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	 * If we are read only, we can't do anything,
75*7c478bd9Sstevel@tonic-gate 	 * so just return quickly.
76*7c478bd9Sstevel@tonic-gate 	 */
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	mcount = 0;
79*7c478bd9Sstevel@tonic-gate 	if (readonly)
80*7c478bd9Sstevel@tonic-gate 		return;
81*7c478bd9Sstevel@tonic-gate 	/*
82*7c478bd9Sstevel@tonic-gate 	 * See if there any messages to save in mbox.  If no, we
83*7c478bd9Sstevel@tonic-gate 	 * can save copying mbox to /tmp and back.
84*7c478bd9Sstevel@tonic-gate 	 *
85*7c478bd9Sstevel@tonic-gate 	 * Check also to see if any files need to be preserved.
86*7c478bd9Sstevel@tonic-gate 	 * Delete all untouched messages to keep them out of mbox.
87*7c478bd9Sstevel@tonic-gate 	 * If all the messages are to be preserved, just exit with
88*7c478bd9Sstevel@tonic-gate 	 * a message.
89*7c478bd9Sstevel@tonic-gate 	 *
90*7c478bd9Sstevel@tonic-gate 	 * If the luser has sent mail to himself, refuse to do
91*7c478bd9Sstevel@tonic-gate 	 * anything with the mailbox, unless mail locking works.
92*7c478bd9Sstevel@tonic-gate 	 */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate #ifndef CANLOCK
95*7c478bd9Sstevel@tonic-gate 	if (selfsent) {
96*7c478bd9Sstevel@tonic-gate 		printf(gettext("You have new mail.\n"));
97*7c478bd9Sstevel@tonic-gate 		return;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate #endif
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/*
102*7c478bd9Sstevel@tonic-gate 	 * Adjust the message flags in each message.
103*7c478bd9Sstevel@tonic-gate 	 */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	anystat = 0;
106*7c478bd9Sstevel@tonic-gate 	autohold = value("hold") != NOSTR;
107*7c478bd9Sstevel@tonic-gate 	appending = value("append") != NOSTR;
108*7c478bd9Sstevel@tonic-gate 	holdbit = autohold ? MPRESERVE : MBOX;
109*7c478bd9Sstevel@tonic-gate 	nohold = MBOXED|MBOX|MSAVED|MDELETED|MPRESERVE;
110*7c478bd9Sstevel@tonic-gate 	if (value("keepsave") != NOSTR)
111*7c478bd9Sstevel@tonic-gate 		nohold &= ~MSAVED;
112*7c478bd9Sstevel@tonic-gate 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
113*7c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MNEW) {
114*7c478bd9Sstevel@tonic-gate 			receipt(mp);
115*7c478bd9Sstevel@tonic-gate 			mp->m_flag &= ~MNEW;
116*7c478bd9Sstevel@tonic-gate 			mp->m_flag |= MSTATUS;
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MSTATUS)
119*7c478bd9Sstevel@tonic-gate 			anystat++;
120*7c478bd9Sstevel@tonic-gate 		if ((mp->m_flag & MTOUCH) == 0)
121*7c478bd9Sstevel@tonic-gate 			mp->m_flag |= MPRESERVE;
122*7c478bd9Sstevel@tonic-gate 		if ((mp->m_flag & nohold) == 0)
123*7c478bd9Sstevel@tonic-gate 			mp->m_flag |= holdbit;
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	modify = 0;
126*7c478bd9Sstevel@tonic-gate 	if (Tflag != NOSTR) {
127*7c478bd9Sstevel@tonic-gate 		if ((readstat = fopen(Tflag, "w")) == NULL)
128*7c478bd9Sstevel@tonic-gate 			Tflag = NOSTR;
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
131*7c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MBOX)
132*7c478bd9Sstevel@tonic-gate 			c++;
133*7c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MPRESERVE)
134*7c478bd9Sstevel@tonic-gate 			p++;
135*7c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MODIFY)
136*7c478bd9Sstevel@tonic-gate 			modify++;
137*7c478bd9Sstevel@tonic-gate 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
138*7c478bd9Sstevel@tonic-gate 			id = hfield("message-id", mp, addone);
139*7c478bd9Sstevel@tonic-gate 			if (id != NOSTR)
140*7c478bd9Sstevel@tonic-gate 				fprintf(readstat, "%s\n", id);
141*7c478bd9Sstevel@tonic-gate 			else {
142*7c478bd9Sstevel@tonic-gate 				id = hfield("article-id", mp, addone);
143*7c478bd9Sstevel@tonic-gate 				if (id != NOSTR)
144*7c478bd9Sstevel@tonic-gate 					fprintf(readstat, "%s\n", id);
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	if (Tflag != NOSTR)
149*7c478bd9Sstevel@tonic-gate 		fclose(readstat);
150*7c478bd9Sstevel@tonic-gate 	if (p == msgCount && !modify && !anystat) {
151*7c478bd9Sstevel@tonic-gate 		if (p == 1)
152*7c478bd9Sstevel@tonic-gate 			printf(gettext("Held 1 message in %s\n"), mailname);
153*7c478bd9Sstevel@tonic-gate 		else
154*7c478bd9Sstevel@tonic-gate 			printf(gettext("Held %d messages in %s\n"), p,
155*7c478bd9Sstevel@tonic-gate 			    mailname);
156*7c478bd9Sstevel@tonic-gate 		return;
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 	if (c == 0) {
159*7c478bd9Sstevel@tonic-gate 		writeback(noremove);
160*7c478bd9Sstevel@tonic-gate 		return;
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/*
164*7c478bd9Sstevel@tonic-gate 	 * Create another temporary file and copy user's mbox file
165*7c478bd9Sstevel@tonic-gate 	 * therein.  If there is no mbox, copy nothing.
166*7c478bd9Sstevel@tonic-gate 	 * If s/he has specified "append" don't copy the mailbox,
167*7c478bd9Sstevel@tonic-gate 	 * just copy saveable entries at the end.
168*7c478bd9Sstevel@tonic-gate 	 */
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	mcount = c;
171*7c478bd9Sstevel@tonic-gate 	if (!appending) {
172*7c478bd9Sstevel@tonic-gate 		if ((fd = open(tempQuit, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
173*7c478bd9Sstevel@tonic-gate 		(obuf = fdopen(fd, "w")) == NULL) {
174*7c478bd9Sstevel@tonic-gate 			perror(tempQuit);
175*7c478bd9Sstevel@tonic-gate 			return;
176*7c478bd9Sstevel@tonic-gate 		}
177*7c478bd9Sstevel@tonic-gate 		if ((ibuf = fopen(tempQuit, "r")) == NULL) {
178*7c478bd9Sstevel@tonic-gate 			perror(tempQuit);
179*7c478bd9Sstevel@tonic-gate 			removefile(tempQuit);
180*7c478bd9Sstevel@tonic-gate 			fclose(obuf);
181*7c478bd9Sstevel@tonic-gate 			return;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 		removefile(tempQuit);
184*7c478bd9Sstevel@tonic-gate 		if ((fbuf = fopen(mbox, "r")) != NULL) {
185*7c478bd9Sstevel@tonic-gate 			while ((c = getc(fbuf)) != EOF)
186*7c478bd9Sstevel@tonic-gate 				putc(c, obuf);
187*7c478bd9Sstevel@tonic-gate 			fclose(fbuf);
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		fflush(obuf);
190*7c478bd9Sstevel@tonic-gate 		if (fferror(obuf)) {
191*7c478bd9Sstevel@tonic-gate 			perror(tempQuit);
192*7c478bd9Sstevel@tonic-gate 			fclose(ibuf);
193*7c478bd9Sstevel@tonic-gate 			fclose(obuf);
194*7c478bd9Sstevel@tonic-gate 			return;
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 		fclose(obuf);
197*7c478bd9Sstevel@tonic-gate 		if ((fd = open(mbox, O_RDWR|O_CREAT|O_TRUNC, MBOXPERM)) < 0 ||
198*7c478bd9Sstevel@tonic-gate 		    (obuf = fdopen(fd, "r+")) == NULL) {
199*7c478bd9Sstevel@tonic-gate 			perror(mbox);
200*7c478bd9Sstevel@tonic-gate 			fclose(ibuf);
201*7c478bd9Sstevel@tonic-gate 			return;
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		if (issysmbox)
204*7c478bd9Sstevel@tonic-gate 			touchlock();
205*7c478bd9Sstevel@tonic-gate 	} else {	/* we are appending */
206*7c478bd9Sstevel@tonic-gate 		if ((fd = open(mbox, O_RDWR|O_CREAT, MBOXPERM)) < 0 ||
207*7c478bd9Sstevel@tonic-gate 		    (obuf = fdopen(fd, "a")) == NULL) {
208*7c478bd9Sstevel@tonic-gate 			perror(mbox);
209*7c478bd9Sstevel@tonic-gate 			return;
210*7c478bd9Sstevel@tonic-gate 		}
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
213*7c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MBOX) {
214*7c478bd9Sstevel@tonic-gate 			if (msend(mp, obuf, (int)value("alwaysignore") ?
215*7c478bd9Sstevel@tonic-gate 			    M_IGNORE|M_SAVING : M_SAVING, fputs) < 0) {
216*7c478bd9Sstevel@tonic-gate 				perror(mbox);
217*7c478bd9Sstevel@tonic-gate 				if (!appending)
218*7c478bd9Sstevel@tonic-gate 					fclose(ibuf);
219*7c478bd9Sstevel@tonic-gate 				fclose(obuf);
220*7c478bd9Sstevel@tonic-gate 				return;
221*7c478bd9Sstevel@tonic-gate 			}
222*7c478bd9Sstevel@tonic-gate 			mp->m_flag &= ~MBOX;
223*7c478bd9Sstevel@tonic-gate 			mp->m_flag |= MBOXED;
224*7c478bd9Sstevel@tonic-gate 			if (issysmbox)
225*7c478bd9Sstevel@tonic-gate 				touchlock();
226*7c478bd9Sstevel@tonic-gate 		}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	/*
229*7c478bd9Sstevel@tonic-gate 	 * Copy the user's old mbox contents back
230*7c478bd9Sstevel@tonic-gate 	 * to the end of the stuff we just saved.
231*7c478bd9Sstevel@tonic-gate 	 * If we are appending, this is unnecessary.
232*7c478bd9Sstevel@tonic-gate 	 */
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	if (!appending) {
235*7c478bd9Sstevel@tonic-gate 		rewind(ibuf);
236*7c478bd9Sstevel@tonic-gate 		c = getc(ibuf);
237*7c478bd9Sstevel@tonic-gate 		while (c != EOF) {
238*7c478bd9Sstevel@tonic-gate 			putc(c, obuf);
239*7c478bd9Sstevel@tonic-gate 			if (ferror(obuf))
240*7c478bd9Sstevel@tonic-gate 				break;
241*7c478bd9Sstevel@tonic-gate 			c = getc(ibuf);
242*7c478bd9Sstevel@tonic-gate 		}
243*7c478bd9Sstevel@tonic-gate 		fclose(ibuf);
244*7c478bd9Sstevel@tonic-gate 		fflush(obuf);
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	trunc(obuf);
247*7c478bd9Sstevel@tonic-gate 	if (fferror(obuf)) {
248*7c478bd9Sstevel@tonic-gate 		perror(mbox);
249*7c478bd9Sstevel@tonic-gate 		fclose(obuf);
250*7c478bd9Sstevel@tonic-gate 		return;
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 	fclose(obuf);
253*7c478bd9Sstevel@tonic-gate 	if (mcount == 1)
254*7c478bd9Sstevel@tonic-gate 		printf(gettext("Saved 1 message in %s\n"), mbox);
255*7c478bd9Sstevel@tonic-gate 	else
256*7c478bd9Sstevel@tonic-gate 		printf(gettext("Saved %d messages in %s\n"), mcount, mbox);
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	/*
259*7c478bd9Sstevel@tonic-gate 	 * Now we are ready to copy back preserved files to
260*7c478bd9Sstevel@tonic-gate 	 * the system mailbox, if any were requested.
261*7c478bd9Sstevel@tonic-gate 	 */
262*7c478bd9Sstevel@tonic-gate 	writeback(noremove);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate  * Preserve all the appropriate messages back in the system
267*7c478bd9Sstevel@tonic-gate  * mailbox, and print a nice message indicating how many were
268*7c478bd9Sstevel@tonic-gate  * saved.  Incorporate any new mail that we found.
269*7c478bd9Sstevel@tonic-gate  */
270*7c478bd9Sstevel@tonic-gate static void
writeback(int noremove)271*7c478bd9Sstevel@tonic-gate writeback(int noremove)
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate 	register struct message *mp;
274*7c478bd9Sstevel@tonic-gate 	register int p, c;
275*7c478bd9Sstevel@tonic-gate 	struct stat st;
276*7c478bd9Sstevel@tonic-gate 	FILE *obuf = 0, *fbuf = 0, *rbuf = 0;
277*7c478bd9Sstevel@tonic-gate 	void (*fhup)(int), (*fint)(int), (*fquit)(int);
278*7c478bd9Sstevel@tonic-gate 	int fd = -1;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	fhup = sigset(SIGHUP, SIG_IGN);
281*7c478bd9Sstevel@tonic-gate 	fint = sigset(SIGINT, SIG_IGN);
282*7c478bd9Sstevel@tonic-gate 	fquit = sigset(SIGQUIT, SIG_IGN);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	if (issysmbox)
285*7c478bd9Sstevel@tonic-gate 		lockmail();
286*7c478bd9Sstevel@tonic-gate 	if ((fbuf = fopen(mailname, "r+")) == NULL) {
287*7c478bd9Sstevel@tonic-gate 		perror(mailname);
288*7c478bd9Sstevel@tonic-gate 		goto die;
289*7c478bd9Sstevel@tonic-gate 	}
290*7c478bd9Sstevel@tonic-gate 	if (!issysmbox)
291*7c478bd9Sstevel@tonic-gate 		lock(fbuf, "r+", 1);
292*7c478bd9Sstevel@tonic-gate 	fstat(fileno(fbuf), &st);
293*7c478bd9Sstevel@tonic-gate 	if (st.st_size > mailsize) {
294*7c478bd9Sstevel@tonic-gate 		printf(gettext("New mail has arrived.\n"));
295*7c478bd9Sstevel@tonic-gate 		snprintf(tempResid, PATHSIZE, "%s/:saved/%s", maildir, myname);
296*7c478bd9Sstevel@tonic-gate 		PRIV(rbuf = fopen(tempResid, "w+"));
297*7c478bd9Sstevel@tonic-gate 		if (rbuf == NULL) {
298*7c478bd9Sstevel@tonic-gate 			snprintf(tempResid, PATHSIZE, "/tmp/Rq%-ld", mypid);
299*7c478bd9Sstevel@tonic-gate 			fd = open(tempResid,O_RDWR|O_CREAT|O_EXCL, 0600);
300*7c478bd9Sstevel@tonic-gate 			PRIV(rbuf = fdopen(fd, "w+"));
301*7c478bd9Sstevel@tonic-gate 			if (rbuf == NULL) {
302*7c478bd9Sstevel@tonic-gate 				snprintf(tempResid, PATHSIZE,
303*7c478bd9Sstevel@tonic-gate 					"%s/:saved/%s", maildir,
304*7c478bd9Sstevel@tonic-gate 				    myname);
305*7c478bd9Sstevel@tonic-gate 				perror(tempResid);
306*7c478bd9Sstevel@tonic-gate 				fclose(fbuf);
307*7c478bd9Sstevel@tonic-gate 				goto die;
308*7c478bd9Sstevel@tonic-gate 			}
309*7c478bd9Sstevel@tonic-gate 		}
310*7c478bd9Sstevel@tonic-gate #ifdef APPEND
311*7c478bd9Sstevel@tonic-gate 		fseek(fbuf, mailsize, 0);
312*7c478bd9Sstevel@tonic-gate 		while ((c = getc(fbuf)) != EOF)
313*7c478bd9Sstevel@tonic-gate 			putc(c, rbuf);
314*7c478bd9Sstevel@tonic-gate #else
315*7c478bd9Sstevel@tonic-gate 		p = st.st_size - mailsize;
316*7c478bd9Sstevel@tonic-gate 		while (p-- > 0) {
317*7c478bd9Sstevel@tonic-gate 			c = getc(fbuf);
318*7c478bd9Sstevel@tonic-gate 			if (c == EOF) {
319*7c478bd9Sstevel@tonic-gate 				perror(mailname);
320*7c478bd9Sstevel@tonic-gate 				fclose(fbuf);
321*7c478bd9Sstevel@tonic-gate 				goto die;
322*7c478bd9Sstevel@tonic-gate 			}
323*7c478bd9Sstevel@tonic-gate 			putc(c, rbuf);
324*7c478bd9Sstevel@tonic-gate 		}
325*7c478bd9Sstevel@tonic-gate #endif
326*7c478bd9Sstevel@tonic-gate 		fclose(fbuf);
327*7c478bd9Sstevel@tonic-gate 		fseek(rbuf, 0L, 0);
328*7c478bd9Sstevel@tonic-gate 		if (issysmbox)
329*7c478bd9Sstevel@tonic-gate 			touchlock();
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	if ((obuf = fopen(mailname, "r+")) == NULL) {
333*7c478bd9Sstevel@tonic-gate 		perror(mailname);
334*7c478bd9Sstevel@tonic-gate 		goto die;
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate #ifndef APPEND
337*7c478bd9Sstevel@tonic-gate 	if (rbuf != NULL)
338*7c478bd9Sstevel@tonic-gate 		while ((c = getc(rbuf)) != EOF)
339*7c478bd9Sstevel@tonic-gate 			putc(c, obuf);
340*7c478bd9Sstevel@tonic-gate #endif
341*7c478bd9Sstevel@tonic-gate 	p = 0;
342*7c478bd9Sstevel@tonic-gate 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
343*7c478bd9Sstevel@tonic-gate 		if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
344*7c478bd9Sstevel@tonic-gate 			p++;
345*7c478bd9Sstevel@tonic-gate 			if (msend(mp, obuf, 0, fputs) < 0) {
346*7c478bd9Sstevel@tonic-gate 				perror(mailname);
347*7c478bd9Sstevel@tonic-gate 				goto die;
348*7c478bd9Sstevel@tonic-gate 			}
349*7c478bd9Sstevel@tonic-gate 			if (issysmbox)
350*7c478bd9Sstevel@tonic-gate 				touchlock();
351*7c478bd9Sstevel@tonic-gate 		}
352*7c478bd9Sstevel@tonic-gate #ifdef APPEND
353*7c478bd9Sstevel@tonic-gate 	if (rbuf != NULL)
354*7c478bd9Sstevel@tonic-gate 		while ((c = getc(rbuf)) != EOF)
355*7c478bd9Sstevel@tonic-gate 			putc(c, obuf);
356*7c478bd9Sstevel@tonic-gate #endif
357*7c478bd9Sstevel@tonic-gate 	fflush(obuf);
358*7c478bd9Sstevel@tonic-gate 	trunc(obuf);
359*7c478bd9Sstevel@tonic-gate 	if (fferror(obuf)) {
360*7c478bd9Sstevel@tonic-gate 		perror(mailname);
361*7c478bd9Sstevel@tonic-gate 		goto die;
362*7c478bd9Sstevel@tonic-gate 	}
363*7c478bd9Sstevel@tonic-gate 	alter(mailname);
364*7c478bd9Sstevel@tonic-gate 	if (p) {
365*7c478bd9Sstevel@tonic-gate 		if (p == 1)
366*7c478bd9Sstevel@tonic-gate 			printf(gettext("Held 1 message in %s\n"), mailname);
367*7c478bd9Sstevel@tonic-gate 		else
368*7c478bd9Sstevel@tonic-gate 			printf(gettext("Held %d messages in %s\n"), p,
369*7c478bd9Sstevel@tonic-gate 			    mailname);
370*7c478bd9Sstevel@tonic-gate 	}
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	if (!noremove && (fsize(obuf) == 0) && (value("keep") == NOSTR)) {
373*7c478bd9Sstevel@tonic-gate 		if (stat(mailname, &st) >= 0)
374*7c478bd9Sstevel@tonic-gate 			PRIV(delempty(st.st_mode, mailname));
375*7c478bd9Sstevel@tonic-gate 	}
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate die:
378*7c478bd9Sstevel@tonic-gate 	if (rbuf) {
379*7c478bd9Sstevel@tonic-gate 		fclose(rbuf);
380*7c478bd9Sstevel@tonic-gate 		PRIV(removefile(tempResid));
381*7c478bd9Sstevel@tonic-gate 	}
382*7c478bd9Sstevel@tonic-gate 	if (obuf)
383*7c478bd9Sstevel@tonic-gate 		fclose(obuf);
384*7c478bd9Sstevel@tonic-gate 	if (issysmbox)
385*7c478bd9Sstevel@tonic-gate 		unlockmail();
386*7c478bd9Sstevel@tonic-gate 	sigset(SIGHUP, fhup);
387*7c478bd9Sstevel@tonic-gate 	sigset(SIGINT, fint);
388*7c478bd9Sstevel@tonic-gate 	sigset(SIGQUIT, fquit);
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate void
lockmail(void)392*7c478bd9Sstevel@tonic-gate lockmail(void)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate     PRIV(maillock(lockname,10));
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate void
unlockmail(void)398*7c478bd9Sstevel@tonic-gate unlockmail(void)
399*7c478bd9Sstevel@tonic-gate {
400*7c478bd9Sstevel@tonic-gate     PRIV(mailunlock());
401*7c478bd9Sstevel@tonic-gate }
402